1 | /* $Id: VBoxNetNAT.cpp 36470 2011-03-30 09:36:09Z 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/vmm/pdmnetinline.h>
|
---|
50 | #include <VBox/vmm/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 || rc == VERR_INTERRUPTED)
|
---|
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 | extern "C" void slirp_output_pending(void *pvUser)
|
---|
373 | {
|
---|
374 | AssertMsgFailed(("Unimplemented"));
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Worker function for drvNATSend().
|
---|
379 | * @thread "NAT" thread.
|
---|
380 | */
|
---|
381 | static void SendWorker(struct mbuf *m, size_t cb)
|
---|
382 | {
|
---|
383 | slirp_input(g_pNAT->m_pNATState, m, cb);
|
---|
384 | }
|
---|
385 |
|
---|
386 | static void IntNetSendWorker(bool urg, void *pvFrame, size_t cbFrame, struct mbuf *m)
|
---|
387 | {
|
---|
388 | Log2(("VBoxNetNAT: going to send some bytes ... \n"));
|
---|
389 | VBoxNetNAT *pThis = g_pNAT;
|
---|
390 | INTNETIFSENDREQ SendReq;
|
---|
391 | int rc;
|
---|
392 |
|
---|
393 | if (!urg)
|
---|
394 | {
|
---|
395 | while (ASMAtomicReadU32(&g_pNAT->cUrgPkt) != 0
|
---|
396 | || ASMAtomicReadU32(&g_pNAT->cPkt) == 0)
|
---|
397 | rc = RTSemEventWait(g_pNAT->m_EventSend, RT_INDEFINITE_WAIT);
|
---|
398 | }
|
---|
399 | else
|
---|
400 | {
|
---|
401 | while (ASMAtomicReadU32(&g_pNAT->cUrgPkt) == 0)
|
---|
402 | rc = RTSemEventWait(g_pNAT->m_EventUrgSend, RT_INDEFINITE_WAIT);
|
---|
403 | }
|
---|
404 | rc = IntNetRingWriteFrame(&pThis->m_pIfBuf->Send, pvFrame, cbFrame);
|
---|
405 | if (RT_FAILURE(rc))
|
---|
406 | {
|
---|
407 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
408 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
409 | SendReq.pSession = pThis->m_pSession;
|
---|
410 | SendReq.hIf = pThis->m_hIf;
|
---|
411 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
412 |
|
---|
413 | rc = IntNetRingWriteFrame(&pThis->m_pIfBuf->Send, pvFrame, cbFrame);
|
---|
414 |
|
---|
415 | }
|
---|
416 | if (RT_SUCCESS(rc))
|
---|
417 | {
|
---|
418 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
419 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
420 | SendReq.pSession = pThis->m_pSession;
|
---|
421 | SendReq.hIf = pThis->m_hIf;
|
---|
422 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
423 | }
|
---|
424 | if (RT_FAILURE(rc))
|
---|
425 | Log2(("VBoxNetNAT: Failed to send packet; rc=%Rrc\n", rc));
|
---|
426 |
|
---|
427 | if (!urg)
|
---|
428 | {
|
---|
429 | ASMAtomicDecU32(&g_pNAT->cPkt);
|
---|
430 | }
|
---|
431 | else {
|
---|
432 | if (ASMAtomicDecU32(&g_pNAT->cUrgPkt) == 0)
|
---|
433 | RTSemEventSignal(g_pNAT->m_EventSend);
|
---|
434 | }
|
---|
435 | natNotifyNATThread();
|
---|
436 | slirp_ext_m_free(pThis->m_pNATState, m, (uint8_t *)pvFrame);
|
---|
437 | }
|
---|
438 |
|
---|
439 | static DECLCALLBACK(int) AsyncIoThread(RTTHREAD pThread, void *pvUser)
|
---|
440 | {
|
---|
441 | VBoxNetNAT *pThis = (VBoxNetNAT *)pvUser;
|
---|
442 | int nFDs = -1;
|
---|
443 | #ifdef RT_OS_WINDOWS
|
---|
444 | HANDLE *pahEvents = slirp_get_events(pThis->m_pNATState);
|
---|
445 | #else /* RT_OS_WINDOWS */
|
---|
446 | unsigned int cPollNegRet = 0;
|
---|
447 | #endif /* !RT_OS_WINDOWS */
|
---|
448 |
|
---|
449 | LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Polling loop.
|
---|
453 | */
|
---|
454 | for(;;)
|
---|
455 | {
|
---|
456 | /*
|
---|
457 | * To prevent concurrent execution of sending/receiving threads
|
---|
458 | */
|
---|
459 | #ifndef RT_OS_WINDOWS
|
---|
460 | nFDs = slirp_get_nsock(pThis->m_pNATState);
|
---|
461 | /* allocation for all sockets + Management pipe */
|
---|
462 | struct pollfd *polls = (struct pollfd *)RTMemAlloc((1 + nFDs) * sizeof(struct pollfd) + sizeof(uint32_t));
|
---|
463 | if (polls == NULL)
|
---|
464 | return VERR_NO_MEMORY;
|
---|
465 |
|
---|
466 | /* don't pass the management pipe */
|
---|
467 | slirp_select_fill(pThis->m_pNATState, &nFDs, &polls[1]);
|
---|
468 | unsigned int cMsTimeout = slirp_get_timeout_ms(pThis->m_pNATState);
|
---|
469 |
|
---|
470 | polls[0].fd = pThis->m_PipeRead;
|
---|
471 | /* POLLRDBAND usually doesn't used on Linux but seems used on Solaris */
|
---|
472 | polls[0].events = POLLRDNORM|POLLPRI|POLLRDBAND;
|
---|
473 | polls[0].revents = 0;
|
---|
474 |
|
---|
475 | int cChangedFDs = poll(polls, nFDs + 1, cMsTimeout);
|
---|
476 | if (cChangedFDs < 0)
|
---|
477 | {
|
---|
478 | if (errno == EINTR)
|
---|
479 | {
|
---|
480 | Log2(("NAT: signal was caught while sleep on poll\n"));
|
---|
481 | /* No error, just process all outstanding requests but don't wait */
|
---|
482 | cChangedFDs = 0;
|
---|
483 | }
|
---|
484 | else if (cPollNegRet++ > 128)
|
---|
485 | {
|
---|
486 | LogRel(("NAT:Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
|
---|
487 | cPollNegRet = 0;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | if (cChangedFDs >= 0)
|
---|
492 | {
|
---|
493 | slirp_select_poll(pThis->m_pNATState, &polls[1], nFDs);
|
---|
494 | if (polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
|
---|
495 | {
|
---|
496 | /* drain the pipe */
|
---|
497 | char ch[1];
|
---|
498 | size_t cbRead;
|
---|
499 | int counter = 0;
|
---|
500 | /*
|
---|
501 | * drvNATSend decoupled so we don't know how many times
|
---|
502 | * device's thread sends before we've entered multiplex,
|
---|
503 | * so to avoid false alarm drain pipe here to the very end
|
---|
504 | *
|
---|
505 | * @todo: Probably we should counter drvNATSend to count how
|
---|
506 | * deep pipe has been filed before drain.
|
---|
507 | *
|
---|
508 | * XXX:Make it reading exactly we need to drain the pipe.
|
---|
509 | */
|
---|
510 | RTFileRead(pThis->m_PipeRead, &ch, 1, &cbRead);
|
---|
511 | }
|
---|
512 | }
|
---|
513 | /* process _all_ outstanding requests but don't wait */
|
---|
514 | RTReqProcess(pThis->m_pReqQueue, 0);
|
---|
515 | RTMemFree(polls);
|
---|
516 |
|
---|
517 | #else /* RT_OS_WINDOWS */
|
---|
518 | nFDs = -1;
|
---|
519 | slirp_select_fill(pThis->m_pNATState, &nFDs);
|
---|
520 | DWORD dwEvent = WSAWaitForMultipleEvents(nFDs, pahEvents, FALSE,
|
---|
521 | slirp_get_timeout_ms(pThis->m_pNATState),
|
---|
522 | FALSE);
|
---|
523 | if ( (dwEvent < WSA_WAIT_EVENT_0 || dwEvent > WSA_WAIT_EVENT_0 + nFDs - 1)
|
---|
524 | && dwEvent != WSA_WAIT_TIMEOUT)
|
---|
525 | {
|
---|
526 | int error = WSAGetLastError();
|
---|
527 | LogRel(("NAT: WSAWaitForMultipleEvents returned %d (error %d)\n", dwEvent, error));
|
---|
528 | RTAssertReleasePanic();
|
---|
529 | }
|
---|
530 |
|
---|
531 | if (dwEvent == WSA_WAIT_TIMEOUT)
|
---|
532 | {
|
---|
533 | /* only check for slow/fast timers */
|
---|
534 | slirp_select_poll(pThis->m_pNATState, /* fTimeout=*/true, /*fIcmp=*/false);
|
---|
535 | continue;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /* poll the sockets in any case */
|
---|
539 | slirp_select_poll(pThis->m_pNATState, /* fTimeout=*/false, /* fIcmp=*/(dwEvent == WSA_WAIT_EVENT_0));
|
---|
540 | /* process _all_ outstanding requests but don't wait */
|
---|
541 | RTReqProcess(pThis->m_pReqQueue, 0);
|
---|
542 | #endif /* RT_OS_WINDOWS */
|
---|
543 | }
|
---|
544 |
|
---|
545 | return VINF_SUCCESS;
|
---|
546 | }
|
---|
547 |
|
---|
548 | static DECLCALLBACK(int) natSndThread(RTTHREAD pThread, void *pvUser)
|
---|
549 | {
|
---|
550 | while (g_pNAT->fIsRunning)
|
---|
551 | RTReqProcess(g_pNAT->m_pSendQueue, 0);
|
---|
552 | return VINF_SUCCESS;
|
---|
553 | }
|
---|
554 | static DECLCALLBACK(int) natUrgSndThread(RTTHREAD pThread, void *pvUser)
|
---|
555 | {
|
---|
556 | while (g_pNAT->fIsRunning)
|
---|
557 | RTReqProcess(g_pNAT->m_pUrgSendQueue, 0);
|
---|
558 | return VINF_SUCCESS;
|
---|
559 | }
|
---|
560 |
|
---|
561 | #ifndef VBOX_WITH_HARDENING
|
---|
562 |
|
---|
563 | int main(int argc, char **argv, char **envp)
|
---|
564 | {
|
---|
565 | int rc = RTR3InitAndSUPLib();
|
---|
566 | if (RT_FAILURE(rc))
|
---|
567 | {
|
---|
568 | RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: RTR3InitAndSupLib failed, rc=%Rrc\n", rc);
|
---|
569 | return 1;
|
---|
570 | }
|
---|
571 |
|
---|
572 | return TrustedMain(argc, argv, envp);
|
---|
573 | }
|
---|
574 |
|
---|
575 | # if defined(RT_OS_WINDOWS)
|
---|
576 |
|
---|
577 | static LRESULT CALLBACK WindowProc(HWND hwnd,
|
---|
578 | UINT uMsg,
|
---|
579 | WPARAM wParam,
|
---|
580 | LPARAM lParam
|
---|
581 | )
|
---|
582 | {
|
---|
583 | if(uMsg == WM_DESTROY)
|
---|
584 | {
|
---|
585 | PostQuitMessage(0);
|
---|
586 | return 0;
|
---|
587 | }
|
---|
588 | return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
---|
589 | }
|
---|
590 |
|
---|
591 | static LPCSTR g_WndClassName = "VBoxNetNatClass";
|
---|
592 |
|
---|
593 | static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
|
---|
594 | {
|
---|
595 | HWND hwnd = 0;
|
---|
596 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
|
---|
597 | bool bExit = false;
|
---|
598 |
|
---|
599 | /* Register the Window Class. */
|
---|
600 | WNDCLASS wc;
|
---|
601 | wc.style = 0;
|
---|
602 | wc.lpfnWndProc = WindowProc;
|
---|
603 | wc.cbClsExtra = 0;
|
---|
604 | wc.cbWndExtra = sizeof(void *);
|
---|
605 | wc.hInstance = hInstance;
|
---|
606 | wc.hIcon = NULL;
|
---|
607 | wc.hCursor = NULL;
|
---|
608 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
609 | wc.lpszMenuName = NULL;
|
---|
610 | wc.lpszClassName = g_WndClassName;
|
---|
611 |
|
---|
612 | ATOM atomWindowClass = RegisterClass(&wc);
|
---|
613 |
|
---|
614 | if (atomWindowClass != 0)
|
---|
615 | {
|
---|
616 | /* Create the window. */
|
---|
617 | hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
618 | g_WndClassName, g_WndClassName,
|
---|
619 | WS_POPUPWINDOW,
|
---|
620 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
621 |
|
---|
622 | if (hwnd)
|
---|
623 | {
|
---|
624 | SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
625 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
626 |
|
---|
627 | MSG msg;
|
---|
628 | while (GetMessage(&msg, NULL, 0, 0))
|
---|
629 | {
|
---|
630 | TranslateMessage(&msg);
|
---|
631 | DispatchMessage(&msg);
|
---|
632 | }
|
---|
633 |
|
---|
634 | DestroyWindow (hwnd);
|
---|
635 |
|
---|
636 | bExit = true;
|
---|
637 | }
|
---|
638 |
|
---|
639 | UnregisterClass (g_WndClassName, hInstance);
|
---|
640 | }
|
---|
641 |
|
---|
642 | if(bExit)
|
---|
643 | {
|
---|
644 | /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
|
---|
645 | exit(0);
|
---|
646 | }
|
---|
647 |
|
---|
648 | return 0;
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 |
|
---|
653 | /** (We don't want a console usually.) */
|
---|
654 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
655 | {
|
---|
656 | #if 0
|
---|
657 | NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
|
---|
658 |
|
---|
659 | HANDLE hThread = CreateThread(
|
---|
660 | NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
|
---|
661 | 0, /*__in SIZE_T dwStackSize, */
|
---|
662 | MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
|
---|
663 | NULL, /*__in_opt LPVOID lpParameter,*/
|
---|
664 | 0, /*__in DWORD dwCreationFlags,*/
|
---|
665 | NULL /*__out_opt LPDWORD lpThreadId*/
|
---|
666 | );
|
---|
667 |
|
---|
668 | if(hThread != NULL)
|
---|
669 | CloseHandle(hThread);
|
---|
670 |
|
---|
671 | #endif
|
---|
672 | return main(__argc, __argv, environ);
|
---|
673 | }
|
---|
674 | # endif /* RT_OS_WINDOWS */
|
---|
675 |
|
---|
676 | #endif /* !VBOX_WITH_HARDENING */
|
---|
677 |
|
---|