1 | /* $Id: DrvNATlibslirp.cpp 105380 2024-07-17 14:08:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DrvNATlibslirp - NATlibslirp network transport driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2022-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
33 |
|
---|
34 | #include "DrvNATlibslirp.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * PDM Function Implementations
|
---|
39 | */
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @callback_method_impl{FNPDMTHREADDRV}
|
---|
43 | *
|
---|
44 | * Queues guest process received packet. Triggered by drvNATRecvWakeup.
|
---|
45 | */
|
---|
46 | static DECLCALLBACK(int) drvNATRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
47 | {
|
---|
48 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
49 |
|
---|
50 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
51 | return VINF_SUCCESS;
|
---|
52 |
|
---|
53 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
54 | {
|
---|
55 | RTReqQueueProcess(pThis->hRecvReqQueue, 0);
|
---|
56 | if (ASMAtomicReadU32(&pThis->cPkts) == 0)
|
---|
57 | RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
|
---|
58 | }
|
---|
59 | return VINF_SUCCESS;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @callback_method_impl{FNPDMTHREADWAKEUPDRV}
|
---|
64 | */
|
---|
65 | static DECLCALLBACK(int) drvNATRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
66 | {
|
---|
67 | RT_NOREF(pThread);
|
---|
68 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
69 | int rc;
|
---|
70 | rc = RTSemEventSignal(pThis->EventRecv);
|
---|
71 |
|
---|
72 | STAM_COUNTER_INC(&pThis->StatNATRecvWakeups);
|
---|
73 | return VINF_SUCCESS;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * @brief Processes incoming packet (to guest).
|
---|
78 | *
|
---|
79 | * @param pThis Pointer to DRVNAT state for current context.
|
---|
80 | * @param pBuf Pointer to packet buffer.
|
---|
81 | * @param cb Size of packet in buffer.
|
---|
82 | *
|
---|
83 | * @thread NAT
|
---|
84 | */
|
---|
85 | static DECLCALLBACK(void) drvNATRecvWorker(PDRVNAT pThis, void *pBuf, size_t cb)
|
---|
86 | {
|
---|
87 | int rc;
|
---|
88 | STAM_PROFILE_START(&pThis->StatNATRecv, a);
|
---|
89 |
|
---|
90 | rc = RTCritSectEnter(&pThis->DevAccessLock);
|
---|
91 | AssertRC(rc);
|
---|
92 |
|
---|
93 | STAM_PROFILE_START(&pThis->StatNATRecvWait, b);
|
---|
94 | rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
|
---|
95 | STAM_PROFILE_STOP(&pThis->StatNATRecvWait, b);
|
---|
96 |
|
---|
97 | if (RT_SUCCESS(rc))
|
---|
98 | {
|
---|
99 | rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pBuf, cb);
|
---|
100 | AssertRC(rc);
|
---|
101 | RTMemFree(pBuf);
|
---|
102 | pBuf = NULL;
|
---|
103 | }
|
---|
104 | else if ( rc != VERR_TIMEOUT
|
---|
105 | && rc != VERR_INTERRUPTED)
|
---|
106 | {
|
---|
107 | AssertRC(rc);
|
---|
108 | }
|
---|
109 |
|
---|
110 | rc = RTCritSectLeave(&pThis->DevAccessLock);
|
---|
111 | AssertRC(rc);
|
---|
112 | ASMAtomicDecU32(&pThis->cPkts);
|
---|
113 | drvNATNotifyNATThread(pThis, "drvNATRecvWorker");
|
---|
114 | STAM_PROFILE_STOP(&pThis->StatNATRecv, a);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Frees a S/G buffer allocated by drvNATNetworkUp_AllocBuf.
|
---|
119 | *
|
---|
120 | * @param pThis Pointer to the NAT instance.
|
---|
121 | * @param pSgBuf The S/G buffer to free.
|
---|
122 | *
|
---|
123 | * @thread NAT
|
---|
124 | */
|
---|
125 | static void drvNATFreeSgBuf(PDRVNAT pThis, PPDMSCATTERGATHER pSgBuf)
|
---|
126 | {
|
---|
127 | RT_NOREF(pThis);
|
---|
128 | Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_MAGIC_MASK) == PDMSCATTERGATHER_FLAGS_MAGIC);
|
---|
129 | pSgBuf->fFlags = 0;
|
---|
130 | if (pSgBuf->pvAllocator)
|
---|
131 | {
|
---|
132 | Assert(!pSgBuf->pvUser);
|
---|
133 | RTMemFree(pSgBuf->aSegs[0].pvSeg);
|
---|
134 | }
|
---|
135 | else if (pSgBuf->pvUser)
|
---|
136 | {
|
---|
137 | RTMemFree(pSgBuf->aSegs[0].pvSeg);
|
---|
138 | pSgBuf->aSegs[0].pvSeg = NULL;
|
---|
139 | RTMemFree(pSgBuf->pvUser);
|
---|
140 | pSgBuf->pvUser = NULL;
|
---|
141 | }
|
---|
142 | RTMemFree(pSgBuf);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Worker function for drvNATSend().
|
---|
147 | *
|
---|
148 | * @param pThis Pointer to the NAT instance.
|
---|
149 | * @param pSgBuf The scatter/gather buffer.
|
---|
150 | * @thread NAT
|
---|
151 | */
|
---|
152 | static DECLCALLBACK(void) drvNATSendWorker(PDRVNAT pThis, PPDMSCATTERGATHER pSgBuf)
|
---|
153 | {
|
---|
154 | LogFlowFunc(("pThis=%p pSgBuf=%p\n", pThis, pSgBuf));
|
---|
155 |
|
---|
156 | if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
|
---|
157 | {
|
---|
158 | const uint8_t *m = static_cast<const uint8_t*>(pSgBuf->pvAllocator);
|
---|
159 | if (m)
|
---|
160 | {
|
---|
161 | /*
|
---|
162 | * A normal frame.
|
---|
163 | */
|
---|
164 | LogFlowFunc(("m=%p\n", m));
|
---|
165 | slirp_input(pThis->pNATState->pSlirp, (uint8_t const *)pSgBuf->pvAllocator, (int)pSgBuf->cbUsed);
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | /*
|
---|
170 | * M_EXT buf, need to segment it.
|
---|
171 | */
|
---|
172 |
|
---|
173 | uint8_t const *pbFrame = (uint8_t const *)pSgBuf->aSegs[0].pvSeg;
|
---|
174 | PCPDMNETWORKGSO pGso = (PCPDMNETWORKGSO)pSgBuf->pvUser;
|
---|
175 | /* Do not attempt to segment frames with invalid GSO parameters. */
|
---|
176 | if (PDMNetGsoIsValid((const PDMNETWORKGSO *)pGso, sizeof(*pGso), pSgBuf->cbUsed))
|
---|
177 | {
|
---|
178 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, pSgBuf->cbUsed);
|
---|
179 | Assert(cSegs > 1);
|
---|
180 | for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
181 | {
|
---|
182 | void *pvSeg;
|
---|
183 |
|
---|
184 | /** @todo r=jack: is this fine leaving as a constant instead of dynamic? */
|
---|
185 | pvSeg = RTMemAlloc(DRVNAT_MAXFRAMESIZE);
|
---|
186 |
|
---|
187 | uint32_t cbPayload, cbHdrs;
|
---|
188 | uint32_t offPayload = PDMNetGsoCarveSegment(pGso, pbFrame, pSgBuf->cbUsed,
|
---|
189 | iSeg, cSegs, (uint8_t *)pvSeg, &cbHdrs, &cbPayload);
|
---|
190 | memcpy((uint8_t *)pvSeg + cbHdrs, pbFrame + offPayload, cbPayload);
|
---|
191 |
|
---|
192 | slirp_input(pThis->pNATState->pSlirp, (uint8_t const *)pvSeg, cbPayload + cbHdrs);
|
---|
193 | RTMemFree(pvSeg);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | LogFlowFunc(("leave\n"));
|
---|
200 | drvNATFreeSgBuf(pThis, pSgBuf);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
|
---|
205 | */
|
---|
206 | static DECLCALLBACK(int) drvNATNetworkUp_BeginXmit(PPDMINETWORKUP pInterface, bool fOnWorkerThread)
|
---|
207 | {
|
---|
208 | RT_NOREF(fOnWorkerThread);
|
---|
209 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
|
---|
210 | int rc = RTCritSectTryEnter(&pThis->XmitLock);
|
---|
211 | if (RT_FAILURE(rc))
|
---|
212 | {
|
---|
213 | /** @todo Kick the worker thread when we have one... */
|
---|
214 | rc = VERR_TRY_AGAIN;
|
---|
215 | }
|
---|
216 | LogFlowFunc(("Beginning xmit...\n"));
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
|
---|
222 | */
|
---|
223 | static DECLCALLBACK(int) drvNATNetworkUp_AllocBuf(PPDMINETWORKUP pInterface, size_t cbMin,
|
---|
224 | PCPDMNETWORKGSO pGso, PPPDMSCATTERGATHER ppSgBuf)
|
---|
225 | {
|
---|
226 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
|
---|
227 | Assert(RTCritSectIsOwner(&pThis->XmitLock));
|
---|
228 |
|
---|
229 | LogFlowFunc(("enter\n"));
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Drop the incoming frame if the NAT thread isn't running.
|
---|
233 | */
|
---|
234 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
235 | {
|
---|
236 | Log(("drvNATNetowrkUp_AllocBuf: returns VERR_NET_NO_NETWORK\n"));
|
---|
237 | return VERR_NET_NO_NETWORK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Allocate a scatter/gather buffer and an mbuf.
|
---|
242 | */
|
---|
243 | PPDMSCATTERGATHER pSgBuf = (PPDMSCATTERGATHER)RTMemAllocZ(sizeof(PDMSCATTERGATHER));
|
---|
244 | if (!pSgBuf)
|
---|
245 | return VERR_NO_MEMORY;
|
---|
246 | if (!pGso)
|
---|
247 | {
|
---|
248 | /*
|
---|
249 | * Drop the frame if it is too big.
|
---|
250 | */
|
---|
251 | if (cbMin >= DRVNAT_MAXFRAMESIZE)
|
---|
252 | {
|
---|
253 | Log(("drvNATNetowrkUp_AllocBuf: drops over-sized frame (%u bytes), returns VERR_INVALID_PARAMETER\n",
|
---|
254 | cbMin));
|
---|
255 | RTMemFree(pSgBuf);
|
---|
256 | return VERR_INVALID_PARAMETER;
|
---|
257 | }
|
---|
258 |
|
---|
259 | pSgBuf->pvUser = NULL;
|
---|
260 | pSgBuf->aSegs[0].cbSeg = RT_ALIGN_Z(cbMin, 128);
|
---|
261 | pSgBuf->aSegs[0].pvSeg = RTMemAlloc(pSgBuf->aSegs[0].cbSeg);
|
---|
262 | pSgBuf->pvAllocator = pSgBuf->aSegs[0].pvSeg;
|
---|
263 |
|
---|
264 | if (!pSgBuf->pvAllocator)
|
---|
265 | {
|
---|
266 | RTMemFree(pSgBuf);
|
---|
267 | return VERR_TRY_AGAIN;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | /*
|
---|
273 | * Drop the frame if its segment is too big.
|
---|
274 | */
|
---|
275 | if (pGso->cbHdrsTotal + pGso->cbMaxSeg >= DRVNAT_MAXFRAMESIZE)
|
---|
276 | {
|
---|
277 | Log(("drvNATNetowrkUp_AllocBuf: drops over-sized frame (%u bytes), returns VERR_INVALID_PARAMETER\n",
|
---|
278 | pGso->cbHdrsTotal + pGso->cbMaxSeg));
|
---|
279 | RTMemFree(pSgBuf);
|
---|
280 | return VERR_INVALID_PARAMETER;
|
---|
281 | }
|
---|
282 |
|
---|
283 | pSgBuf->pvUser = RTMemDup(pGso, sizeof(*pGso));
|
---|
284 | pSgBuf->pvAllocator = NULL;
|
---|
285 |
|
---|
286 | /** @todo r=jack: figure out why need *2 */
|
---|
287 | pSgBuf->aSegs[0].cbSeg = RT_ALIGN_Z(cbMin*2, 128);
|
---|
288 | pSgBuf->aSegs[0].pvSeg = RTMemAlloc(pSgBuf->aSegs[0].cbSeg);
|
---|
289 | if (!pSgBuf->pvUser || !pSgBuf->aSegs[0].pvSeg)
|
---|
290 | {
|
---|
291 | RTMemFree(pSgBuf->aSegs[0].pvSeg);
|
---|
292 | RTMemFree(pSgBuf->pvUser);
|
---|
293 | RTMemFree(pSgBuf);
|
---|
294 | return VERR_TRY_AGAIN;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * Initialize the S/G buffer and return.
|
---|
300 | */
|
---|
301 | pSgBuf->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
|
---|
302 | pSgBuf->cbUsed = 0;
|
---|
303 | pSgBuf->cbAvailable = pSgBuf->aSegs[0].cbSeg;
|
---|
304 | pSgBuf->cSegs = 1;
|
---|
305 |
|
---|
306 | *ppSgBuf = pSgBuf;
|
---|
307 | return VINF_SUCCESS;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
|
---|
312 | */
|
---|
313 | static DECLCALLBACK(int) drvNATNetworkUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
|
---|
314 | {
|
---|
315 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
|
---|
316 | Assert(RTCritSectIsOwner(&pThis->XmitLock));
|
---|
317 | drvNATFreeSgBuf(pThis, pSgBuf);
|
---|
318 | return VINF_SUCCESS;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
|
---|
323 | */
|
---|
324 | static DECLCALLBACK(int) drvNATNetworkUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
|
---|
325 | {
|
---|
326 | RT_NOREF(fOnWorkerThread);
|
---|
327 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
|
---|
328 | Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_OWNER_MASK) == PDMSCATTERGATHER_FLAGS_OWNER_1);
|
---|
329 | Assert(RTCritSectIsOwner(&pThis->XmitLock));
|
---|
330 |
|
---|
331 | LogFlowFunc(("enter\n"));
|
---|
332 |
|
---|
333 | int rc;
|
---|
334 | if (pThis->pSlirpThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
335 | {
|
---|
336 | rc = RTReqQueueCallEx(pThis->hSlirpReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
337 | (PFNRT)drvNATSendWorker, 2, pThis, pSgBuf);
|
---|
338 | if (RT_SUCCESS(rc))
|
---|
339 | {
|
---|
340 | drvNATNotifyNATThread(pThis, "drvNATNetworkUp_SendBuf");
|
---|
341 | LogFlowFunc(("leave success\n"));
|
---|
342 | return VINF_SUCCESS;
|
---|
343 | }
|
---|
344 |
|
---|
345 | rc = VERR_NET_NO_BUFFER_SPACE;
|
---|
346 | }
|
---|
347 | else
|
---|
348 | rc = VERR_NET_DOWN;
|
---|
349 | drvNATFreeSgBuf(pThis, pSgBuf);
|
---|
350 | LogFlowFunc(("leave rc=%Rrc\n", rc));
|
---|
351 | return rc;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
|
---|
356 | */
|
---|
357 | static DECLCALLBACK(void) drvNATNetworkUp_EndXmit(PPDMINETWORKUP pInterface)
|
---|
358 | {
|
---|
359 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
|
---|
360 | RTCritSectLeave(&pThis->XmitLock);
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Get the NAT thread out of poll/WSAWaitForMultipleEvents
|
---|
365 | */
|
---|
366 | static void drvNATNotifyNATThread(PDRVNAT pThis, const char *pszWho)
|
---|
367 | {
|
---|
368 | RT_NOREF(pszWho);
|
---|
369 | int rc = 0;
|
---|
370 | #ifndef RT_OS_WINDOWS
|
---|
371 | /* kick poll() */
|
---|
372 | size_t cbIgnored;
|
---|
373 | rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
|
---|
374 | #else
|
---|
375 | RT_NOREF(pThis);
|
---|
376 | #endif
|
---|
377 | AssertRC(rc);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
|
---|
382 | */
|
---|
383 | static DECLCALLBACK(void) drvNATNetworkUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
|
---|
384 | {
|
---|
385 | RT_NOREF(pInterface, fPromiscuous);
|
---|
386 | LogFlow(("drvNATNetworkUp_SetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
387 | /* nothing to do */
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Worker function for drvNATNetworkUp_NotifyLinkChanged().
|
---|
392 | * @thread "NAT" thread.
|
---|
393 | *
|
---|
394 | * @param pThis Pointer to DRVNAT state for current context.
|
---|
395 | * @param enmLinkState Enum value of link state.
|
---|
396 | *
|
---|
397 | * @thread NAT
|
---|
398 | */
|
---|
399 | static DECLCALLBACK(void) drvNATNotifyLinkChangedWorker(PDRVNAT pThis, PDMNETWORKLINKSTATE enmLinkState)
|
---|
400 | {
|
---|
401 | pThis->enmLinkState = pThis->enmLinkStateWant = enmLinkState;
|
---|
402 | switch (enmLinkState)
|
---|
403 | {
|
---|
404 | case PDMNETWORKLINKSTATE_UP:
|
---|
405 | LogRel(("NAT: Link up\n"));
|
---|
406 | break;
|
---|
407 |
|
---|
408 | case PDMNETWORKLINKSTATE_DOWN:
|
---|
409 | case PDMNETWORKLINKSTATE_DOWN_RESUME:
|
---|
410 | LogRel(("NAT: Link down\n"));
|
---|
411 | break;
|
---|
412 |
|
---|
413 | default:
|
---|
414 | AssertMsgFailed(("drvNATNetworkUp_NotifyLinkChanged: unexpected link state %d\n", enmLinkState));
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Notification on link status changes.
|
---|
420 | *
|
---|
421 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
422 | * @param enmLinkState The new link state.
|
---|
423 | *
|
---|
424 | * @thread EMT
|
---|
425 | */
|
---|
426 | static DECLCALLBACK(void) drvNATNetworkUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
427 | {
|
---|
428 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
|
---|
429 |
|
---|
430 | LogFlow(("drvNATNetworkUp_NotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
431 |
|
---|
432 | /* Don't queue new requests if the NAT thread is not running (e.g. paused,
|
---|
433 | * stopping), otherwise we would deadlock. Memorize the change. */
|
---|
434 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
435 | {
|
---|
436 | pThis->enmLinkStateWant = enmLinkState;
|
---|
437 | return;
|
---|
438 | }
|
---|
439 |
|
---|
440 | PRTREQ pReq;
|
---|
441 | int rc = RTReqQueueCallEx(pThis->hSlirpReqQueue, &pReq, 0 /*cMillies*/, RTREQFLAGS_VOID,
|
---|
442 | (PFNRT)drvNATNotifyLinkChangedWorker, 2, pThis, enmLinkState);
|
---|
443 | if (rc == VERR_TIMEOUT)
|
---|
444 | {
|
---|
445 | drvNATNotifyNATThread(pThis, "drvNATNetworkUp_NotifyLinkChanged");
|
---|
446 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
|
---|
447 | AssertRC(rc);
|
---|
448 | }
|
---|
449 | else
|
---|
450 | AssertRC(rc);
|
---|
451 | RTReqRelease(pReq);
|
---|
452 | }
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * NAT thread handling the slirp stuff.
|
---|
456 | *
|
---|
457 | * The slirp implementation is single-threaded so we execute this enginre in a
|
---|
458 | * dedicated thread. We take care that this thread does not become the
|
---|
459 | * bottleneck: If the guest wants to send, a request is enqueued into the
|
---|
460 | * hSlirpReqQueue and handled asynchronously by this thread. If this thread
|
---|
461 | * wants to deliver packets to the guest, it enqueues a request into
|
---|
462 | * hRecvReqQueue which is later handled by the Recv thread.
|
---|
463 | *
|
---|
464 | * @param pDrvIns Pointer to PDM driver context.
|
---|
465 | * @param pThread Pointer to calling thread context.
|
---|
466 | *
|
---|
467 | * @returns VBox status code
|
---|
468 | *
|
---|
469 | * @thread NAT
|
---|
470 | */
|
---|
471 | static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
472 | {
|
---|
473 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
474 | #ifdef RT_OS_WINDOWS
|
---|
475 | unsigned int cBreak = 0;
|
---|
476 | #else /* RT_OS_WINDOWS */
|
---|
477 | unsigned int cPollNegRet = 0;
|
---|
478 | drvNAT_AddPollCb(RTPipeToNative(pThis->hPipeRead), SLIRP_POLL_IN | SLIRP_POLL_HUP, pThis);
|
---|
479 | pThis->pNATState->polls[0].fd = RTPipeToNative(pThis->hPipeRead);
|
---|
480 | pThis->pNATState->polls[0].events = POLLRDNORM | POLLPRI | POLLRDBAND;
|
---|
481 | pThis->pNATState->polls[0].revents = 0;
|
---|
482 | #endif /* !RT_OS_WINDOWS */
|
---|
483 |
|
---|
484 | LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
|
---|
485 |
|
---|
486 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
487 | return VINF_SUCCESS;
|
---|
488 |
|
---|
489 | if (pThis->enmLinkStateWant != pThis->enmLinkState)
|
---|
490 | drvNATNotifyLinkChangedWorker(pThis, pThis->enmLinkStateWant);
|
---|
491 |
|
---|
492 | /*
|
---|
493 | * Polling loop.
|
---|
494 | */
|
---|
495 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
496 | {
|
---|
497 | /*
|
---|
498 | * To prevent concurrent execution of sending/receiving threads
|
---|
499 | */
|
---|
500 | #ifndef RT_OS_WINDOWS
|
---|
501 | uint32_t uTimeout = 0;
|
---|
502 | pThis->pNATState->nsock = 1;
|
---|
503 |
|
---|
504 | slirp_pollfds_fill(pThis->pNATState->pSlirp, &uTimeout, drvNAT_AddPollCb /* SlirpAddPollCb */, pThis /* opaque */);
|
---|
505 | drvNAT_UpdateTimeout(&uTimeout, pThis);
|
---|
506 |
|
---|
507 | int cChangedFDs = poll(pThis->pNATState->polls, pThis->pNATState->nsock, uTimeout /* timeout */);
|
---|
508 |
|
---|
509 | if (cChangedFDs < 0)
|
---|
510 | {
|
---|
511 | if (errno == EINTR)
|
---|
512 | {
|
---|
513 | Log2(("NAT: signal was caught while sleep on poll\n"));
|
---|
514 | /* No error, just process all outstanding requests but don't wait */
|
---|
515 | cChangedFDs = 0;
|
---|
516 | }
|
---|
517 | else if (cPollNegRet++ > 128)
|
---|
518 | {
|
---|
519 | LogRel(("NAT: Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
|
---|
520 | cPollNegRet = 0;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | slirp_pollfds_poll(pThis->pNATState->pSlirp, cChangedFDs < 0, drvNAT_GetREventsCb /* SlirpGetREventsCb */, pThis /* opaque */);
|
---|
526 | if (pThis->pNATState->polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
|
---|
527 | {
|
---|
528 | /* drain the pipe
|
---|
529 | *
|
---|
530 | * Note! drvNATSend decoupled so we don't know how many times
|
---|
531 | * device's thread sends before we've entered multiplex,
|
---|
532 | * so to avoid false alarm drain pipe here to the very end
|
---|
533 | *
|
---|
534 | * @todo: Probably we should counter drvNATSend to count how
|
---|
535 | * deep pipe has been filed before drain.
|
---|
536 | *
|
---|
537 | */
|
---|
538 | /** @todo XXX: Make it reading exactly we need to drain the
|
---|
539 | * pipe.*/
|
---|
540 | char ch;
|
---|
541 | size_t cbRead;
|
---|
542 | RTPipeRead(pThis->hPipeRead, &ch, 1, &cbRead);
|
---|
543 | }
|
---|
544 |
|
---|
545 | /* process _all_ outstanding requests but don't wait */
|
---|
546 | RTReqQueueProcess(pThis->hSlirpReqQueue, 0);
|
---|
547 | drvNAT_CheckTimeout(pThis);
|
---|
548 |
|
---|
549 | #else /* RT_OS_WINDOWS */
|
---|
550 | uint32_t uTimeout = 0;
|
---|
551 | pThis->pNATState->nsock = 0;
|
---|
552 | slirp_pollfds_fill(pThis->pNATState->pSlirp, &uTimeout, drvNAT_AddPollCb /* SlirpAddPollCb */, pThis /* opaque */);
|
---|
553 | drvNAT_UpdateTimeout(&uTimeout, pThis);
|
---|
554 |
|
---|
555 | int cChangedFDs = WSAPoll(pThis->pNATState->polls, pThis->pNATState->nsock, uTimeout /* timeout */);
|
---|
556 | int error = WSAGetLastError();
|
---|
557 |
|
---|
558 | if (cChangedFDs < 0)
|
---|
559 | {
|
---|
560 | LogFlow(("NAT: WSAPoll returned %d (error %d)\n", cChangedFDs, error));
|
---|
561 | LogFlow(("NSOCK = %d\n", pThis->pNATState->nsock));
|
---|
562 |
|
---|
563 | if (error == 10022)
|
---|
564 | RTThreadSleep(100);
|
---|
565 | }
|
---|
566 |
|
---|
567 | if (cChangedFDs == 0)
|
---|
568 | {
|
---|
569 | /* only check for slow/fast timers */
|
---|
570 | slirp_pollfds_poll(pThis->pNATState->pSlirp, false /*select error*/, drvNAT_GetREventsCb /* SlirpGetREventsCb */, pThis /* opaque */);
|
---|
571 | RTReqQueueProcess(pThis->hSlirpReqQueue, 0);
|
---|
572 | continue;
|
---|
573 | }
|
---|
574 | /* poll the sockets in any case */
|
---|
575 | Log2(("%s: poll\n", __FUNCTION__));
|
---|
576 | slirp_pollfds_poll(pThis->pNATState->pSlirp, cChangedFDs < 0 /*select error*/, drvNAT_GetREventsCb /* SlirpGetREventsCb */, pThis /* opaque */);
|
---|
577 |
|
---|
578 | /* process _all_ outstanding requests but don't wait */
|
---|
579 | RTReqQueueProcess(pThis->hSlirpReqQueue, 0);
|
---|
580 | drvNAT_CheckTimeout(pThis);
|
---|
581 | # ifdef VBOX_NAT_DELAY_HACK
|
---|
582 | if (cBreak++ > 128)
|
---|
583 | {
|
---|
584 | cBreak = 0;
|
---|
585 | RTThreadSleep(2);
|
---|
586 | }
|
---|
587 | # endif
|
---|
588 | #endif /* RT_OS_WINDOWS */
|
---|
589 | }
|
---|
590 |
|
---|
591 | return VINF_SUCCESS;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Unblock the send thread so it can respond to a state change.
|
---|
596 | *
|
---|
597 | * @returns VBox status code.
|
---|
598 | * @param pDrvIns The pcnet device instance.
|
---|
599 | * @param pThread The send thread.
|
---|
600 | *
|
---|
601 | * @thread ?
|
---|
602 | */
|
---|
603 | static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
604 | {
|
---|
605 | RT_NOREF(pThread);
|
---|
606 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
607 |
|
---|
608 | drvNATNotifyNATThread(pThis, "drvNATAsyncIoWakeup");
|
---|
609 | return VINF_SUCCESS;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
614 | */
|
---|
615 | static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
616 | {
|
---|
617 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
618 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
619 |
|
---|
620 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
621 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
|
---|
622 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKNATCONFIG, &pThis->INetworkNATCfg);
|
---|
623 | return NULL;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Info handler.
|
---|
628 | *
|
---|
629 | * @param pDrvIns The PDM driver context.
|
---|
630 | * @param pHlp ....
|
---|
631 | * @param pszArgs Unused.
|
---|
632 | *
|
---|
633 | * @thread any
|
---|
634 | */
|
---|
635 | static DECLCALLBACK(void) drvNATInfo(PPDMDRVINS pDrvIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
636 | {
|
---|
637 | RT_NOREF(pszArgs);
|
---|
638 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
639 | pHlp->pfnPrintf(pHlp, "libslirp Connection Info:\n");
|
---|
640 | pHlp->pfnPrintf(pHlp, slirp_connection_info(pThis->pNATState->pSlirp));
|
---|
641 | pHlp->pfnPrintf(pHlp, "libslirp Neighbor Info:\n");
|
---|
642 | pHlp->pfnPrintf(pHlp, slirp_neighbor_info(pThis->pNATState->pSlirp));
|
---|
643 | pHlp->pfnPrintf(pHlp, "libslirp Version String: %s \n", slirp_version_string());
|
---|
644 | }
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Sets up the redirectors.
|
---|
648 | *
|
---|
649 | * @returns VBox status code.
|
---|
650 | * @param uInstance ?
|
---|
651 | * @param pThis ?
|
---|
652 | * @param pCfg The configuration handle.
|
---|
653 | * @param pNetwork Unused.
|
---|
654 | *
|
---|
655 | * @thread ?
|
---|
656 | */
|
---|
657 | static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfg, PRTNETADDRIPV4 pNetwork)
|
---|
658 | {
|
---|
659 | /** @todo r=jack: rewrite to support IPv6? */
|
---|
660 | PPDMDRVINS pDrvIns = pThis->pDrvIns;
|
---|
661 | PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
|
---|
662 |
|
---|
663 | RT_NOREF(pNetwork); /** @todo figure why pNetwork isn't used */
|
---|
664 |
|
---|
665 | PCFGMNODE pPFTree = pHlp->pfnCFGMGetChild(pCfg, "PortForwarding");
|
---|
666 | if (pPFTree == NULL)
|
---|
667 | return VINF_SUCCESS;
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * Enumerate redirections.
|
---|
671 | */
|
---|
672 | for (PCFGMNODE pNode = pHlp->pfnCFGMGetFirstChild(pPFTree); pNode; pNode = pHlp->pfnCFGMGetNextChild(pNode))
|
---|
673 | {
|
---|
674 | /*
|
---|
675 | * Validate the port forwarding config.
|
---|
676 | */
|
---|
677 | if (!pHlp->pfnCFGMAreValuesValid(pNode, "Name\0Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0BindIP\0"))
|
---|
678 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
|
---|
679 | N_("Unknown configuration in port forwarding"));
|
---|
680 |
|
---|
681 | /* protocol type */
|
---|
682 | bool fUDP;
|
---|
683 | char szProtocol[32];
|
---|
684 | int rc;
|
---|
685 | GET_STRING(rc, pDrvIns, pNode, "Protocol", szProtocol[0], sizeof(szProtocol));
|
---|
686 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
687 | {
|
---|
688 | fUDP = false;
|
---|
689 | GET_BOOL(rc, pDrvIns, pNode, "UDP", fUDP);
|
---|
690 | }
|
---|
691 | else if (RT_SUCCESS(rc))
|
---|
692 | {
|
---|
693 | if (!RTStrICmp(szProtocol, "TCP"))
|
---|
694 | fUDP = false;
|
---|
695 | else if (!RTStrICmp(szProtocol, "UDP"))
|
---|
696 | fUDP = true;
|
---|
697 | else
|
---|
698 | return PDMDrvHlpVMSetError(pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
699 | N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""),
|
---|
700 | iInstance, szProtocol);
|
---|
701 | }
|
---|
702 | else
|
---|
703 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
704 | N_("NAT#%d: configuration query for \"Protocol\" failed"),
|
---|
705 | iInstance);
|
---|
706 | /* host port */
|
---|
707 | int32_t iHostPort;
|
---|
708 | GET_S32_STRICT(rc, pDrvIns, pNode, "HostPort", iHostPort);
|
---|
709 |
|
---|
710 | /* guest port */
|
---|
711 | int32_t iGuestPort;
|
---|
712 | GET_S32_STRICT(rc, pDrvIns, pNode, "GuestPort", iGuestPort);
|
---|
713 |
|
---|
714 | /* host address ("BindIP" name is rather unfortunate given "HostPort" to go with it) */
|
---|
715 | struct in_addr BindIP;
|
---|
716 | RT_ZERO(BindIP);
|
---|
717 | GETIP_DEF(rc, pDrvIns, pNode, BindIP, INADDR_ANY);
|
---|
718 |
|
---|
719 | /* guest address */
|
---|
720 | struct in_addr GuestIP;
|
---|
721 | RT_ZERO(GuestIP);
|
---|
722 | GETIP_DEF(rc, pDrvIns, pNode, GuestIP, INADDR_ANY);
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * Call slirp about it.
|
---|
726 | */
|
---|
727 | if (slirp_add_hostfwd(pThis->pNATState->pSlirp, fUDP, BindIP,
|
---|
728 | iHostPort, GuestIP, iGuestPort) < 0)
|
---|
729 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
|
---|
730 | N_("NAT#%d: configuration error: failed to set up "
|
---|
731 | "redirection of %d to %d. Probably a conflict with "
|
---|
732 | "existing services or other rules"), iInstance, iHostPort,
|
---|
733 | iGuestPort);
|
---|
734 | } /* for each redir rule */
|
---|
735 |
|
---|
736 | return VINF_SUCCESS;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * Applies port forwarding between guest and host.
|
---|
741 | *
|
---|
742 | * @param pThis Pointer to DRVNAT state for current context.
|
---|
743 | * @param fRemove Flag to remove port forward instead of create.
|
---|
744 | * @param fUdp Flag specifying if UDP. If false, TCP.
|
---|
745 | * @param pHostIp String of host IP address.
|
---|
746 | * @param u16HostPort Host port to forward to.
|
---|
747 | * @param pGuestIp String of guest IP address.
|
---|
748 | * @param u16GuestPort Guest port to forward.
|
---|
749 | *
|
---|
750 | * @thread ?
|
---|
751 | */
|
---|
752 | static DECLCALLBACK(void) drvNATNotifyApplyPortForwardCommand(PDRVNAT pThis, bool fRemove,
|
---|
753 | bool fUdp, const char *pHostIp,
|
---|
754 | uint16_t u16HostPort, const char *pGuestIp, uint16_t u16GuestPort)
|
---|
755 | {
|
---|
756 | /** @todo r=jack:
|
---|
757 | * - rewrite for IPv6
|
---|
758 | * - do we want to lock the guestIp to the VMs IP?
|
---|
759 | */
|
---|
760 | struct in_addr guestIp, hostIp;
|
---|
761 |
|
---|
762 | if ( pHostIp == NULL
|
---|
763 | || inet_aton(pHostIp, &hostIp) == 0)
|
---|
764 | hostIp.s_addr = INADDR_ANY;
|
---|
765 |
|
---|
766 | if ( pGuestIp == NULL
|
---|
767 | || inet_aton(pGuestIp, &guestIp) == 0)
|
---|
768 | guestIp.s_addr = pThis->GuestIP;
|
---|
769 |
|
---|
770 | if (fRemove)
|
---|
771 | slirp_remove_hostfwd(pThis->pNATState->pSlirp, fUdp, hostIp, u16HostPort);
|
---|
772 | else
|
---|
773 | slirp_add_hostfwd(pThis->pNATState->pSlirp, fUdp, hostIp,
|
---|
774 | u16HostPort, guestIp, u16GuestPort);
|
---|
775 | }
|
---|
776 |
|
---|
777 | static DECLCALLBACK(int) drvNATNetworkNatConfigRedirect(PPDMINETWORKNATCONFIG pInterface, bool fRemove,
|
---|
778 | bool fUdp, const char *pHostIp, uint16_t u16HostPort,
|
---|
779 | const char *pGuestIp, uint16_t u16GuestPort)
|
---|
780 | {
|
---|
781 | LogFlowFunc(("fRemove=%d, fUdp=%d, pHostIp=%s, u16HostPort=%u, pGuestIp=%s, u16GuestPort=%u\n",
|
---|
782 | RT_BOOL(fRemove), RT_BOOL(fUdp), pHostIp, u16HostPort, pGuestIp, u16GuestPort));
|
---|
783 | PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkNATCfg);
|
---|
784 | /* Execute the command directly if the VM is not running. */
|
---|
785 | int rc;
|
---|
786 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
787 | {
|
---|
788 | drvNATNotifyApplyPortForwardCommand(pThis, fRemove, fUdp, pHostIp,
|
---|
789 | u16HostPort, pGuestIp,u16GuestPort);
|
---|
790 | rc = VINF_SUCCESS;
|
---|
791 | }
|
---|
792 | else
|
---|
793 | {
|
---|
794 | PRTREQ pReq;
|
---|
795 | rc = RTReqQueueCallEx(pThis->hSlirpReqQueue, &pReq, 0 /*cMillies*/, RTREQFLAGS_VOID,
|
---|
796 | (PFNRT)drvNATNotifyApplyPortForwardCommand, 7, pThis, fRemove,
|
---|
797 | fUdp, pHostIp, u16HostPort, pGuestIp, u16GuestPort);
|
---|
798 | if (rc == VERR_TIMEOUT)
|
---|
799 | {
|
---|
800 | drvNATNotifyNATThread(pThis, "drvNATNetworkNatConfigRedirect");
|
---|
801 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
|
---|
802 | AssertRC(rc);
|
---|
803 | }
|
---|
804 | else
|
---|
805 | AssertRC(rc);
|
---|
806 |
|
---|
807 | RTReqRelease(pReq);
|
---|
808 | }
|
---|
809 | return rc;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /**
|
---|
813 | * Libslirp Utility Functions
|
---|
814 | */
|
---|
815 | /**
|
---|
816 | * Update the timeout field in given list of Slirp timers.
|
---|
817 | *
|
---|
818 | * @param uTimeout Pointer to timeout value.
|
---|
819 | * @param opaque Pointer to NAT State context.
|
---|
820 | *
|
---|
821 | * @thread ?
|
---|
822 | */
|
---|
823 | static void drvNAT_UpdateTimeout(uint32_t *uTimeout, void *opaque)
|
---|
824 | {
|
---|
825 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
826 | Assert(pThis);
|
---|
827 |
|
---|
828 | int64_t currTime = drvNAT_ClockGetNsCb(pThis) / (1000 * 1000);
|
---|
829 | SlirpTimer *pCurrent = pThis->pNATState->pTimerHead;
|
---|
830 | while (pCurrent != NULL)
|
---|
831 | {
|
---|
832 | if (pCurrent->uTimeExpire != -1)
|
---|
833 | {
|
---|
834 | int64_t diff = pCurrent->uTimeExpire - currTime;
|
---|
835 |
|
---|
836 | if (diff < 0)
|
---|
837 | diff = 0;
|
---|
838 |
|
---|
839 | if (diff < *uTimeout)
|
---|
840 | *uTimeout = diff;
|
---|
841 | }
|
---|
842 |
|
---|
843 | pCurrent = pCurrent->next;
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Check if timeout has passed in given list of Slirp timers.
|
---|
849 | *
|
---|
850 | * @param opaque Pointer to NAT State context.
|
---|
851 | *
|
---|
852 | * @thread ?
|
---|
853 | */
|
---|
854 | static void drvNAT_CheckTimeout(void *opaque)
|
---|
855 | {
|
---|
856 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
857 | Assert(pThis);
|
---|
858 |
|
---|
859 | int64_t currTime = drvNAT_ClockGetNsCb(pThis) / (1000 * 1000);
|
---|
860 | SlirpTimer *pCurrent = pThis->pNATState->pTimerHead;
|
---|
861 | while (pCurrent != NULL)
|
---|
862 | {
|
---|
863 | if (pCurrent->uTimeExpire != -1)
|
---|
864 | {
|
---|
865 | int64_t diff = pCurrent->uTimeExpire - currTime;
|
---|
866 | if (diff <= 0)
|
---|
867 | {
|
---|
868 | pCurrent->uTimeExpire = -1;
|
---|
869 | pCurrent->pHandler(pCurrent->opaque);
|
---|
870 | }
|
---|
871 | }
|
---|
872 |
|
---|
873 | pCurrent = pCurrent->next;
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * Converts slirp representation of poll events to host representation.
|
---|
879 | *
|
---|
880 | * @param iEvents Integer representing slirp type poll events.
|
---|
881 | *
|
---|
882 | * @returns Integer representing host type poll events.
|
---|
883 | *
|
---|
884 | * @thread ?
|
---|
885 | */
|
---|
886 | static int drvNAT_PollEventSlirpToHost(int iEvents) {
|
---|
887 | int iRet = 0;
|
---|
888 | #ifndef RT_OS_WINDOWS
|
---|
889 | if (iEvents & SLIRP_POLL_IN) iRet |= POLLIN;
|
---|
890 | if (iEvents & SLIRP_POLL_OUT) iRet |= POLLOUT;
|
---|
891 | if (iEvents & SLIRP_POLL_PRI) iRet |= POLLPRI;
|
---|
892 | if (iEvents & SLIRP_POLL_ERR) iRet |= POLLERR;
|
---|
893 | if (iEvents & SLIRP_POLL_HUP) iRet |= POLLHUP;
|
---|
894 | #else
|
---|
895 | if (iEvents & SLIRP_POLL_IN) iRet |= (POLLRDNORM | POLLRDBAND);
|
---|
896 | if (iEvents & SLIRP_POLL_OUT) iRet |= POLLWRNORM;
|
---|
897 | if (iEvents & SLIRP_POLL_PRI) iRet |= (POLLIN);
|
---|
898 | if (iEvents & SLIRP_POLL_ERR) iRet |= 0;
|
---|
899 | if (iEvents & SLIRP_POLL_HUP) iRet |= 0;
|
---|
900 | #endif
|
---|
901 | return iRet;
|
---|
902 | }
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Converts host representation of poll events to slirp representation.
|
---|
906 | *
|
---|
907 | * @param iEvents Integer representing host type poll events.
|
---|
908 | *
|
---|
909 | * @returns Integer representing slirp type poll events.
|
---|
910 | *
|
---|
911 | * @thread ?
|
---|
912 | */
|
---|
913 | static int drvNAT_PollEventHostToSlirp(int iEvents) {
|
---|
914 | int iRet = 0;
|
---|
915 | #ifndef RT_OS_WINDOWS
|
---|
916 | if (iEvents & POLLIN) iRet |= SLIRP_POLL_IN;
|
---|
917 | if (iEvents & POLLOUT) iRet |= SLIRP_POLL_OUT;
|
---|
918 | if (iEvents & POLLPRI) iRet |= SLIRP_POLL_PRI;
|
---|
919 | if (iEvents & POLLERR) iRet |= SLIRP_POLL_ERR;
|
---|
920 | if (iEvents & POLLHUP) iRet |= SLIRP_POLL_HUP;
|
---|
921 | #else
|
---|
922 | if (iEvents & (POLLRDNORM | POLLRDBAND)) iRet |= SLIRP_POLL_IN;
|
---|
923 | if (iEvents & POLLWRNORM) iRet |= SLIRP_POLL_OUT;
|
---|
924 | if (iEvents & (POLLPRI)) iRet |= SLIRP_POLL_PRI;
|
---|
925 | if (iEvents & POLLERR) iRet |= SLIRP_POLL_ERR;
|
---|
926 | if (iEvents & POLLHUP) iRet |= SLIRP_POLL_HUP;
|
---|
927 | #endif
|
---|
928 | return iRet;
|
---|
929 | }
|
---|
930 |
|
---|
931 | /**
|
---|
932 | * Libslirp Callbacks
|
---|
933 | */
|
---|
934 | /**
|
---|
935 | * Callback called by libslirp to send packet into guest.
|
---|
936 | *
|
---|
937 | * @param pBuf Pointer to packet buffer.
|
---|
938 | * @param cb Size of packet.
|
---|
939 | * @param opaque Pointer to NAT State context.
|
---|
940 | *
|
---|
941 | * @returns Size of packet received or -1 on error.
|
---|
942 | *
|
---|
943 | * @thread ?
|
---|
944 | */
|
---|
945 | static DECLCALLBACK(ssize_t) drvNAT_SendPacketCb(const void *pBuf, size_t cb, void *opaque /* PDRVNAT */)
|
---|
946 | {
|
---|
947 | char *pNewBuf = (char *)RTMemAlloc(cb);
|
---|
948 | if (pNewBuf == NULL)
|
---|
949 | return -1;
|
---|
950 |
|
---|
951 | memcpy(pNewBuf, pBuf, cb);
|
---|
952 |
|
---|
953 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
954 | Assert(pThis);
|
---|
955 |
|
---|
956 | LogFlow(("slirp_output BEGIN %p %d\n", pNewBuf, cb));
|
---|
957 | Log6(("slirp_output: pNewBuf=%p cb=%#x (pThis=%p)\n"
|
---|
958 | "%.*Rhxd\n", pNewBuf, cb, pThis, cb, pNewBuf));
|
---|
959 |
|
---|
960 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
961 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
962 | return -1;
|
---|
963 |
|
---|
964 | ASMAtomicIncU32(&pThis->cPkts);
|
---|
965 | int rc = RTReqQueueCallEx(pThis->hRecvReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
966 | (PFNRT)drvNATRecvWorker, 3, pThis, pNewBuf, cb);
|
---|
967 | AssertRC(rc);
|
---|
968 | drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
|
---|
969 | drvNATNotifyNATThread(pThis, "drvNAT_SendPacketCb");
|
---|
970 | STAM_COUNTER_INC(&pThis->StatQueuePktSent);
|
---|
971 | LogFlowFuncLeave();
|
---|
972 | return cb;
|
---|
973 | }
|
---|
974 |
|
---|
975 | /**
|
---|
976 | * Callback called by libslirp on an error from a guest.
|
---|
977 | *
|
---|
978 | * @param pMsg Error message string.
|
---|
979 | * @param opaque Pointer to NAT State context.
|
---|
980 | *
|
---|
981 | * @thread ?
|
---|
982 | */
|
---|
983 | static DECLCALLBACK(void) drvNAT_GuestErrorCb(const char *pMsg, void *opaque)
|
---|
984 | {
|
---|
985 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
986 | Assert(pThis);
|
---|
987 |
|
---|
988 | PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_UNKNOWN_DRVREG_VERSION,
|
---|
989 | N_("Unknown error: "));
|
---|
990 | LogRel((pMsg));
|
---|
991 | }
|
---|
992 |
|
---|
993 | /**
|
---|
994 | * Callback called by libslirp to get the current timestamp in nanoseconds.
|
---|
995 | *
|
---|
996 | * @param opaque Pointer to NAT State context.
|
---|
997 | *
|
---|
998 | * @returns 64-bit signed integer representing time in nanoseconds.
|
---|
999 | */
|
---|
1000 | static DECLCALLBACK(int64_t) drvNAT_ClockGetNsCb(void *opaque)
|
---|
1001 | {
|
---|
1002 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1003 | Assert(pThis);
|
---|
1004 |
|
---|
1005 | RT_NOREF(pThis);
|
---|
1006 |
|
---|
1007 | return (int64_t)RTTimeNanoTS();
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * Callback called by slirp to create a new timer and insert it into the given list.
|
---|
1012 | *
|
---|
1013 | * @param slirpTimeCb Callback function supplied to the new timer upon timer expiry.
|
---|
1014 | * Called later by the timeout handler.
|
---|
1015 | * @param cb_opaque Opaque object supplied to slirpTimeCb when called. Should be
|
---|
1016 | * Identical to the opaque parameter.
|
---|
1017 | * @param opaque Pointer to NAT State context.
|
---|
1018 | *
|
---|
1019 | * @returns Pointer to new timer.
|
---|
1020 | */
|
---|
1021 | static DECLCALLBACK(void *) drvNAT_TimerNewCb(SlirpTimerCb slirpTimeCb, void *cb_opaque, void *opaque)
|
---|
1022 | {
|
---|
1023 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1024 | Assert(pThis);
|
---|
1025 |
|
---|
1026 | SlirpTimer *pNewTimer = (SlirpTimer *)RTMemAlloc(sizeof(SlirpTimer));
|
---|
1027 | if (!pNewTimer)
|
---|
1028 | return NULL;
|
---|
1029 |
|
---|
1030 | pNewTimer->next = pThis->pNATState->pTimerHead;
|
---|
1031 | pNewTimer->uTimeExpire = -1;
|
---|
1032 | pNewTimer->pHandler = slirpTimeCb;
|
---|
1033 | pNewTimer->opaque = cb_opaque;
|
---|
1034 | pThis->pNATState->pTimerHead = pNewTimer;
|
---|
1035 |
|
---|
1036 | return pNewTimer;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | /**
|
---|
1040 | * Callback called by slirp to free a timer.
|
---|
1041 | *
|
---|
1042 | * @param pTimer Pointer to slirpTimer object to be freed.
|
---|
1043 | * @param opaque Pointer to NAT State context.
|
---|
1044 | */
|
---|
1045 | static DECLCALLBACK(void) drvNAT_TimerFreeCb(void *pTimer, void *opaque)
|
---|
1046 | {
|
---|
1047 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1048 | Assert(pThis);
|
---|
1049 | SlirpTimer *pCurrent = pThis->pNATState->pTimerHead;
|
---|
1050 |
|
---|
1051 | while (pCurrent != NULL)
|
---|
1052 | {
|
---|
1053 | if (pCurrent == (SlirpTimer *)pTimer)
|
---|
1054 | {
|
---|
1055 | SlirpTimer *pTmp = pCurrent->next;
|
---|
1056 | RTMemFree(pCurrent);
|
---|
1057 | pCurrent = pTmp;
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | pCurrent = pCurrent->next;
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /**
|
---|
1065 | * Callback called by slirp to modify a timer.
|
---|
1066 | *
|
---|
1067 | * @param pTimer Pointer to slirpTimer object to be modified.
|
---|
1068 | * @param expireTime Signed 64-bit integer representing the new expiry time.
|
---|
1069 | * @param opaque Pointer to NAT State context.
|
---|
1070 | */
|
---|
1071 | static DECLCALLBACK(void) drvNAT_TimerModCb(void *pTimer, int64_t expireTime, void *opaque)
|
---|
1072 | {
|
---|
1073 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1074 | Assert(pThis);
|
---|
1075 |
|
---|
1076 | RT_NOREF(pThis);
|
---|
1077 |
|
---|
1078 | ((SlirpTimer *)pTimer)->uTimeExpire = expireTime;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | /**
|
---|
1082 | * Callback called by slirp when there is I/O that needs to happen.
|
---|
1083 | *
|
---|
1084 | * @param opaque Pointer to NAT State context.
|
---|
1085 | */
|
---|
1086 | static DECLCALLBACK(void) drvNAT_NotifyCb(void *opaque)
|
---|
1087 | {
|
---|
1088 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1089 |
|
---|
1090 | drvNATAsyncIoWakeup(pThis->pDrvIns, NULL);
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | /**
|
---|
1094 | * Registers poll. Unused function (other than logging).
|
---|
1095 | */
|
---|
1096 | static DECLCALLBACK(void) drvNAT_RegisterPoll(int fd, void *opaque) {
|
---|
1097 | RT_NOREF(fd, opaque);
|
---|
1098 | Log4(("Poll registered\n"));
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Unregisters poll. Unused function (other than logging).
|
---|
1103 | */
|
---|
1104 | static DECLCALLBACK(void) drvNAT_UnregisterPoll(int fd, void *opaque) {
|
---|
1105 | RT_NOREF(fd, opaque);
|
---|
1106 | Log4(("Poll unregistered\n"));
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Callback function to add entry to pollfd array.
|
---|
1111 | *
|
---|
1112 | * @param iFd Integer of system file descriptor of socket.
|
---|
1113 | * (on windows, this is a VBox internal, not system, value).
|
---|
1114 | * @param iEvents Integer of slirp type poll events.
|
---|
1115 | * @param opaque Pointer to NAT State context.
|
---|
1116 | *
|
---|
1117 | * @returns Index of latest pollfd entry.
|
---|
1118 | *
|
---|
1119 | * @thread ?
|
---|
1120 | */
|
---|
1121 | static DECLCALLBACK(int) drvNAT_AddPollCb(int iFd, int iEvents, void *opaque)
|
---|
1122 | {
|
---|
1123 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1124 |
|
---|
1125 | if (pThis->pNATState->nsock + 1 >= pThis->pNATState->uPollCap)
|
---|
1126 | {
|
---|
1127 | int cbNew = pThis->pNATState->uPollCap * 2 * sizeof(struct pollfd);
|
---|
1128 | struct pollfd *pvNew = (struct pollfd *)RTMemRealloc(pThis->pNATState->polls, cbNew);
|
---|
1129 | if(pvNew)
|
---|
1130 | {
|
---|
1131 | pThis->pNATState->polls = pvNew;
|
---|
1132 | pThis->pNATState->uPollCap *= 2;
|
---|
1133 | }
|
---|
1134 | else
|
---|
1135 | return -1;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | int idx = pThis->pNATState->nsock;
|
---|
1139 | #ifdef RT_OS_WINDOWS
|
---|
1140 | pThis->pNATState->polls[idx].fd = libslirp_wrap_RTHandleTableLookup(iFd);
|
---|
1141 | #else
|
---|
1142 | pThis->pNATState->polls[idx].fd = iFd;
|
---|
1143 | #endif
|
---|
1144 | pThis->pNATState->polls[idx].events = drvNAT_PollEventSlirpToHost(iEvents);
|
---|
1145 | pThis->pNATState->polls[idx].revents = 0;
|
---|
1146 | pThis->pNATState->nsock += 1;
|
---|
1147 | return idx;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /**
|
---|
1151 | * Get translated revents from a poll at a given index.
|
---|
1152 | *
|
---|
1153 | * @param idx Integer index of poll.
|
---|
1154 | * @param opaque Pointer to NAT State context.
|
---|
1155 | *
|
---|
1156 | * @returns Integer representing transalted revents.
|
---|
1157 | *
|
---|
1158 | * @thread ?
|
---|
1159 | */
|
---|
1160 | static DECLCALLBACK(int) drvNAT_GetREventsCb(int idx, void *opaque)
|
---|
1161 | {
|
---|
1162 | PDRVNAT pThis = (PDRVNAT)opaque;
|
---|
1163 | struct pollfd* polls = pThis->pNATState->polls;
|
---|
1164 | return drvNAT_PollEventHostToSlirp(polls[idx].revents);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | /**
|
---|
1168 | * Contructor/Destructor
|
---|
1169 | */
|
---|
1170 | /**
|
---|
1171 | * Destruct a driver instance.
|
---|
1172 | *
|
---|
1173 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
1174 | * resources can be freed correctly.
|
---|
1175 | *
|
---|
1176 | * @param pDrvIns The driver instance data.
|
---|
1177 | */
|
---|
1178 | static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
|
---|
1179 | {
|
---|
1180 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
1181 | LogFlow(("drvNATDestruct:\n"));
|
---|
1182 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
1183 |
|
---|
1184 | if (pThis->pNATState)
|
---|
1185 | {
|
---|
1186 | slirp_cleanup(pThis->pNATState->pSlirp);
|
---|
1187 | #ifdef VBOX_WITH_STATISTICS
|
---|
1188 | # define DRV_PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
|
---|
1189 | # define DRV_COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
|
---|
1190 | # include "slirp/counters.h"
|
---|
1191 | #endif
|
---|
1192 | pThis->pNATState = NULL;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | RTReqQueueDestroy(pThis->hSlirpReqQueue);
|
---|
1196 | pThis->hSlirpReqQueue = NIL_RTREQQUEUE;
|
---|
1197 |
|
---|
1198 | RTReqQueueDestroy(pThis->hRecvReqQueue);
|
---|
1199 | pThis->hRecvReqQueue = NIL_RTREQQUEUE;
|
---|
1200 |
|
---|
1201 | RTSemEventDestroy(pThis->EventRecv);
|
---|
1202 | pThis->EventRecv = NIL_RTSEMEVENT;
|
---|
1203 |
|
---|
1204 | if (RTCritSectIsInitialized(&pThis->DevAccessLock))
|
---|
1205 | RTCritSectDelete(&pThis->DevAccessLock);
|
---|
1206 |
|
---|
1207 | if (RTCritSectIsInitialized(&pThis->XmitLock))
|
---|
1208 | RTCritSectDelete(&pThis->XmitLock);
|
---|
1209 |
|
---|
1210 | #ifndef RT_OS_WINDOWS
|
---|
1211 | RTPipeClose(pThis->hPipeRead);
|
---|
1212 | RTPipeClose(pThis->hPipeWrite);
|
---|
1213 | #endif
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | /**
|
---|
1217 | * Construct a NAT network transport driver instance.
|
---|
1218 | *
|
---|
1219 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
1220 | */
|
---|
1221 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
1222 | {
|
---|
1223 | int rc = 0;
|
---|
1224 |
|
---|
1225 | /* Construct PDRVNAT */
|
---|
1226 |
|
---|
1227 | RT_NOREF(fFlags);
|
---|
1228 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
1229 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
1230 |
|
---|
1231 | /*
|
---|
1232 | * Init the static parts.
|
---|
1233 | */
|
---|
1234 | pThis->pDrvIns = pDrvIns;
|
---|
1235 | pThis->pNATState = (SlirpState *)RTMemAlloc(sizeof(SlirpState));
|
---|
1236 | if(pThis->pNATState == NULL)
|
---|
1237 | return VERR_NO_MEMORY;
|
---|
1238 | else
|
---|
1239 | {
|
---|
1240 | pThis->pNATState->nsock = 0;
|
---|
1241 | pThis->pNATState->pTimerHead = NULL;
|
---|
1242 | pThis->pNATState->polls = (struct pollfd *)RTMemAlloc(64 * sizeof(struct pollfd));
|
---|
1243 | pThis->pNATState->uPollCap = 64;
|
---|
1244 | }
|
---|
1245 | pThis->hSlirpReqQueue = NIL_RTREQQUEUE;
|
---|
1246 | pThis->EventRecv = NIL_RTSEMEVENT;
|
---|
1247 |
|
---|
1248 | /* IBase */
|
---|
1249 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
1250 |
|
---|
1251 | /* INetwork */
|
---|
1252 | pThis->INetworkUp.pfnBeginXmit = drvNATNetworkUp_BeginXmit;
|
---|
1253 | pThis->INetworkUp.pfnAllocBuf = drvNATNetworkUp_AllocBuf;
|
---|
1254 | pThis->INetworkUp.pfnFreeBuf = drvNATNetworkUp_FreeBuf;
|
---|
1255 | pThis->INetworkUp.pfnSendBuf = drvNATNetworkUp_SendBuf;
|
---|
1256 | pThis->INetworkUp.pfnEndXmit = drvNATNetworkUp_EndXmit;
|
---|
1257 | pThis->INetworkUp.pfnSetPromiscuousMode = drvNATNetworkUp_SetPromiscuousMode;
|
---|
1258 | pThis->INetworkUp.pfnNotifyLinkChanged = drvNATNetworkUp_NotifyLinkChanged;
|
---|
1259 |
|
---|
1260 | /* NAT engine configuration */
|
---|
1261 | pThis->INetworkNATCfg.pfnRedirectRuleCommand = drvNATNetworkNatConfigRedirect;
|
---|
1262 | pThis->INetworkNATCfg.pfnNotifyDnsChanged = NULL;
|
---|
1263 |
|
---|
1264 | /*
|
---|
1265 | * Validate the config.
|
---|
1266 | */
|
---|
1267 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns,
|
---|
1268 | "PassDomain"
|
---|
1269 | "|TFTPPrefix"
|
---|
1270 | "|BootFile"
|
---|
1271 | "|Network"
|
---|
1272 | "|NextServer"
|
---|
1273 | "|DNSProxy"
|
---|
1274 | "|BindIP"
|
---|
1275 | "|UseHostResolver"
|
---|
1276 | "|SlirpMTU"
|
---|
1277 | "|AliasMode"
|
---|
1278 | "|SockRcv"
|
---|
1279 | "|SockSnd"
|
---|
1280 | "|TcpRcv"
|
---|
1281 | "|TcpSnd"
|
---|
1282 | "|ICMPCacheLimit"
|
---|
1283 | "|SoMaxConnection"
|
---|
1284 | "|LocalhostReachable"
|
---|
1285 | "|HostResolverMappings"
|
---|
1286 | , "PortForwarding");
|
---|
1287 |
|
---|
1288 | /*
|
---|
1289 | * Get the configuration settings.
|
---|
1290 | */
|
---|
1291 | bool fPassDomain = true;
|
---|
1292 | GET_BOOL(rc, pDrvIns, pCfg, "PassDomain", fPassDomain);
|
---|
1293 |
|
---|
1294 | GET_STRING_ALLOC(rc, pDrvIns, pCfg, "TFTPPrefix", pThis->pszTFTPPrefix);
|
---|
1295 | GET_STRING_ALLOC(rc, pDrvIns, pCfg, "BootFile", pThis->pszBootFile);
|
---|
1296 | GET_STRING_ALLOC(rc, pDrvIns, pCfg, "NextServer", pThis->pszNextServer);
|
---|
1297 |
|
---|
1298 | int fDNSProxy = 0;
|
---|
1299 | GET_S32(rc, pDrvIns, pCfg, "DNSProxy", fDNSProxy);
|
---|
1300 | int MTU = 1500;
|
---|
1301 | GET_S32(rc, pDrvIns, pCfg, "SlirpMTU", MTU);
|
---|
1302 | int i32AliasMode = 0;
|
---|
1303 | int i32MainAliasMode = 0;
|
---|
1304 | GET_S32(rc, pDrvIns, pCfg, "AliasMode", i32MainAliasMode);
|
---|
1305 | int iIcmpCacheLimit = 100;
|
---|
1306 | GET_S32(rc, pDrvIns, pCfg, "ICMPCacheLimit", iIcmpCacheLimit);
|
---|
1307 | bool fLocalhostReachable = false;
|
---|
1308 | GET_BOOL(rc, pDrvIns, pCfg, "LocalhostReachable", fLocalhostReachable);
|
---|
1309 |
|
---|
1310 | i32AliasMode |= (i32MainAliasMode & 0x1 ? 0x1 : 0);
|
---|
1311 | i32AliasMode |= (i32MainAliasMode & 0x2 ? 0x40 : 0);
|
---|
1312 | i32AliasMode |= (i32MainAliasMode & 0x4 ? 0x4 : 0);
|
---|
1313 | int i32SoMaxConn = 10;
|
---|
1314 | GET_S32(rc, pDrvIns, pCfg, "SoMaxConnection", i32SoMaxConn);
|
---|
1315 | /*
|
---|
1316 | * Query the network port interface.
|
---|
1317 | */
|
---|
1318 | pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
|
---|
1319 | if (!pThis->pIAboveNet)
|
---|
1320 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
1321 | N_("Configuration error: the above device/driver didn't "
|
---|
1322 | "export the network port interface"));
|
---|
1323 | pThis->pIAboveConfig = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKCONFIG);
|
---|
1324 | if (!pThis->pIAboveConfig)
|
---|
1325 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
1326 | N_("Configuration error: the above device/driver didn't "
|
---|
1327 | "export the network config interface"));
|
---|
1328 |
|
---|
1329 | /* Generate a network address for this network card. */
|
---|
1330 | char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
|
---|
1331 | GET_STRING(rc, pDrvIns, pCfg, "Network", szNetwork[0], sizeof(szNetwork));
|
---|
1332 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1333 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT%d: Configuration error: missing network"),
|
---|
1334 | pDrvIns->iInstance);
|
---|
1335 |
|
---|
1336 | RTNETADDRIPV4 Network, Netmask;
|
---|
1337 |
|
---|
1338 | rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
|
---|
1339 | if (RT_FAILURE(rc))
|
---|
1340 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
1341 | N_("NAT#%d: Configuration error: network '%s' describes not a valid IPv4 network"),
|
---|
1342 | pDrvIns->iInstance, szNetwork);
|
---|
1343 |
|
---|
1344 | /* Construct Libslirp Config and Initialzie Slirp */
|
---|
1345 |
|
---|
1346 | LogFlow(("Here is what is coming out of the vbox config:\n \
|
---|
1347 | Network: %lu\n \
|
---|
1348 | Netmask: %lu\n", Network, Netmask));
|
---|
1349 |
|
---|
1350 | #ifndef RT_OS_WINDOWS
|
---|
1351 | struct in_addr vnetwork = RTNetIPv4AddrHEToInAddr(&Network);
|
---|
1352 | struct in_addr vnetmask = RTNetIPv4AddrHEToInAddr(&Netmask);
|
---|
1353 | struct in_addr vhost = RTNetInAddrFromU8(10, 0, 2, 2);
|
---|
1354 | struct in_addr vdhcp_start = RTNetInAddrFromU8(10, 0, 2, 15);
|
---|
1355 | struct in_addr vnameserver = RTNetInAddrFromU8(10, 0, 2, 3);
|
---|
1356 | #else
|
---|
1357 | struct in_addr vnetwork;
|
---|
1358 | vnetwork.S_un.S_addr = RT_BSWAP_U32(Network.u);
|
---|
1359 |
|
---|
1360 | struct in_addr vnetmask;
|
---|
1361 | vnetmask.S_un.S_addr = RT_BSWAP_U32(Netmask.u);
|
---|
1362 |
|
---|
1363 | struct in_addr vhost;
|
---|
1364 | vhost.S_un.S_addr = RT_BSWAP_U32(0x0a000202);
|
---|
1365 |
|
---|
1366 | struct in_addr vdhcp_start;
|
---|
1367 | vdhcp_start.S_un.S_addr = RT_BSWAP_U32(0x0a00020f);
|
---|
1368 |
|
---|
1369 | struct in_addr vnameserver;
|
---|
1370 | vnameserver.S_un.S_addr = RT_BSWAP_U32(0x0a000203);
|
---|
1371 | #endif
|
---|
1372 |
|
---|
1373 | SlirpConfig *pSlirpCfg = new SlirpConfig { 0 };
|
---|
1374 |
|
---|
1375 | pSlirpCfg->version = 4;
|
---|
1376 | pSlirpCfg->restricted = false;
|
---|
1377 | pSlirpCfg->in_enabled = true;
|
---|
1378 | pSlirpCfg->vnetwork = vnetwork;
|
---|
1379 | pSlirpCfg->vnetmask = vnetmask;
|
---|
1380 | pSlirpCfg->vhost = vhost;
|
---|
1381 | pSlirpCfg->in6_enabled = true;
|
---|
1382 |
|
---|
1383 | inet_pton(AF_INET6, "fd00::", &pSlirpCfg->vprefix_addr6);
|
---|
1384 | pSlirpCfg->vprefix_len = 64;
|
---|
1385 | inet_pton(AF_INET6, "fd00::2", &pSlirpCfg->vhost6);
|
---|
1386 |
|
---|
1387 | pSlirpCfg->vhostname = "vbox";
|
---|
1388 | pSlirpCfg->tftp_server_name = pThis->pszNextServer;
|
---|
1389 | pSlirpCfg->tftp_path = pThis->pszTFTPPrefix;
|
---|
1390 | pSlirpCfg->bootfile = pThis->pszBootFile;
|
---|
1391 | pSlirpCfg->vdhcp_start = vdhcp_start;
|
---|
1392 | pSlirpCfg->vnameserver = vnameserver;
|
---|
1393 | pSlirpCfg->if_mtu = MTU;
|
---|
1394 |
|
---|
1395 | inet_pton(AF_INET6, "fd00::3", &pSlirpCfg->vnameserver6);
|
---|
1396 |
|
---|
1397 | pSlirpCfg->vdnssearch = NULL;
|
---|
1398 | pSlirpCfg->vdomainname = NULL;
|
---|
1399 |
|
---|
1400 | SlirpCb *slirpCallbacks = (struct SlirpCb *)RTMemAlloc(sizeof(SlirpCb));
|
---|
1401 |
|
---|
1402 | slirpCallbacks->send_packet = &drvNAT_SendPacketCb;
|
---|
1403 | slirpCallbacks->guest_error = &drvNAT_GuestErrorCb;
|
---|
1404 | slirpCallbacks->clock_get_ns = &drvNAT_ClockGetNsCb;
|
---|
1405 | slirpCallbacks->timer_new = &drvNAT_TimerNewCb;
|
---|
1406 | slirpCallbacks->timer_free = &drvNAT_TimerFreeCb;
|
---|
1407 | slirpCallbacks->timer_mod = &drvNAT_TimerModCb;
|
---|
1408 | slirpCallbacks->register_poll_fd = &drvNAT_RegisterPoll;
|
---|
1409 | slirpCallbacks->unregister_poll_fd = &drvNAT_UnregisterPoll;
|
---|
1410 | slirpCallbacks->notify = &drvNAT_NotifyCb;
|
---|
1411 | slirpCallbacks->init_completed = NULL;
|
---|
1412 | slirpCallbacks->timer_new_opaque = NULL;
|
---|
1413 |
|
---|
1414 | Slirp *pSlirp = slirp_new(/* cfg */ pSlirpCfg, /* callbacks */ slirpCallbacks, /* opaque */ pThis);
|
---|
1415 |
|
---|
1416 | if (pSlirp == NULL)
|
---|
1417 | return VERR_INVALID_POINTER;
|
---|
1418 |
|
---|
1419 | pThis->pNATState->pSlirp = pSlirp;
|
---|
1420 |
|
---|
1421 | rc = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfg, &Network);
|
---|
1422 | AssertLogRelRCReturn(rc, rc);
|
---|
1423 |
|
---|
1424 | rc = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, NULL);
|
---|
1425 | AssertLogRelRCReturn(rc, rc);
|
---|
1426 |
|
---|
1427 | rc = RTReqQueueCreate(&pThis->hSlirpReqQueue);
|
---|
1428 | AssertLogRelRCReturn(rc, rc);
|
---|
1429 |
|
---|
1430 | rc = RTReqQueueCreate(&pThis->hRecvReqQueue);
|
---|
1431 | AssertLogRelRCReturn(rc, rc);
|
---|
1432 |
|
---|
1433 | rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pRecvThread, pThis, drvNATRecv,
|
---|
1434 | drvNATRecvWakeup, 256 * _1K, RTTHREADTYPE_IO, "NATRX");
|
---|
1435 | AssertRCReturn(rc, rc);
|
---|
1436 |
|
---|
1437 | rc = RTSemEventCreate(&pThis->EventRecv);
|
---|
1438 | AssertRCReturn(rc, rc);
|
---|
1439 |
|
---|
1440 | rc = RTCritSectInit(&pThis->DevAccessLock);
|
---|
1441 | AssertRCReturn(rc, rc);
|
---|
1442 |
|
---|
1443 | rc = RTCritSectInit(&pThis->XmitLock);
|
---|
1444 | AssertRCReturn(rc, rc);
|
---|
1445 |
|
---|
1446 | char szTmp[128];
|
---|
1447 | RTStrPrintf(szTmp, sizeof(szTmp), "nat%d", pDrvIns->iInstance);
|
---|
1448 | PDMDrvHlpDBGFInfoRegister(pDrvIns, szTmp, "NAT info.", drvNATInfo);
|
---|
1449 |
|
---|
1450 | #ifdef VBOX_WITH_STATISTICS
|
---|
1451 | # define DRV_PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
|
---|
1452 | # define DRV_COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
|
---|
1453 | # include "slirp/counters.h"
|
---|
1454 | #endif
|
---|
1455 |
|
---|
1456 | #ifndef RT_OS_WINDOWS
|
---|
1457 | /**
|
---|
1458 | * Create the control pipe.
|
---|
1459 | */
|
---|
1460 | rc = RTPipeCreate(&pThis->hPipeRead, &pThis->hPipeWrite, 0 /*fFlags*/);
|
---|
1461 | AssertRCReturn(rc, rc);
|
---|
1462 | #endif
|
---|
1463 |
|
---|
1464 | rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pSlirpThread, pThis, drvNATAsyncIoThread,
|
---|
1465 | drvNATAsyncIoWakeup, 256 * _1K, RTTHREADTYPE_IO, "NAT");
|
---|
1466 | AssertRCReturn(rc, rc);
|
---|
1467 |
|
---|
1468 | pThis->enmLinkState = pThis->enmLinkStateWant = PDMNETWORKLINKSTATE_UP;
|
---|
1469 |
|
---|
1470 | return rc;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | /**
|
---|
1474 | * NAT network transport driver registration record.
|
---|
1475 | */
|
---|
1476 | const PDMDRVREG g_DrvNATlibslirp =
|
---|
1477 | {
|
---|
1478 | /* u32Version */
|
---|
1479 | PDM_DRVREG_VERSION,
|
---|
1480 | /* szName */
|
---|
1481 | "NAT",
|
---|
1482 | /* szRCMod */
|
---|
1483 | "",
|
---|
1484 | /* szR0Mod */
|
---|
1485 | "",
|
---|
1486 | /* pszDescription */
|
---|
1487 | "NATlibslrip Network Transport Driver",
|
---|
1488 | /* fFlags */
|
---|
1489 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
1490 | /* fClass. */
|
---|
1491 | PDM_DRVREG_CLASS_NETWORK,
|
---|
1492 | /* cMaxInstances */
|
---|
1493 | ~0U,
|
---|
1494 | /* cbInstance */
|
---|
1495 | sizeof(DRVNAT),
|
---|
1496 | /* pfnConstruct */
|
---|
1497 | drvNATConstruct,
|
---|
1498 | /* pfnDestruct */
|
---|
1499 | drvNATDestruct,
|
---|
1500 | /* pfnRelocate */
|
---|
1501 | NULL,
|
---|
1502 | /* pfnIOCtl */
|
---|
1503 | NULL,
|
---|
1504 | /* pfnPowerOn */
|
---|
1505 | NULL,
|
---|
1506 | /* pfnReset */
|
---|
1507 | NULL,
|
---|
1508 | /* pfnSuspend */
|
---|
1509 | NULL,
|
---|
1510 | /* pfnResume */
|
---|
1511 | NULL,
|
---|
1512 | /* pfnAttach */
|
---|
1513 | NULL,
|
---|
1514 | /* pfnDetach */
|
---|
1515 | NULL,
|
---|
1516 | /* pfnPowerOff */
|
---|
1517 | NULL,
|
---|
1518 | /* pfnSoftReset */
|
---|
1519 | NULL,
|
---|
1520 | /* u32EndVersion */
|
---|
1521 | PDM_DRVREG_VERSION
|
---|
1522 | };
|
---|