1 | /* $Id: VBoxNetNAT.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetNAT - NAT Service for connecting to IntNet.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /** @page pg_net_nat VBoxNetNAT
|
---|
19 | *
|
---|
20 | * Write a few words...
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #include <iprt/net.h>
|
---|
28 | #include <iprt/initterm.h>
|
---|
29 | #include <iprt/alloca.h>
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/time.h>
|
---|
32 | #include <iprt/timer.h>
|
---|
33 | #include <iprt/thread.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/getopt.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/req.h>
|
---|
41 | #include <iprt/file.h>
|
---|
42 | #include <iprt/semaphore.h>
|
---|
43 | #define LOG_GROUP LOG_GROUP_NAT_SERVICE
|
---|
44 | #include <VBox/log.h>
|
---|
45 |
|
---|
46 | #include <VBox/sup.h>
|
---|
47 | #include <VBox/intnet.h>
|
---|
48 | #include <VBox/intnetinline.h>
|
---|
49 | #include <VBox/pdmnetinline.h>
|
---|
50 | #include <VBox/vmm.h>
|
---|
51 | #include <VBox/version.h>
|
---|
52 |
|
---|
53 | #include <vector>
|
---|
54 | #include <string>
|
---|
55 |
|
---|
56 | #include "../NetLib/VBoxNetLib.h"
|
---|
57 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
58 | #include <libslirp.h>
|
---|
59 |
|
---|
60 | #ifdef RT_OS_WINDOWS /* WinMain */
|
---|
61 | # include <Windows.h>
|
---|
62 | # include <stdlib.h>
|
---|
63 | #else
|
---|
64 | # include <errno.h>
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | /*******************************************************************************
|
---|
70 | * Structures and Typedefs *
|
---|
71 | *******************************************************************************/
|
---|
72 |
|
---|
73 | class VBoxNetNAT : public VBoxNetBaseService
|
---|
74 | {
|
---|
75 | public:
|
---|
76 | VBoxNetNAT();
|
---|
77 | virtual ~VBoxNetNAT();
|
---|
78 | void usage(void);
|
---|
79 | void run(void);
|
---|
80 | void init(void);
|
---|
81 |
|
---|
82 | public:
|
---|
83 | PNATState m_pNATState;
|
---|
84 | RTNETADDRIPV4 m_Ipv4Netmask;
|
---|
85 | bool m_fPassDomain;
|
---|
86 | RTTHREAD m_ThrNAT;
|
---|
87 | RTTHREAD m_ThrSndNAT;
|
---|
88 | RTTHREAD m_ThrUrgSndNAT;
|
---|
89 | #ifdef RT_OS_WINDOWS
|
---|
90 | HANDLE m_hWakeupEvent;
|
---|
91 | #else
|
---|
92 | RTFILE m_PipeWrite;
|
---|
93 | RTFILE m_PipeRead;
|
---|
94 | #endif
|
---|
95 | /** Queue for NAT-thread-external events. */
|
---|
96 | /** event to wakeup the guest receive thread */
|
---|
97 | RTSEMEVENT m_EventSend;
|
---|
98 | /** event to wakeup the guest urgent receive thread */
|
---|
99 | RTSEMEVENT m_EventUrgSend;
|
---|
100 |
|
---|
101 | PRTREQQUEUE m_pReqQueue;
|
---|
102 | PRTREQQUEUE m_pSendQueue;
|
---|
103 | PRTREQQUEUE m_pUrgSendQueue;
|
---|
104 | volatile uint32_t cUrgPkt;
|
---|
105 | volatile uint32_t cPkt;
|
---|
106 | bool fIsRunning;
|
---|
107 | };
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | /*******************************************************************************
|
---|
112 | * Global Variables *
|
---|
113 | *******************************************************************************/
|
---|
114 | /** Pointer to the NAT server. */
|
---|
115 | class VBoxNetNAT *g_pNAT;
|
---|
116 | static DECLCALLBACK(int) AsyncIoThread(RTTHREAD pThread, void *pvUser);
|
---|
117 | static DECLCALLBACK(int) natSndThread(RTTHREAD pThread, void *pvUser);
|
---|
118 | static DECLCALLBACK(int) natUrgSndThread(RTTHREAD pThread, void *pvUser);
|
---|
119 | static void SendWorker(struct mbuf *m, size_t cb);
|
---|
120 | static void IntNetSendWorker(bool urg, void *pvFrame, size_t cbFrame, struct mbuf *m);
|
---|
121 |
|
---|
122 |
|
---|
123 | static void natNotifyNATThread(void)
|
---|
124 | {
|
---|
125 | int rc;
|
---|
126 | #ifndef RT_OS_WINDOWS
|
---|
127 | /* kick select() */
|
---|
128 | rc = RTFileWrite(g_pNAT->m_PipeWrite, "", 1, NULL);
|
---|
129 | #else
|
---|
130 | /* kick WSAWaitForMultipleEvents */
|
---|
131 | rc = WSASetEvent(g_pNAT->hWakeupEvent);
|
---|
132 | #endif
|
---|
133 | AssertRC(rc);
|
---|
134 | }
|
---|
135 |
|
---|
136 | VBoxNetNAT::VBoxNetNAT()
|
---|
137 | {
|
---|
138 | #if defined(RT_OS_WINDOWS)
|
---|
139 | /*@todo check if we can remove this*/
|
---|
140 | VBoxNetBaseService();
|
---|
141 | #endif
|
---|
142 | m_enmTrunkType = kIntNetTrunkType_WhateverNone;
|
---|
143 | m_TrunkName = "";
|
---|
144 | m_MacAddress.au8[0] = 0x08;
|
---|
145 | m_MacAddress.au8[1] = 0x00;
|
---|
146 | m_MacAddress.au8[2] = 0x27;
|
---|
147 | m_MacAddress.au8[3] = 0x40;
|
---|
148 | m_MacAddress.au8[4] = 0x41;
|
---|
149 | m_MacAddress.au8[5] = 0x42;
|
---|
150 | m_Ipv4Address.u = RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8( 10, 0, 2, 1)));
|
---|
151 | m_Ipv4Netmask.u = 0xffff0000;
|
---|
152 | cPkt = 0;
|
---|
153 | cUrgPkt = 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 | VBoxNetNAT::~VBoxNetNAT() { }
|
---|
157 | void VBoxNetNAT::init()
|
---|
158 | {
|
---|
159 | int rc;
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Initialize slirp.
|
---|
163 | */
|
---|
164 | rc = slirp_init(&m_pNATState, m_Ipv4Address.u, m_Ipv4Netmask.u, m_fPassDomain, false, 0, this);
|
---|
165 | AssertReleaseRC(rc);
|
---|
166 |
|
---|
167 | slirp_set_ethaddr_and_activate_port_forwarding(m_pNATState, &m_MacAddress.au8[0], INADDR_ANY);
|
---|
168 | #ifndef RT_OS_WINDOWS
|
---|
169 | /*
|
---|
170 | * Create the control pipe.
|
---|
171 | */
|
---|
172 | int fds[2];
|
---|
173 | if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
|
---|
174 | {
|
---|
175 | rc = RTErrConvertFromErrno(errno);
|
---|
176 | AssertReleaseRC(rc);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 | m_PipeRead = fds[0];
|
---|
180 | m_PipeWrite = fds[1];
|
---|
181 | #else
|
---|
182 | m_hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
|
---|
183 | slirp_register_external_event(m_pNATState, m_hWakeupEvent, VBOX_WAKEUP_EVENT_INDEX);
|
---|
184 | #endif
|
---|
185 | rc = RTReqCreateQueue(&m_pReqQueue);
|
---|
186 | AssertReleaseRC(rc);
|
---|
187 |
|
---|
188 | rc = RTReqCreateQueue(&m_pSendQueue);
|
---|
189 | AssertReleaseRC(rc);
|
---|
190 |
|
---|
191 | rc = RTReqCreateQueue(&m_pUrgSendQueue);
|
---|
192 | AssertReleaseRC(rc);
|
---|
193 |
|
---|
194 | rc = RTThreadCreate(&m_ThrNAT, AsyncIoThread, this, 128 * _1K, RTTHREADTYPE_DEFAULT, 0, "NAT");
|
---|
195 | rc = RTThreadCreate(&m_ThrSndNAT, natSndThread, this, 128 * _1K, RTTHREADTYPE_DEFAULT, 0, "SndNAT");
|
---|
196 | rc = RTThreadCreate(&m_ThrUrgSndNAT, natUrgSndThread, this, 128 * _1K, RTTHREADTYPE_DEFAULT, 0, "UrgSndNAT");
|
---|
197 | rc = RTSemEventCreate(&m_EventSend);
|
---|
198 | rc = RTSemEventCreate(&m_EventUrgSend);
|
---|
199 | AssertReleaseRC(rc);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* Mandatory functions */
|
---|
203 | void VBoxNetNAT::run()
|
---|
204 | {
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * The loop.
|
---|
208 | */
|
---|
209 | fIsRunning = true;
|
---|
210 | PINTNETRINGBUF pRingBuf = &m_pIfBuf->Recv;
|
---|
211 | //RTThreadSetType(RTThreadSelf(), RTTHREADTYPE_IO);
|
---|
212 | for (;;)
|
---|
213 | {
|
---|
214 | /*
|
---|
215 | * Wait for a packet to become available.
|
---|
216 | */
|
---|
217 | INTNETIFWAITREQ WaitReq;
|
---|
218 | WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
219 | WaitReq.Hdr.cbReq = sizeof(WaitReq);
|
---|
220 | WaitReq.pSession = m_pSession;
|
---|
221 | WaitReq.hIf = m_hIf;
|
---|
222 | WaitReq.cMillies = 2000; /* 2 secs - the sleep is for some reason uninterruptible... */ /** @todo fix interruptability in SrvIntNet! */
|
---|
223 | #if 1
|
---|
224 | RTReqProcess(m_pSendQueue, 0);
|
---|
225 | RTReqProcess(m_pUrgSendQueue, 0);
|
---|
226 | #endif
|
---|
227 | int rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_WAIT, 0, &WaitReq.Hdr);
|
---|
228 | if (RT_FAILURE(rc))
|
---|
229 | {
|
---|
230 | if (rc == VERR_TIMEOUT)
|
---|
231 | continue;
|
---|
232 | LogRel(("VBoxNetNAT: VMMR0_DO_INTNET_IF_WAIT returned %Rrc\n", rc));
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Process the receive buffer.
|
---|
238 | */
|
---|
239 | PCINTNETHDR pHdr;
|
---|
240 | while ((pHdr = IntNetRingGetNextFrameToRead(pRingBuf)) != NULL)
|
---|
241 | {
|
---|
242 | uint16_t const u16Type = pHdr->u16Type;
|
---|
243 | if (RT_LIKELY( u16Type == INTNETHDR_TYPE_FRAME
|
---|
244 | || u16Type == INTNETHDR_TYPE_GSO))
|
---|
245 | {
|
---|
246 | size_t cbFrame = pHdr->cbFrame;
|
---|
247 | size_t cbIgnored;
|
---|
248 | void *pvSlirpFrame;
|
---|
249 | struct mbuf *m;
|
---|
250 | if (u16Type == INTNETHDR_TYPE_FRAME)
|
---|
251 | {
|
---|
252 | m = slirp_ext_m_get(g_pNAT->m_pNATState, cbFrame, &pvSlirpFrame, &cbIgnored);
|
---|
253 | if (!m)
|
---|
254 | {
|
---|
255 | LogRel(("NAT: Can't allocate send buffer cbFrame=%u\n", cbFrame));
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | memcpy(pvSlirpFrame, IntNetHdrGetFramePtr(pHdr, m_pIfBuf), cbFrame);
|
---|
259 | IntNetRingSkipFrame(&m_pIfBuf->Recv);
|
---|
260 |
|
---|
261 | /* don't wait, we may have to wakeup the NAT thread first */
|
---|
262 | rc = RTReqCallEx(m_pReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
263 | (PFNRT)SendWorker, 2, m, cbFrame);
|
---|
264 | AssertReleaseRC(rc);
|
---|
265 | }
|
---|
266 | else
|
---|
267 | {
|
---|
268 | /** @todo pass these unmodified. */
|
---|
269 | PCPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr, m_pIfBuf);
|
---|
270 | if (!PDMNetGsoIsValid(pGso, cbFrame, cbFrame - sizeof(*pGso)))
|
---|
271 | {
|
---|
272 | IntNetRingSkipFrame(&m_pIfBuf->Recv);
|
---|
273 | STAM_REL_COUNTER_INC(&m_pIfBuf->cStatBadFrames);
|
---|
274 | continue;
|
---|
275 | }
|
---|
276 |
|
---|
277 | uint8_t abHdrScratch[256];
|
---|
278 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, cbFrame - sizeof(*pGso));
|
---|
279 | for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
280 | {
|
---|
281 | uint32_t cbSegFrame;
|
---|
282 | void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)(pGso + 1), cbFrame, abHdrScratch,
|
---|
283 | iSeg, cSegs, &cbSegFrame);
|
---|
284 | m = slirp_ext_m_get(g_pNAT->m_pNATState, cbFrame, &pvSlirpFrame, &cbIgnored);
|
---|
285 | if (!m)
|
---|
286 | {
|
---|
287 | LogRel(("NAT: Can't allocate send buffer cbSegFrame=%u seg=%u/%u\n", cbSegFrame, iSeg, cSegs));
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | memcpy(pvSlirpFrame, pvSegFrame, cbFrame);
|
---|
291 |
|
---|
292 | rc = RTReqCallEx(m_pReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
293 | (PFNRT)SendWorker, 2, m, cbSegFrame);
|
---|
294 | AssertReleaseRC(rc);
|
---|
295 | }
|
---|
296 | IntNetRingSkipFrame(&m_pIfBuf->Recv);
|
---|
297 | }
|
---|
298 |
|
---|
299 | #ifndef RT_OS_WINDOWS
|
---|
300 | /* kick select() */
|
---|
301 | rc = RTFileWrite(m_PipeWrite, "", 1, NULL);
|
---|
302 | AssertRC(rc);
|
---|
303 | #else
|
---|
304 | /* kick WSAWaitForMultipleEvents */
|
---|
305 | rc = WSASetEvent(m_hWakeupEvent);
|
---|
306 | AssertRelease(rc == TRUE);
|
---|
307 | #endif
|
---|
308 | }
|
---|
309 | else if (u16Type == INTNETHDR_TYPE_PADDING)
|
---|
310 | IntNetRingSkipFrame(&m_pIfBuf->Recv);
|
---|
311 | else
|
---|
312 | {
|
---|
313 | IntNetRingSkipFrame(&m_pIfBuf->Recv);
|
---|
314 | STAM_REL_COUNTER_INC(&m_pIfBuf->cStatBadFrames);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | }
|
---|
319 | fIsRunning = false;
|
---|
320 | }
|
---|
321 |
|
---|
322 | void VBoxNetNAT::usage()
|
---|
323 | {
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Entry point.
|
---|
328 | */
|
---|
329 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
330 | {
|
---|
331 | Log2(("NAT: main\n"));
|
---|
332 | g_pNAT = new VBoxNetNAT();
|
---|
333 | Log2(("NAT: parsing command line\n"));
|
---|
334 | int rc = g_pNAT->parseArgs(argc - 1, argv + 1);
|
---|
335 | if (!rc)
|
---|
336 | {
|
---|
337 | Log2(("NAT: initialization\n"));
|
---|
338 | g_pNAT->init();
|
---|
339 | Log2(("NAT: try go online\n"));
|
---|
340 | g_pNAT->tryGoOnline();
|
---|
341 | Log2(("NAT: main loop\n"));
|
---|
342 | g_pNAT->run();
|
---|
343 | }
|
---|
344 | delete g_pNAT;
|
---|
345 | return 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /** slirp's hooks */
|
---|
349 | extern "C" int slirp_can_output(void * pvUser)
|
---|
350 | {
|
---|
351 | return 1;
|
---|
352 | }
|
---|
353 |
|
---|
354 | extern "C" void slirp_urg_output(void *pvUser, struct mbuf *m, const uint8_t *pu8Buf, int cb)
|
---|
355 | {
|
---|
356 | int rc = RTReqCallEx(g_pNAT->m_pUrgSendQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
357 | (PFNRT)IntNetSendWorker, 4, (uintptr_t)1, (uintptr_t)pu8Buf, (uintptr_t)cb, (uintptr_t)m);
|
---|
358 | ASMAtomicIncU32(&g_pNAT->cUrgPkt);
|
---|
359 | RTSemEventSignal(g_pNAT->m_EventUrgSend);
|
---|
360 | AssertReleaseRC(rc);
|
---|
361 | }
|
---|
362 | extern "C" void slirp_output(void *pvUser, struct mbuf *m, const uint8_t *pu8Buf, int cb)
|
---|
363 | {
|
---|
364 | AssertRelease(g_pNAT == pvUser);
|
---|
365 | int rc = RTReqCallEx(g_pNAT->m_pSendQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
366 | (PFNRT)IntNetSendWorker, 4, (uintptr_t)0, (uintptr_t)pu8Buf, (uintptr_t)cb, (uintptr_t)m);
|
---|
367 | ASMAtomicIncU32(&g_pNAT->cPkt);
|
---|
368 | RTSemEventSignal(g_pNAT->m_EventSend);
|
---|
369 | AssertReleaseRC(rc);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Worker function for drvNATSend().
|
---|
374 | * @thread "NAT" thread.
|
---|
375 | */
|
---|
376 | static void SendWorker(struct mbuf *m, size_t cb)
|
---|
377 | {
|
---|
378 | slirp_input(g_pNAT->m_pNATState, m, cb);
|
---|
379 | }
|
---|
380 |
|
---|
381 | static void IntNetSendWorker(bool urg, void *pvFrame, size_t cbFrame, struct mbuf *m)
|
---|
382 | {
|
---|
383 | Log2(("VBoxNetNAT: going to send some bytes ... \n"));
|
---|
384 | VBoxNetNAT *pThis = g_pNAT;
|
---|
385 | INTNETIFSENDREQ SendReq;
|
---|
386 | int rc;
|
---|
387 |
|
---|
388 | if (!urg)
|
---|
389 | {
|
---|
390 | while (ASMAtomicReadU32(&g_pNAT->cUrgPkt) != 0
|
---|
391 | || ASMAtomicReadU32(&g_pNAT->cPkt) == 0)
|
---|
392 | rc = RTSemEventWait(g_pNAT->m_EventSend, RT_INDEFINITE_WAIT);
|
---|
393 | }
|
---|
394 | else
|
---|
395 | {
|
---|
396 | while (ASMAtomicReadU32(&g_pNAT->cUrgPkt) == 0)
|
---|
397 | rc = RTSemEventWait(g_pNAT->m_EventUrgSend, RT_INDEFINITE_WAIT);
|
---|
398 | }
|
---|
399 | rc = IntNetRingWriteFrame(&pThis->m_pIfBuf->Send, pvFrame, cbFrame);
|
---|
400 | if (RT_FAILURE(rc))
|
---|
401 | {
|
---|
402 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
403 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
404 | SendReq.pSession = pThis->m_pSession;
|
---|
405 | SendReq.hIf = pThis->m_hIf;
|
---|
406 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
407 |
|
---|
408 | rc = IntNetRingWriteFrame(&pThis->m_pIfBuf->Send, pvFrame, cbFrame);
|
---|
409 |
|
---|
410 | }
|
---|
411 | if (RT_SUCCESS(rc))
|
---|
412 | {
|
---|
413 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
414 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
415 | SendReq.pSession = pThis->m_pSession;
|
---|
416 | SendReq.hIf = pThis->m_hIf;
|
---|
417 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
418 | }
|
---|
419 | if (RT_FAILURE(rc))
|
---|
420 | Log2(("VBoxNetNAT: Failed to send packet; rc=%Rrc\n", rc));
|
---|
421 |
|
---|
422 | if (!urg)
|
---|
423 | {
|
---|
424 | ASMAtomicDecU32(&g_pNAT->cPkt);
|
---|
425 | }
|
---|
426 | else {
|
---|
427 | if (ASMAtomicDecU32(&g_pNAT->cUrgPkt) == 0)
|
---|
428 | RTSemEventSignal(g_pNAT->m_EventSend);
|
---|
429 | }
|
---|
430 | natNotifyNATThread();
|
---|
431 | slirp_ext_m_free(pThis->m_pNATState, m, (uint8_t *)pvFrame);
|
---|
432 | }
|
---|
433 |
|
---|
434 | static DECLCALLBACK(int) AsyncIoThread(RTTHREAD pThread, void *pvUser)
|
---|
435 | {
|
---|
436 | VBoxNetNAT *pThis = (VBoxNetNAT *)pvUser;
|
---|
437 | int nFDs = -1;
|
---|
438 | #ifdef RT_OS_WINDOWS
|
---|
439 | HANDLE *pahEvents = slirp_get_events(pThis->m_pNATState);
|
---|
440 | #else /* RT_OS_WINDOWS */
|
---|
441 | unsigned int cPollNegRet = 0;
|
---|
442 | #endif /* !RT_OS_WINDOWS */
|
---|
443 |
|
---|
444 | LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
|
---|
445 |
|
---|
446 | /*
|
---|
447 | * Polling loop.
|
---|
448 | */
|
---|
449 | for(;;)
|
---|
450 | {
|
---|
451 | /*
|
---|
452 | * To prevent concurrent execution of sending/receiving threads
|
---|
453 | */
|
---|
454 | #ifndef RT_OS_WINDOWS
|
---|
455 | nFDs = slirp_get_nsock(pThis->m_pNATState);
|
---|
456 | /* allocation for all sockets + Management pipe */
|
---|
457 | struct pollfd *polls = (struct pollfd *)RTMemAlloc((1 + nFDs) * sizeof(struct pollfd) + sizeof(uint32_t));
|
---|
458 | if (polls == NULL)
|
---|
459 | return VERR_NO_MEMORY;
|
---|
460 |
|
---|
461 | /* don't pass the management pipe */
|
---|
462 | slirp_select_fill(pThis->m_pNATState, &nFDs, &polls[1]);
|
---|
463 | unsigned int cMsTimeout = slirp_get_timeout_ms(pThis->m_pNATState);
|
---|
464 |
|
---|
465 | polls[0].fd = pThis->m_PipeRead;
|
---|
466 | /* POLLRDBAND usually doesn't used on Linux but seems used on Solaris */
|
---|
467 | polls[0].events = POLLRDNORM|POLLPRI|POLLRDBAND;
|
---|
468 | polls[0].revents = 0;
|
---|
469 |
|
---|
470 | int cChangedFDs = poll(polls, nFDs + 1, cMsTimeout);
|
---|
471 | if (cChangedFDs < 0)
|
---|
472 | {
|
---|
473 | if (errno == EINTR)
|
---|
474 | {
|
---|
475 | Log2(("NAT: signal was caught while sleep on poll\n"));
|
---|
476 | /* No error, just process all outstanding requests but don't wait */
|
---|
477 | cChangedFDs = 0;
|
---|
478 | }
|
---|
479 | else if (cPollNegRet++ > 128)
|
---|
480 | {
|
---|
481 | LogRel(("NAT:Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
|
---|
482 | cPollNegRet = 0;
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | if (cChangedFDs >= 0)
|
---|
487 | {
|
---|
488 | slirp_select_poll(pThis->m_pNATState, &polls[1], nFDs);
|
---|
489 | if (polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
|
---|
490 | {
|
---|
491 | /* drain the pipe */
|
---|
492 | char ch[1];
|
---|
493 | size_t cbRead;
|
---|
494 | int counter = 0;
|
---|
495 | /*
|
---|
496 | * drvNATSend decoupled so we don't know how many times
|
---|
497 | * device's thread sends before we've entered multiplex,
|
---|
498 | * so to avoid false alarm drain pipe here to the very end
|
---|
499 | *
|
---|
500 | * @todo: Probably we should counter drvNATSend to count how
|
---|
501 | * deep pipe has been filed before drain.
|
---|
502 | *
|
---|
503 | * XXX:Make it reading exactly we need to drain the pipe.
|
---|
504 | */
|
---|
505 | RTFileRead(pThis->m_PipeRead, &ch, 1, &cbRead);
|
---|
506 | }
|
---|
507 | }
|
---|
508 | /* process _all_ outstanding requests but don't wait */
|
---|
509 | RTReqProcess(pThis->m_pReqQueue, 0);
|
---|
510 | RTMemFree(polls);
|
---|
511 |
|
---|
512 | #else /* RT_OS_WINDOWS */
|
---|
513 | nFDs = -1;
|
---|
514 | slirp_select_fill(pThis->m_pNATState, &nFDs);
|
---|
515 | DWORD dwEvent = WSAWaitForMultipleEvents(nFDs, pahEvents, FALSE,
|
---|
516 | slirp_get_timeout_ms(pThis->m_pNATState),
|
---|
517 | FALSE);
|
---|
518 | if ( (dwEvent < WSA_WAIT_EVENT_0 || dwEvent > WSA_WAIT_EVENT_0 + nFDs - 1)
|
---|
519 | && dwEvent != WSA_WAIT_TIMEOUT)
|
---|
520 | {
|
---|
521 | int error = WSAGetLastError();
|
---|
522 | LogRel(("NAT: WSAWaitForMultipleEvents returned %d (error %d)\n", dwEvent, error));
|
---|
523 | RTAssertReleasePanic();
|
---|
524 | }
|
---|
525 |
|
---|
526 | if (dwEvent == WSA_WAIT_TIMEOUT)
|
---|
527 | {
|
---|
528 | /* only check for slow/fast timers */
|
---|
529 | slirp_select_poll(pThis->m_pNATState, /* fTimeout=*/true, /*fIcmp=*/false);
|
---|
530 | continue;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /* poll the sockets in any case */
|
---|
534 | slirp_select_poll(pThis->m_pNATState, /* fTimeout=*/false, /* fIcmp=*/(dwEvent == WSA_WAIT_EVENT_0));
|
---|
535 | /* process _all_ outstanding requests but don't wait */
|
---|
536 | RTReqProcess(pThis->m_pReqQueue, 0);
|
---|
537 | #endif /* RT_OS_WINDOWS */
|
---|
538 | }
|
---|
539 |
|
---|
540 | return VINF_SUCCESS;
|
---|
541 | }
|
---|
542 |
|
---|
543 | static DECLCALLBACK(int) natSndThread(RTTHREAD pThread, void *pvUser)
|
---|
544 | {
|
---|
545 | while (g_pNAT->fIsRunning)
|
---|
546 | RTReqProcess(g_pNAT->m_pSendQueue, 0);
|
---|
547 | return VINF_SUCCESS;
|
---|
548 | }
|
---|
549 | static DECLCALLBACK(int) natUrgSndThread(RTTHREAD pThread, void *pvUser)
|
---|
550 | {
|
---|
551 | while (g_pNAT->fIsRunning)
|
---|
552 | RTReqProcess(g_pNAT->m_pUrgSendQueue, 0);
|
---|
553 | return VINF_SUCCESS;
|
---|
554 | }
|
---|
555 |
|
---|
556 | #ifndef VBOX_WITH_HARDENING
|
---|
557 |
|
---|
558 | int main(int argc, char **argv, char **envp)
|
---|
559 | {
|
---|
560 | int rc = RTR3InitAndSUPLib();
|
---|
561 | if (RT_FAILURE(rc))
|
---|
562 | {
|
---|
563 | RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: RTR3InitAndSupLib failed, rc=%Rrc\n", rc);
|
---|
564 | return 1;
|
---|
565 | }
|
---|
566 |
|
---|
567 | return TrustedMain(argc, argv, envp);
|
---|
568 | }
|
---|
569 |
|
---|
570 | # if defined(RT_OS_WINDOWS)
|
---|
571 |
|
---|
572 | static LRESULT CALLBACK WindowProc(HWND hwnd,
|
---|
573 | UINT uMsg,
|
---|
574 | WPARAM wParam,
|
---|
575 | LPARAM lParam
|
---|
576 | )
|
---|
577 | {
|
---|
578 | if(uMsg == WM_DESTROY)
|
---|
579 | {
|
---|
580 | PostQuitMessage(0);
|
---|
581 | return 0;
|
---|
582 | }
|
---|
583 | return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
---|
584 | }
|
---|
585 |
|
---|
586 | static LPCSTR g_WndClassName = "VBoxNetNatClass";
|
---|
587 |
|
---|
588 | static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
|
---|
589 | {
|
---|
590 | HWND hwnd = 0;
|
---|
591 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
|
---|
592 | bool bExit = false;
|
---|
593 |
|
---|
594 | /* Register the Window Class. */
|
---|
595 | WNDCLASS wc;
|
---|
596 | wc.style = 0;
|
---|
597 | wc.lpfnWndProc = WindowProc;
|
---|
598 | wc.cbClsExtra = 0;
|
---|
599 | wc.cbWndExtra = sizeof(void *);
|
---|
600 | wc.hInstance = hInstance;
|
---|
601 | wc.hIcon = NULL;
|
---|
602 | wc.hCursor = NULL;
|
---|
603 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
604 | wc.lpszMenuName = NULL;
|
---|
605 | wc.lpszClassName = g_WndClassName;
|
---|
606 |
|
---|
607 | ATOM atomWindowClass = RegisterClass(&wc);
|
---|
608 |
|
---|
609 | if (atomWindowClass != 0)
|
---|
610 | {
|
---|
611 | /* Create the window. */
|
---|
612 | hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
613 | g_WndClassName, g_WndClassName,
|
---|
614 | WS_POPUPWINDOW,
|
---|
615 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
616 |
|
---|
617 | if (hwnd)
|
---|
618 | {
|
---|
619 | SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
620 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
621 |
|
---|
622 | MSG msg;
|
---|
623 | while (GetMessage(&msg, NULL, 0, 0))
|
---|
624 | {
|
---|
625 | TranslateMessage(&msg);
|
---|
626 | DispatchMessage(&msg);
|
---|
627 | }
|
---|
628 |
|
---|
629 | DestroyWindow (hwnd);
|
---|
630 |
|
---|
631 | bExit = true;
|
---|
632 | }
|
---|
633 |
|
---|
634 | UnregisterClass (g_WndClassName, hInstance);
|
---|
635 | }
|
---|
636 |
|
---|
637 | if(bExit)
|
---|
638 | {
|
---|
639 | /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
|
---|
640 | exit(0);
|
---|
641 | }
|
---|
642 |
|
---|
643 | return 0;
|
---|
644 | }
|
---|
645 |
|
---|
646 |
|
---|
647 |
|
---|
648 | /** (We don't want a console usually.) */
|
---|
649 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
650 | {
|
---|
651 | #if 0
|
---|
652 | NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
|
---|
653 |
|
---|
654 | HANDLE hThread = CreateThread(
|
---|
655 | NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
|
---|
656 | 0, /*__in SIZE_T dwStackSize, */
|
---|
657 | MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
|
---|
658 | NULL, /*__in_opt LPVOID lpParameter,*/
|
---|
659 | 0, /*__in DWORD dwCreationFlags,*/
|
---|
660 | NULL /*__out_opt LPDWORD lpThreadId*/
|
---|
661 | );
|
---|
662 |
|
---|
663 | if(hThread != NULL)
|
---|
664 | CloseHandle(hThread);
|
---|
665 |
|
---|
666 | #endif
|
---|
667 | return main(__argc, __argv, environ);
|
---|
668 | }
|
---|
669 | # endif /* RT_OS_WINDOWS */
|
---|
670 |
|
---|
671 | #endif /* !VBOX_WITH_HARDENING */
|
---|
672 |
|
---|