1 | /* $Id: VBoxNetLwipNAT.cpp 55365 2015-04-22 09:34:46Z 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 | /* Must be included before winutils.h (lwip/def.h), otherwise Windows build breaks. */
|
---|
19 | #define LOG_GROUP LOG_GROUP_NAT_SERVICE
|
---|
20 |
|
---|
21 | #include <iprt/cpp/mem.h>
|
---|
22 |
|
---|
23 | #include "winutils.h"
|
---|
24 |
|
---|
25 | #include <VBox/com/assert.h>
|
---|
26 | #include <VBox/com/com.h>
|
---|
27 | #include <VBox/com/listeners.h>
|
---|
28 | #include <VBox/com/string.h>
|
---|
29 | #include <VBox/com/Guid.h>
|
---|
30 | #include <VBox/com/array.h>
|
---|
31 | #include <VBox/com/ErrorInfo.h>
|
---|
32 | #include <VBox/com/errorprint.h>
|
---|
33 | #include <VBox/com/VirtualBox.h>
|
---|
34 |
|
---|
35 | #include <iprt/net.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/alloca.h>
|
---|
38 | #ifndef RT_OS_WINDOWS
|
---|
39 | # include <arpa/inet.h>
|
---|
40 | #endif
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/time.h>
|
---|
43 | #include <iprt/timer.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 | #include <iprt/stream.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/pipe.h>
|
---|
49 | #include <iprt/getopt.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/mem.h>
|
---|
52 | #include <iprt/message.h>
|
---|
53 | #include <iprt/req.h>
|
---|
54 | #include <iprt/file.h>
|
---|
55 | #include <iprt/semaphore.h>
|
---|
56 | #include <iprt/cpp/utils.h>
|
---|
57 | #include <VBox/log.h>
|
---|
58 |
|
---|
59 | #include <VBox/sup.h>
|
---|
60 | #include <VBox/intnet.h>
|
---|
61 | #include <VBox/intnetinline.h>
|
---|
62 | #include <VBox/vmm/pdmnetinline.h>
|
---|
63 | #include <VBox/vmm/vmm.h>
|
---|
64 | #include <VBox/version.h>
|
---|
65 |
|
---|
66 | #ifndef RT_OS_WINDOWS
|
---|
67 | # include <sys/poll.h>
|
---|
68 | # include <sys/socket.h>
|
---|
69 | # include <netinet/in.h>
|
---|
70 | # ifdef RT_OS_LINUX
|
---|
71 | # include <linux/icmp.h> /* ICMP_FILTER */
|
---|
72 | # endif
|
---|
73 | # include <netinet/icmp6.h>
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #include <map>
|
---|
77 | #include <vector>
|
---|
78 | #include <string>
|
---|
79 |
|
---|
80 | #include <stdio.h>
|
---|
81 |
|
---|
82 | #include "../NetLib/VBoxNetLib.h"
|
---|
83 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
84 | #include "../NetLib/utils.h"
|
---|
85 | #include "VBoxLwipCore.h"
|
---|
86 |
|
---|
87 | extern "C"
|
---|
88 | {
|
---|
89 | /* bunch of LWIP headers */
|
---|
90 | #include "lwip/sys.h"
|
---|
91 | #include "lwip/pbuf.h"
|
---|
92 | #include "lwip/netif.h"
|
---|
93 | #include "lwip/ethip6.h"
|
---|
94 | #include "lwip/nd6.h" // for proxy_na_hook
|
---|
95 | #include "lwip/mld6.h"
|
---|
96 | #include "lwip/tcpip.h"
|
---|
97 | #include "netif/etharp.h"
|
---|
98 |
|
---|
99 | #include "proxy.h"
|
---|
100 | #include "pxremap.h"
|
---|
101 | #include "portfwd.h"
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | #if defined(VBOX_RAWSOCK_DEBUG_HELPER) \
|
---|
106 | && (defined(VBOX_WITH_HARDENING) \
|
---|
107 | || defined(RT_OS_WINDOWS) \
|
---|
108 | || defined(RT_OS_DARWIN))
|
---|
109 | # error Have you forgotten to turn off VBOX_RAWSOCK_DEBUG_HELPER?
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | #ifdef VBOX_RAWSOCK_DEBUG_HELPER
|
---|
113 | extern "C" int getrawsock(int type);
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | #include "../NetLib/VBoxPortForwardString.h"
|
---|
117 |
|
---|
118 | static RTGETOPTDEF g_aGetOptDef[] =
|
---|
119 | {
|
---|
120 | { "--port-forward4", 'p', RTGETOPT_REQ_STRING },
|
---|
121 | { "--port-forward6", 'P', RTGETOPT_REQ_STRING }
|
---|
122 | };
|
---|
123 |
|
---|
124 | typedef struct NATSEVICEPORTFORWARDRULE
|
---|
125 | {
|
---|
126 | PORTFORWARDRULE Pfr;
|
---|
127 | fwspec FWSpec;
|
---|
128 | } NATSEVICEPORTFORWARDRULE, *PNATSEVICEPORTFORWARDRULE;
|
---|
129 |
|
---|
130 | typedef std::vector<NATSEVICEPORTFORWARDRULE> VECNATSERVICEPF;
|
---|
131 | typedef VECNATSERVICEPF::iterator ITERATORNATSERVICEPF;
|
---|
132 | typedef VECNATSERVICEPF::const_iterator CITERATORNATSERVICEPF;
|
---|
133 |
|
---|
134 | static int fetchNatPortForwardRules(const ComNatPtr&, bool, VECNATSERVICEPF&);
|
---|
135 |
|
---|
136 | static int vboxNetNATLogInit(int argc, char **argv);
|
---|
137 |
|
---|
138 |
|
---|
139 | class VBoxNetLwipNAT: public VBoxNetBaseService, public NATNetworkEventAdapter
|
---|
140 | {
|
---|
141 | friend class NATNetworkListener;
|
---|
142 | public:
|
---|
143 | VBoxNetLwipNAT(SOCKET icmpsock4, SOCKET icmpsock6);
|
---|
144 | virtual ~VBoxNetLwipNAT();
|
---|
145 | void usage(){ /* @todo: should be implemented */ };
|
---|
146 | int run();
|
---|
147 | virtual int init(void);
|
---|
148 | virtual int parseOpt(int rc, const RTGETOPTUNION& getOptVal);
|
---|
149 | /* VBoxNetNAT always needs Main */
|
---|
150 | virtual bool isMainNeeded() const { return true; }
|
---|
151 | virtual int processFrame(void *, size_t);
|
---|
152 | virtual int processGSO(PCPDMNETWORKGSO, size_t);
|
---|
153 | virtual int processUDP(void *, size_t) { return VERR_IGNORED; }
|
---|
154 |
|
---|
155 | private:
|
---|
156 | struct proxy_options m_ProxyOptions;
|
---|
157 | struct sockaddr_in m_src4;
|
---|
158 | struct sockaddr_in6 m_src6;
|
---|
159 | /**
|
---|
160 | * place for registered local interfaces.
|
---|
161 | */
|
---|
162 | ip4_lomap m_lo2off[10];
|
---|
163 | ip4_lomap_desc m_loOptDescriptor;
|
---|
164 |
|
---|
165 | uint16_t m_u16Mtu;
|
---|
166 | netif m_LwipNetIf;
|
---|
167 |
|
---|
168 | /* Our NAT network descriptor in Main */
|
---|
169 | ComPtr<INATNetwork> m_net;
|
---|
170 | ComPtr<IHost> m_host;
|
---|
171 |
|
---|
172 | ComNatListenerPtr m_NatListener;
|
---|
173 | ComNatListenerPtr m_VBoxListener;
|
---|
174 | ComNatListenerPtr m_VBoxClientListener;
|
---|
175 | static INTNETSEG aXmitSeg[64];
|
---|
176 |
|
---|
177 | HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent);
|
---|
178 |
|
---|
179 | const char **getHostNameservers();
|
---|
180 |
|
---|
181 | /* Only for debug needs, by default NAT service should load rules from SVC
|
---|
182 | * on startup, and then on sync them on events.
|
---|
183 | */
|
---|
184 | bool fDontLoadRulesOnStartup;
|
---|
185 | static void onLwipTcpIpInit(void *arg);
|
---|
186 | static void onLwipTcpIpFini(void *arg);
|
---|
187 | static err_t netifInit(netif *pNetif);
|
---|
188 | static err_t netifLinkoutput(netif *pNetif, pbuf *pBuf);
|
---|
189 | static int intNetThreadRecv(RTTHREAD, void *);
|
---|
190 |
|
---|
191 | VECNATSERVICEPF m_vecPortForwardRule4;
|
---|
192 | VECNATSERVICEPF m_vecPortForwardRule6;
|
---|
193 |
|
---|
194 | static int natServicePfRegister(NATSEVICEPORTFORWARDRULE& natServicePf);
|
---|
195 | static int natServiceProcessRegisteredPf(VECNATSERVICEPF& vecPf);
|
---|
196 | };
|
---|
197 |
|
---|
198 |
|
---|
199 | static VBoxNetLwipNAT *g_pLwipNat;
|
---|
200 | INTNETSEG VBoxNetLwipNAT::aXmitSeg[64];
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * @note: this work on Event thread.
|
---|
204 | */
|
---|
205 | HRESULT VBoxNetLwipNAT::HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
|
---|
206 | {
|
---|
207 | HRESULT hrc = S_OK;
|
---|
208 | switch (aEventType)
|
---|
209 | {
|
---|
210 | case VBoxEventType_OnNATNetworkSetting:
|
---|
211 | {
|
---|
212 | ComPtr<INATNetworkSettingEvent> pSettingsEvent(pEvent);
|
---|
213 |
|
---|
214 | com::Bstr networkName;
|
---|
215 | hrc = pSettingsEvent->COMGETTER(NetworkName)(networkName.asOutParam());
|
---|
216 | AssertComRCReturn(hrc, hrc);
|
---|
217 | if (networkName.compare(getNetworkName().c_str()))
|
---|
218 | break; /* change not for our network */
|
---|
219 |
|
---|
220 | // XXX: only handle IPv6 default route for now
|
---|
221 | if (!m_ProxyOptions.ipv6_enabled)
|
---|
222 | break;
|
---|
223 |
|
---|
224 | BOOL fIPv6DefaultRoute = FALSE;
|
---|
225 | hrc = pSettingsEvent->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
|
---|
226 | AssertComRCReturn(hrc, hrc);
|
---|
227 |
|
---|
228 | if (m_ProxyOptions.ipv6_defroute == fIPv6DefaultRoute)
|
---|
229 | break;
|
---|
230 |
|
---|
231 | m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
|
---|
232 | tcpip_callback_with_block(proxy_rtadvd_do_quick, &m_LwipNetIf, 0);
|
---|
233 | break;
|
---|
234 | }
|
---|
235 |
|
---|
236 | case VBoxEventType_OnNATNetworkPortForward:
|
---|
237 | {
|
---|
238 | ComPtr<INATNetworkPortForwardEvent> pForwardEvent = pEvent;
|
---|
239 |
|
---|
240 | com::Bstr networkName;
|
---|
241 | hrc = pForwardEvent->COMGETTER(NetworkName)(networkName.asOutParam());
|
---|
242 | AssertComRCReturn(hrc, hrc);
|
---|
243 | if (networkName.compare(getNetworkName().c_str()))
|
---|
244 | break; /* change not for our network */
|
---|
245 |
|
---|
246 | BOOL fCreateFW;
|
---|
247 | hrc = pForwardEvent->COMGETTER(Create)(&fCreateFW);
|
---|
248 | AssertComRCReturn(hrc, hrc);
|
---|
249 |
|
---|
250 | BOOL fIPv6FW;
|
---|
251 | hrc = pForwardEvent->COMGETTER(Ipv6)(&fIPv6FW);
|
---|
252 | AssertComRCReturn(hrc, hrc);
|
---|
253 |
|
---|
254 | com::Bstr name;
|
---|
255 | hrc = pForwardEvent->COMGETTER(Name)(name.asOutParam());
|
---|
256 | AssertComRCReturn(hrc, hrc);
|
---|
257 |
|
---|
258 | NATProtocol_T proto = NATProtocol_TCP;
|
---|
259 | hrc = pForwardEvent->COMGETTER(Proto)(&proto);
|
---|
260 | AssertComRCReturn(hrc, hrc);
|
---|
261 |
|
---|
262 | com::Bstr strHostAddr;
|
---|
263 | hrc = pForwardEvent->COMGETTER(HostIp)(strHostAddr.asOutParam());
|
---|
264 | AssertComRCReturn(hrc, hrc);
|
---|
265 |
|
---|
266 | LONG lHostPort;
|
---|
267 | hrc = pForwardEvent->COMGETTER(HostPort)(&lHostPort);
|
---|
268 | AssertComRCReturn(hrc, hrc);
|
---|
269 |
|
---|
270 | com::Bstr strGuestAddr;
|
---|
271 | hrc = pForwardEvent->COMGETTER(GuestIp)(strGuestAddr.asOutParam());
|
---|
272 | AssertComRCReturn(hrc, hrc);
|
---|
273 |
|
---|
274 | LONG lGuestPort;
|
---|
275 | hrc = pForwardEvent->COMGETTER(GuestPort)(&lGuestPort);
|
---|
276 | AssertComRCReturn(hrc, hrc);
|
---|
277 |
|
---|
278 | VECNATSERVICEPF& rules = fIPv6FW ? m_vecPortForwardRule6
|
---|
279 | : m_vecPortForwardRule4;
|
---|
280 |
|
---|
281 | NATSEVICEPORTFORWARDRULE r;
|
---|
282 | RT_ZERO(r);
|
---|
283 |
|
---|
284 | r.Pfr.fPfrIPv6 = fIPv6FW;
|
---|
285 |
|
---|
286 | switch (proto)
|
---|
287 | {
|
---|
288 | case NATProtocol_TCP:
|
---|
289 | r.Pfr.iPfrProto = IPPROTO_TCP;
|
---|
290 | break;
|
---|
291 | case NATProtocol_UDP:
|
---|
292 | r.Pfr.iPfrProto = IPPROTO_UDP;
|
---|
293 | break;
|
---|
294 |
|
---|
295 | default:
|
---|
296 | LogRel(("Event: %s %s port-forwarding rule \"%s\":"
|
---|
297 | " invalid protocol %d\n",
|
---|
298 | fCreateFW ? "Add" : "Remove",
|
---|
299 | fIPv6FW ? "IPv6" : "IPv4",
|
---|
300 | com::Utf8Str(name).c_str(),
|
---|
301 | (int)proto));
|
---|
302 | goto port_forward_done;
|
---|
303 | }
|
---|
304 |
|
---|
305 | LogRel(("Event: %s %s port-forwarding rule \"%s\":"
|
---|
306 | " %s %s%s%s:%d -> %s%s%s:%d\n",
|
---|
307 | fCreateFW ? "Add" : "Remove",
|
---|
308 | fIPv6FW ? "IPv6" : "IPv4",
|
---|
309 | com::Utf8Str(name).c_str(),
|
---|
310 | proto == NATProtocol_TCP ? "TCP" : "UDP",
|
---|
311 | /* from */
|
---|
312 | fIPv6FW ? "[" : "",
|
---|
313 | com::Utf8Str(strHostAddr).c_str(),
|
---|
314 | fIPv6FW ? "]" : "",
|
---|
315 | lHostPort,
|
---|
316 | /* to */
|
---|
317 | fIPv6FW ? "[" : "",
|
---|
318 | com::Utf8Str(strGuestAddr).c_str(),
|
---|
319 | fIPv6FW ? "]" : "",
|
---|
320 | lGuestPort));
|
---|
321 |
|
---|
322 | if (name.length() > sizeof(r.Pfr.szPfrName))
|
---|
323 | {
|
---|
324 | hrc = E_INVALIDARG;
|
---|
325 | goto port_forward_done;
|
---|
326 | }
|
---|
327 |
|
---|
328 | RTStrPrintf(r.Pfr.szPfrName, sizeof(r.Pfr.szPfrName),
|
---|
329 | "%s", com::Utf8Str(name).c_str());
|
---|
330 |
|
---|
331 | RTStrPrintf(r.Pfr.szPfrHostAddr, sizeof(r.Pfr.szPfrHostAddr),
|
---|
332 | "%s", com::Utf8Str(strHostAddr).c_str());
|
---|
333 |
|
---|
334 | /* XXX: limits should be checked */
|
---|
335 | r.Pfr.u16PfrHostPort = (uint16_t)lHostPort;
|
---|
336 |
|
---|
337 | RTStrPrintf(r.Pfr.szPfrGuestAddr, sizeof(r.Pfr.szPfrGuestAddr),
|
---|
338 | "%s", com::Utf8Str(strGuestAddr).c_str());
|
---|
339 |
|
---|
340 | /* XXX: limits should be checked */
|
---|
341 | r.Pfr.u16PfrGuestPort = (uint16_t)lGuestPort;
|
---|
342 |
|
---|
343 | if (fCreateFW) /* Addition */
|
---|
344 | {
|
---|
345 | int rc = natServicePfRegister(r);
|
---|
346 | if (RT_SUCCESS(rc))
|
---|
347 | rules.push_back(r);
|
---|
348 | }
|
---|
349 | else /* Deletion */
|
---|
350 | {
|
---|
351 | ITERATORNATSERVICEPF it;
|
---|
352 | for (it = rules.begin(); it != rules.end(); ++it)
|
---|
353 | {
|
---|
354 | /* compare */
|
---|
355 | NATSEVICEPORTFORWARDRULE& natFw = *it;
|
---|
356 | if ( natFw.Pfr.iPfrProto == r.Pfr.iPfrProto
|
---|
357 | && natFw.Pfr.u16PfrHostPort == r.Pfr.u16PfrHostPort
|
---|
358 | && (strncmp(natFw.Pfr.szPfrHostAddr, r.Pfr.szPfrHostAddr, INET6_ADDRSTRLEN) == 0)
|
---|
359 | && natFw.Pfr.u16PfrGuestPort == r.Pfr.u16PfrGuestPort
|
---|
360 | && (strncmp(natFw.Pfr.szPfrGuestAddr, r.Pfr.szPfrGuestAddr, INET6_ADDRSTRLEN) == 0))
|
---|
361 | {
|
---|
362 | RTCMemAutoPtr<fwspec> pFwCopy;
|
---|
363 | if (RT_UNLIKELY(!pFwCopy.alloc()))
|
---|
364 | break;
|
---|
365 |
|
---|
366 | memcpy(pFwCopy.get(), &natFw.FWSpec, sizeof(natFw.FWSpec));
|
---|
367 |
|
---|
368 | int status = portfwd_rule_del(pFwCopy.get());
|
---|
369 | if (status != 0)
|
---|
370 | break;
|
---|
371 |
|
---|
372 | pFwCopy.release(); /* owned by lwip thread now */
|
---|
373 | rules.erase(it);
|
---|
374 | break;
|
---|
375 | }
|
---|
376 | } /* loop over vector elements */
|
---|
377 | } /* condition add or delete */
|
---|
378 | port_forward_done:
|
---|
379 | /* clean up strings */
|
---|
380 | name.setNull();
|
---|
381 | strHostAddr.setNull();
|
---|
382 | strGuestAddr.setNull();
|
---|
383 | break;
|
---|
384 | }
|
---|
385 |
|
---|
386 | case VBoxEventType_OnHostNameResolutionConfigurationChange:
|
---|
387 | {
|
---|
388 | const char **ppcszNameServers = getHostNameservers();
|
---|
389 | err_t error;
|
---|
390 |
|
---|
391 | error = tcpip_callback_with_block(pxdns_set_nameservers,
|
---|
392 | ppcszNameServers,
|
---|
393 | /* :block */ 0);
|
---|
394 | if (error != ERR_OK && ppcszNameServers != NULL)
|
---|
395 | RTMemFree(ppcszNameServers);
|
---|
396 | break;
|
---|
397 | }
|
---|
398 |
|
---|
399 | case VBoxEventType_OnNATNetworkStartStop:
|
---|
400 | {
|
---|
401 | ComPtr <INATNetworkStartStopEvent> pStartStopEvent = pEvent;
|
---|
402 |
|
---|
403 | com::Bstr networkName;
|
---|
404 | hrc = pStartStopEvent->COMGETTER(NetworkName)(networkName.asOutParam());
|
---|
405 | AssertComRCReturn(hrc, hrc);
|
---|
406 | if (networkName.compare(getNetworkName().c_str()))
|
---|
407 | break; /* change not for our network */
|
---|
408 |
|
---|
409 | BOOL fStart = TRUE;
|
---|
410 | hrc = pStartStopEvent->COMGETTER(StartEvent)(&fStart);
|
---|
411 | AssertComRCReturn(hrc, hrc);
|
---|
412 |
|
---|
413 | if (!fStart)
|
---|
414 | shutdown();
|
---|
415 | break;
|
---|
416 | }
|
---|
417 |
|
---|
418 | case VBoxEventType_OnVBoxSVCAvailabilityChanged:
|
---|
419 | {
|
---|
420 | LogRel(("VBoxSVC became unavailable, exiting.\n"));
|
---|
421 | shutdown();
|
---|
422 | break;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | return hrc;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | void VBoxNetLwipNAT::onLwipTcpIpInit(void* arg)
|
---|
430 | {
|
---|
431 | AssertPtrReturnVoid(arg);
|
---|
432 | VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(arg);
|
---|
433 |
|
---|
434 | HRESULT hrc = com::Initialize();
|
---|
435 | Assert(!FAILED(hrc));
|
---|
436 |
|
---|
437 | proxy_arp_hook = pxremap_proxy_arp;
|
---|
438 | proxy_ip4_divert_hook = pxremap_ip4_divert;
|
---|
439 |
|
---|
440 | proxy_na_hook = pxremap_proxy_na;
|
---|
441 | proxy_ip6_divert_hook = pxremap_ip6_divert;
|
---|
442 |
|
---|
443 | /* lwip thread */
|
---|
444 | RTNETADDRIPV4 network;
|
---|
445 | RTNETADDRIPV4 address = g_pLwipNat->getIpv4Address();
|
---|
446 | RTNETADDRIPV4 netmask = g_pLwipNat->getIpv4Netmask();
|
---|
447 | network.u = address.u & netmask.u;
|
---|
448 |
|
---|
449 | ip_addr LwipIpAddr, LwipIpNetMask, LwipIpNetwork;
|
---|
450 |
|
---|
451 | memcpy(&LwipIpAddr, &address, sizeof(ip_addr));
|
---|
452 | memcpy(&LwipIpNetMask, &netmask, sizeof(ip_addr));
|
---|
453 | memcpy(&LwipIpNetwork, &network, sizeof(ip_addr));
|
---|
454 |
|
---|
455 | netif *pNetif = netif_add(&g_pLwipNat->m_LwipNetIf /* Lwip Interface */,
|
---|
456 | &LwipIpAddr /* IP address*/,
|
---|
457 | &LwipIpNetMask /* Network mask */,
|
---|
458 | &LwipIpAddr /* gateway address, @todo: is self IP acceptable? */,
|
---|
459 | g_pLwipNat /* state */,
|
---|
460 | VBoxNetLwipNAT::netifInit /* netif_init_fn */,
|
---|
461 | tcpip_input /* netif_input_fn */);
|
---|
462 |
|
---|
463 | AssertPtrReturnVoid(pNetif);
|
---|
464 |
|
---|
465 | LogRel(("netif %c%c%d: mac %RTmac\n",
|
---|
466 | pNetif->name[0], pNetif->name[1], pNetif->num,
|
---|
467 | pNetif->hwaddr));
|
---|
468 | LogRel(("netif %c%c%d: inet %RTnaipv4 netmask %RTnaipv4\n",
|
---|
469 | pNetif->name[0], pNetif->name[1], pNetif->num,
|
---|
470 | pNetif->ip_addr, pNetif->netmask));
|
---|
471 | for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
|
---|
472 | if (!ip6_addr_isinvalid(netif_ip6_addr_state(pNetif, i))) {
|
---|
473 | LogRel(("netif %c%c%d: inet6 %RTnaipv6\n",
|
---|
474 | pNetif->name[0], pNetif->name[1], pNetif->num,
|
---|
475 | netif_ip6_addr(pNetif, i)));
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | netif_set_up(pNetif);
|
---|
480 | netif_set_link_up(pNetif);
|
---|
481 |
|
---|
482 | if (pNat->m_ProxyOptions.ipv6_enabled) {
|
---|
483 | /*
|
---|
484 | * XXX: lwIP currently only ever calls mld6_joingroup() in
|
---|
485 | * nd6_tmr() for fresh tentative addresses, which is a wrong place
|
---|
486 | * to do it - but I'm not keen on fixing this properly for now
|
---|
487 | * (with correct handling of interface up and down transitions,
|
---|
488 | * etc). So stick it here as a kludge.
|
---|
489 | */
|
---|
490 | for (int i = 0; i <= 1; ++i) {
|
---|
491 | ip6_addr_t *paddr = netif_ip6_addr(pNetif, i);
|
---|
492 |
|
---|
493 | ip6_addr_t solicited_node_multicast_address;
|
---|
494 | ip6_addr_set_solicitednode(&solicited_node_multicast_address,
|
---|
495 | paddr->addr[3]);
|
---|
496 | mld6_joingroup(paddr, &solicited_node_multicast_address);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * XXX: We must join the solicited-node multicast for the
|
---|
501 | * addresses we do IPv6 NA-proxy for. We map IPv6 loopback to
|
---|
502 | * proxy address + 1. We only need the low 24 bits, and those are
|
---|
503 | * fixed.
|
---|
504 | */
|
---|
505 | {
|
---|
506 | ip6_addr_t solicited_node_multicast_address;
|
---|
507 |
|
---|
508 | ip6_addr_set_solicitednode(&solicited_node_multicast_address,
|
---|
509 | /* last 24 bits of the address */
|
---|
510 | PP_HTONL(0x00000002));
|
---|
511 | mld6_netif_joingroup(pNetif, &solicited_node_multicast_address);
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | proxy_init(&g_pLwipNat->m_LwipNetIf, &g_pLwipNat->m_ProxyOptions);
|
---|
516 |
|
---|
517 | natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule4);
|
---|
518 | natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule6);
|
---|
519 | }
|
---|
520 |
|
---|
521 |
|
---|
522 | void VBoxNetLwipNAT::onLwipTcpIpFini(void* arg)
|
---|
523 | {
|
---|
524 | AssertPtrReturnVoid(arg);
|
---|
525 | VBoxNetLwipNAT *pThis = (VBoxNetLwipNAT *)arg;
|
---|
526 |
|
---|
527 | /* XXX: proxy finalization */
|
---|
528 | netif_set_link_down(&g_pLwipNat->m_LwipNetIf);
|
---|
529 | netif_set_down(&g_pLwipNat->m_LwipNetIf);
|
---|
530 | netif_remove(&g_pLwipNat->m_LwipNetIf);
|
---|
531 |
|
---|
532 | }
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Callback for netif_add() to initialize the interface.
|
---|
536 | */
|
---|
537 | err_t VBoxNetLwipNAT::netifInit(netif *pNetif)
|
---|
538 | {
|
---|
539 | err_t rcLwip = ERR_OK;
|
---|
540 |
|
---|
541 | AssertPtrReturn(pNetif, ERR_ARG);
|
---|
542 |
|
---|
543 | VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(pNetif->state);
|
---|
544 | AssertPtrReturn(pNat, ERR_ARG);
|
---|
545 |
|
---|
546 | LogFlowFunc(("ENTER: pNetif[%c%c%d]\n", pNetif->name[0], pNetif->name[1], pNetif->num));
|
---|
547 | /* validity */
|
---|
548 | AssertReturn( pNetif->name[0] == 'N'
|
---|
549 | && pNetif->name[1] == 'T', ERR_ARG);
|
---|
550 |
|
---|
551 |
|
---|
552 | pNetif->hwaddr_len = sizeof(RTMAC);
|
---|
553 | RTMAC mac = g_pLwipNat->getMacAddress();
|
---|
554 | memcpy(pNetif->hwaddr, &mac, sizeof(RTMAC));
|
---|
555 |
|
---|
556 | pNat->m_u16Mtu = 1500; // XXX: FIXME
|
---|
557 | pNetif->mtu = pNat->m_u16Mtu;
|
---|
558 |
|
---|
559 | pNetif->flags = NETIF_FLAG_BROADCAST
|
---|
560 | | NETIF_FLAG_ETHARP /* Don't bother driver with ARP and let Lwip resolve ARP handling */
|
---|
561 | | NETIF_FLAG_ETHERNET; /* Lwip works with ethernet too */
|
---|
562 |
|
---|
563 | pNetif->linkoutput = netifLinkoutput; /* ether-level-pipe */
|
---|
564 | pNetif->output = etharp_output; /* ip-pipe */
|
---|
565 |
|
---|
566 | if (pNat->m_ProxyOptions.ipv6_enabled) {
|
---|
567 | pNetif->output_ip6 = ethip6_output;
|
---|
568 |
|
---|
569 | /* IPv6 link-local address in slot 0 */
|
---|
570 | netif_create_ip6_linklocal_address(pNetif, /* :from_mac_48bit */ 1);
|
---|
571 | netif_ip6_addr_set_state(pNetif, 0, IP6_ADDR_PREFERRED); // skip DAD
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * RFC 4193 Locally Assigned Global ID (ULA) in slot 1
|
---|
575 | * [fd17:625c:f037:XXXX::1] where XXXX, 16 bit Subnet ID, are two
|
---|
576 | * bytes from the middle of the IPv4 address, e.g. :dead: for
|
---|
577 | * 10.222.173.1
|
---|
578 | */
|
---|
579 | u8_t nethi = ip4_addr2(&pNetif->ip_addr);
|
---|
580 | u8_t netlo = ip4_addr3(&pNetif->ip_addr);
|
---|
581 |
|
---|
582 | ip6_addr_t *paddr = netif_ip6_addr(pNetif, 1);
|
---|
583 | IP6_ADDR(paddr, 0, 0xFD, 0x17, 0x62, 0x5C);
|
---|
584 | IP6_ADDR(paddr, 1, 0xF0, 0x37, nethi, netlo);
|
---|
585 | IP6_ADDR(paddr, 2, 0x00, 0x00, 0x00, 0x00);
|
---|
586 | IP6_ADDR(paddr, 3, 0x00, 0x00, 0x00, 0x01);
|
---|
587 | netif_ip6_addr_set_state(pNetif, 1, IP6_ADDR_PREFERRED);
|
---|
588 |
|
---|
589 | #if LWIP_IPV6_SEND_ROUTER_SOLICIT
|
---|
590 | pNetif->rs_count = 0;
|
---|
591 | #endif
|
---|
592 | }
|
---|
593 |
|
---|
594 | LogFlowFunc(("LEAVE: %d\n", rcLwip));
|
---|
595 | return rcLwip;
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | err_t VBoxNetLwipNAT::netifLinkoutput(netif *pNetif, pbuf *pPBuf)
|
---|
600 | {
|
---|
601 | AssertPtrReturn(pNetif, ERR_ARG);
|
---|
602 | AssertPtrReturn(pPBuf, ERR_ARG);
|
---|
603 |
|
---|
604 | VBoxNetLwipNAT *self = static_cast<VBoxNetLwipNAT *>(pNetif->state);
|
---|
605 | AssertPtrReturn(self, ERR_IF);
|
---|
606 | AssertReturn(self == g_pLwipNat, ERR_ARG);
|
---|
607 |
|
---|
608 | LogFlowFunc(("ENTER: pNetif[%c%c%d], pPbuf:%p\n",
|
---|
609 | pNetif->name[0],
|
---|
610 | pNetif->name[1],
|
---|
611 | pNetif->num,
|
---|
612 | pPBuf));
|
---|
613 |
|
---|
614 | RT_ZERO(VBoxNetLwipNAT::aXmitSeg);
|
---|
615 |
|
---|
616 | size_t idx = 0;
|
---|
617 | for (struct pbuf *q = pPBuf; q != NULL; q = q->next, ++idx)
|
---|
618 | {
|
---|
619 | AssertReturn(idx < RT_ELEMENTS(VBoxNetLwipNAT::aXmitSeg), ERR_MEM);
|
---|
620 |
|
---|
621 | #if ETH_PAD_SIZE
|
---|
622 | if (q == pPBuf)
|
---|
623 | {
|
---|
624 | VBoxNetLwipNAT::aXmitSeg[idx].pv = (uint8_t *)q->payload + ETH_PAD_SIZE;
|
---|
625 | VBoxNetLwipNAT::aXmitSeg[idx].cb = q->len - ETH_PAD_SIZE;
|
---|
626 | }
|
---|
627 | else
|
---|
628 | #endif
|
---|
629 | {
|
---|
630 | VBoxNetLwipNAT::aXmitSeg[idx].pv = q->payload;
|
---|
631 | VBoxNetLwipNAT::aXmitSeg[idx].cb = q->len;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | int rc = self->sendBufferOnWire(VBoxNetLwipNAT::aXmitSeg, idx,
|
---|
636 | pPBuf->tot_len - ETH_PAD_SIZE);
|
---|
637 | AssertRCReturn(rc, ERR_IF);
|
---|
638 |
|
---|
639 | self->flushWire();
|
---|
640 |
|
---|
641 | LogFlowFunc(("LEAVE: %d\n", ERR_OK));
|
---|
642 | return ERR_OK;
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | VBoxNetLwipNAT::VBoxNetLwipNAT(SOCKET icmpsock4, SOCKET icmpsock6) : VBoxNetBaseService("VBoxNetNAT", "nat-network")
|
---|
647 | {
|
---|
648 | LogFlowFuncEnter();
|
---|
649 |
|
---|
650 | m_ProxyOptions.ipv6_enabled = 0;
|
---|
651 | m_ProxyOptions.ipv6_defroute = 0;
|
---|
652 | m_ProxyOptions.icmpsock4 = icmpsock4;
|
---|
653 | m_ProxyOptions.icmpsock6 = icmpsock6;
|
---|
654 | m_ProxyOptions.tftp_root = NULL;
|
---|
655 | m_ProxyOptions.src4 = NULL;
|
---|
656 | m_ProxyOptions.src6 = NULL;
|
---|
657 | RT_ZERO(m_src4);
|
---|
658 | RT_ZERO(m_src6);
|
---|
659 | m_src4.sin_family = AF_INET;
|
---|
660 | m_src6.sin6_family = AF_INET6;
|
---|
661 | #if HAVE_SA_LEN
|
---|
662 | m_src4.sin_len = sizeof(m_src4);
|
---|
663 | m_src6.sin6_len = sizeof(m_src6);
|
---|
664 | #endif
|
---|
665 | m_ProxyOptions.nameservers = NULL;
|
---|
666 |
|
---|
667 | m_LwipNetIf.name[0] = 'N';
|
---|
668 | m_LwipNetIf.name[1] = 'T';
|
---|
669 |
|
---|
670 | RTMAC mac;
|
---|
671 | mac.au8[0] = 0x52;
|
---|
672 | mac.au8[1] = 0x54;
|
---|
673 | mac.au8[2] = 0;
|
---|
674 | mac.au8[3] = 0x12;
|
---|
675 | mac.au8[4] = 0x35;
|
---|
676 | mac.au8[5] = 0;
|
---|
677 | setMacAddress(mac);
|
---|
678 |
|
---|
679 | RTNETADDRIPV4 address;
|
---|
680 | address.u = RT_MAKE_U32_FROM_U8( 10, 0, 2, 2); // NB: big-endian
|
---|
681 | setIpv4Address(address);
|
---|
682 |
|
---|
683 | address.u = RT_H2N_U32_C(0xffffff00);
|
---|
684 | setIpv4Netmask(address);
|
---|
685 |
|
---|
686 | fDontLoadRulesOnStartup = false;
|
---|
687 |
|
---|
688 | for(unsigned int i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i)
|
---|
689 | addCommandLineOption(&g_aGetOptDef[i]);
|
---|
690 |
|
---|
691 | LogFlowFuncLeave();
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | VBoxNetLwipNAT::~VBoxNetLwipNAT()
|
---|
696 | {
|
---|
697 | if (m_ProxyOptions.tftp_root != NULL)
|
---|
698 | {
|
---|
699 | RTStrFree((char *)m_ProxyOptions.tftp_root);
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 |
|
---|
704 | int VBoxNetLwipNAT::natServicePfRegister(NATSEVICEPORTFORWARDRULE& natPf)
|
---|
705 | {
|
---|
706 | int lrc;
|
---|
707 |
|
---|
708 | int sockFamily = (natPf.Pfr.fPfrIPv6 ? PF_INET6 : PF_INET);
|
---|
709 | int socketSpec;
|
---|
710 | switch(natPf.Pfr.iPfrProto)
|
---|
711 | {
|
---|
712 | case IPPROTO_TCP:
|
---|
713 | socketSpec = SOCK_STREAM;
|
---|
714 | break;
|
---|
715 | case IPPROTO_UDP:
|
---|
716 | socketSpec = SOCK_DGRAM;
|
---|
717 | break;
|
---|
718 | default:
|
---|
719 | return VERR_IGNORED;
|
---|
720 | }
|
---|
721 |
|
---|
722 | const char *pszHostAddr = natPf.Pfr.szPfrHostAddr;
|
---|
723 | if (pszHostAddr[0] == '\0')
|
---|
724 | {
|
---|
725 | if (sockFamily == PF_INET)
|
---|
726 | pszHostAddr = "0.0.0.0";
|
---|
727 | else
|
---|
728 | pszHostAddr = "::";
|
---|
729 | }
|
---|
730 |
|
---|
731 | lrc = fwspec_set(&natPf.FWSpec,
|
---|
732 | sockFamily,
|
---|
733 | socketSpec,
|
---|
734 | pszHostAddr,
|
---|
735 | natPf.Pfr.u16PfrHostPort,
|
---|
736 | natPf.Pfr.szPfrGuestAddr,
|
---|
737 | natPf.Pfr.u16PfrGuestPort);
|
---|
738 | if (lrc != 0)
|
---|
739 | return VERR_IGNORED;
|
---|
740 |
|
---|
741 | RTCMemAutoPtr<fwspec> pFwCopy;
|
---|
742 | if (RT_UNLIKELY(!pFwCopy.alloc()))
|
---|
743 | {
|
---|
744 | LogRel(("Unable to allocate memory for %s rule \"%s\"\n",
|
---|
745 | natPf.Pfr.fPfrIPv6 ? "IPv6" : "IPv4",
|
---|
746 | natPf.Pfr.szPfrName));
|
---|
747 | return VERR_IGNORED;
|
---|
748 | }
|
---|
749 |
|
---|
750 | memcpy(pFwCopy.get(), &natPf.FWSpec, sizeof(natPf.FWSpec));
|
---|
751 |
|
---|
752 | lrc = portfwd_rule_add(pFwCopy.get());
|
---|
753 | if (lrc != 0)
|
---|
754 | return VERR_IGNORED;
|
---|
755 |
|
---|
756 | pFwCopy.release(); /* owned by lwip thread now */
|
---|
757 | return VINF_SUCCESS;
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | int VBoxNetLwipNAT::natServiceProcessRegisteredPf(VECNATSERVICEPF& vecRules)
|
---|
762 | {
|
---|
763 | ITERATORNATSERVICEPF it;
|
---|
764 | for (it = vecRules.begin(); it != vecRules.end(); ++it)
|
---|
765 | {
|
---|
766 | NATSEVICEPORTFORWARDRULE &natPf = *it;
|
---|
767 |
|
---|
768 | LogRel(("Loading %s port-forwarding rule \"%s\": %s %s%s%s:%d -> %s%s%s:%d\n",
|
---|
769 | natPf.Pfr.fPfrIPv6 ? "IPv6" : "IPv4",
|
---|
770 | natPf.Pfr.szPfrName,
|
---|
771 | natPf.Pfr.iPfrProto == IPPROTO_TCP ? "TCP" : "UDP",
|
---|
772 | /* from */
|
---|
773 | natPf.Pfr.fPfrIPv6 ? "[" : "",
|
---|
774 | natPf.Pfr.szPfrHostAddr,
|
---|
775 | natPf.Pfr.fPfrIPv6 ? "]" : "",
|
---|
776 | natPf.Pfr.u16PfrHostPort,
|
---|
777 | /* to */
|
---|
778 | natPf.Pfr.fPfrIPv6 ? "[" : "",
|
---|
779 | natPf.Pfr.szPfrGuestAddr,
|
---|
780 | natPf.Pfr.fPfrIPv6 ? "]" : "",
|
---|
781 | natPf.Pfr.u16PfrGuestPort));
|
---|
782 |
|
---|
783 | natServicePfRegister(natPf);
|
---|
784 | }
|
---|
785 |
|
---|
786 | return VINF_SUCCESS;
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Main thread. Starts also the LWIP thread.
|
---|
792 | */
|
---|
793 | int VBoxNetLwipNAT::init()
|
---|
794 | {
|
---|
795 | LogFlowFuncEnter();
|
---|
796 |
|
---|
797 | /* virtualbox initialized in super class */
|
---|
798 | int rc = ::VBoxNetBaseService::init();
|
---|
799 | AssertRCReturn(rc, rc);
|
---|
800 |
|
---|
801 | std::string networkName = getNetworkName();
|
---|
802 | rc = findNatNetwork(virtualbox, networkName, m_net);
|
---|
803 | AssertRCReturn(rc, rc);
|
---|
804 |
|
---|
805 | {
|
---|
806 | ComEventTypeArray eventTypes;
|
---|
807 | eventTypes.push_back(VBoxEventType_OnNATNetworkPortForward);
|
---|
808 | eventTypes.push_back(VBoxEventType_OnNATNetworkSetting);
|
---|
809 | rc = createNatListener(m_NatListener, virtualbox, this, eventTypes);
|
---|
810 | AssertRCReturn(rc, rc);
|
---|
811 | }
|
---|
812 |
|
---|
813 |
|
---|
814 | // resolver changes are reported on vbox but are retrieved from
|
---|
815 | // host so stash a pointer for future lookups
|
---|
816 | HRESULT hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam());
|
---|
817 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
818 |
|
---|
819 | {
|
---|
820 | ComEventTypeArray eventTypes;
|
---|
821 | eventTypes.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
|
---|
822 | eventTypes.push_back(VBoxEventType_OnNATNetworkStartStop);
|
---|
823 | rc = createNatListener(m_VBoxListener, virtualbox, this, eventTypes);
|
---|
824 | AssertRCReturn(rc, rc);
|
---|
825 | }
|
---|
826 |
|
---|
827 | {
|
---|
828 | ComEventTypeArray eventTypes;
|
---|
829 | eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
|
---|
830 | rc = createClientListener(m_VBoxClientListener, virtualboxClient, this, eventTypes);
|
---|
831 | AssertRCReturn(rc, rc);
|
---|
832 | }
|
---|
833 |
|
---|
834 | BOOL fIPv6Enabled = FALSE;
|
---|
835 | hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
|
---|
836 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
837 |
|
---|
838 | BOOL fIPv6DefaultRoute = FALSE;
|
---|
839 | if (fIPv6Enabled)
|
---|
840 | {
|
---|
841 | hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
|
---|
842 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
843 | }
|
---|
844 |
|
---|
845 | m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
|
---|
846 | m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
|
---|
847 |
|
---|
848 |
|
---|
849 | com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4", networkName.c_str());
|
---|
850 | com::Bstr bstrSourceIpX;
|
---|
851 | hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
|
---|
852 | if (SUCCEEDED(hrc))
|
---|
853 | {
|
---|
854 | RTNETADDRIPV4 addr;
|
---|
855 | rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
|
---|
856 | if (RT_SUCCESS(rc))
|
---|
857 | {
|
---|
858 | RT_ZERO(m_src4);
|
---|
859 |
|
---|
860 | m_src4.sin_addr.s_addr = addr.u;
|
---|
861 | m_ProxyOptions.src4 = &m_src4;
|
---|
862 |
|
---|
863 | bstrSourceIpX.setNull();
|
---|
864 | }
|
---|
865 | }
|
---|
866 |
|
---|
867 | if (!fDontLoadRulesOnStartup)
|
---|
868 | {
|
---|
869 | fetchNatPortForwardRules(m_net, false, m_vecPortForwardRule4);
|
---|
870 | fetchNatPortForwardRules(m_net, true, m_vecPortForwardRule6);
|
---|
871 | } /* if (!fDontLoadRulesOnStartup) */
|
---|
872 |
|
---|
873 | AddressToOffsetMapping tmp;
|
---|
874 | rc = localMappings(m_net, tmp);
|
---|
875 | if (RT_SUCCESS(rc) && tmp.size() != 0)
|
---|
876 | {
|
---|
877 | unsigned long i = 0;
|
---|
878 | for (AddressToOffsetMapping::iterator it = tmp.begin();
|
---|
879 | it != tmp.end() && i < RT_ELEMENTS(m_lo2off);
|
---|
880 | ++it, ++i)
|
---|
881 | {
|
---|
882 | ip4_addr_set_u32(&m_lo2off[i].loaddr, it->first.u);
|
---|
883 | m_lo2off[i].off = it->second;
|
---|
884 | }
|
---|
885 |
|
---|
886 | m_loOptDescriptor.lomap = m_lo2off;
|
---|
887 | m_loOptDescriptor.num_lomap = i;
|
---|
888 | m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
|
---|
889 | }
|
---|
890 |
|
---|
891 | com::Bstr bstr;
|
---|
892 | hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
|
---|
893 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
894 | if (!bstr.isEmpty())
|
---|
895 | {
|
---|
896 | com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
|
---|
897 | bstr.raw(), RTPATH_DELIMITER, "TFTP"));
|
---|
898 | char *pszStrTemp; // avoid const char ** vs char **
|
---|
899 | rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
|
---|
900 | AssertRC(rc);
|
---|
901 | m_ProxyOptions.tftp_root = pszStrTemp;
|
---|
902 | }
|
---|
903 |
|
---|
904 | m_ProxyOptions.nameservers = getHostNameservers();
|
---|
905 |
|
---|
906 | /* end of COM initialization */
|
---|
907 |
|
---|
908 | rc = g_pLwipNat->tryGoOnline();
|
---|
909 | if (RT_FAILURE(rc))
|
---|
910 | return rc;
|
---|
911 |
|
---|
912 | /* this starts LWIP thread */
|
---|
913 | vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
|
---|
914 |
|
---|
915 | LogFlowFuncLeaveRC(rc);
|
---|
916 | return rc;
|
---|
917 | }
|
---|
918 |
|
---|
919 |
|
---|
920 | const char **VBoxNetLwipNAT::getHostNameservers()
|
---|
921 | {
|
---|
922 | if (m_host.isNull())
|
---|
923 | return NULL;
|
---|
924 |
|
---|
925 | com::SafeArray<BSTR> aNameServers;
|
---|
926 | HRESULT hrc = m_host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(aNameServers));
|
---|
927 | if (FAILED(hrc))
|
---|
928 | return NULL;
|
---|
929 |
|
---|
930 | const size_t cNameServers = aNameServers.size();
|
---|
931 | if (cNameServers == 0)
|
---|
932 | return NULL;
|
---|
933 |
|
---|
934 | const char **ppcszNameServers =
|
---|
935 | (const char **)RTMemAllocZ(sizeof(char *) * (cNameServers + 1));
|
---|
936 | if (ppcszNameServers == NULL)
|
---|
937 | return NULL;
|
---|
938 |
|
---|
939 | size_t idxLast = 0;
|
---|
940 | for (size_t i = 0; i < cNameServers; ++i)
|
---|
941 | {
|
---|
942 | com::Utf8Str strNameServer(aNameServers[i]);
|
---|
943 | ppcszNameServers[idxLast] = RTStrDup(strNameServer.c_str());
|
---|
944 | if (ppcszNameServers[idxLast] != NULL)
|
---|
945 | ++idxLast;
|
---|
946 | }
|
---|
947 |
|
---|
948 | if (idxLast == 0)
|
---|
949 | {
|
---|
950 | RTMemFree(ppcszNameServers);
|
---|
951 | return NULL;
|
---|
952 | }
|
---|
953 |
|
---|
954 | return ppcszNameServers;
|
---|
955 | }
|
---|
956 |
|
---|
957 |
|
---|
958 | int VBoxNetLwipNAT::parseOpt(int rc, const RTGETOPTUNION& Val)
|
---|
959 | {
|
---|
960 | switch (rc)
|
---|
961 | {
|
---|
962 | case 'p':
|
---|
963 | case 'P':
|
---|
964 | {
|
---|
965 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
966 | VECNATSERVICEPF& rules = (rc == 'P'?
|
---|
967 | m_vecPortForwardRule6
|
---|
968 | : m_vecPortForwardRule4);
|
---|
969 |
|
---|
970 | fDontLoadRulesOnStartup = true;
|
---|
971 |
|
---|
972 | RT_ZERO(Rule);
|
---|
973 |
|
---|
974 | int irc = netPfStrToPf(Val.psz, (rc == 'P'), &Rule.Pfr);
|
---|
975 | rules.push_back(Rule);
|
---|
976 | return VINF_SUCCESS;
|
---|
977 | }
|
---|
978 | default:;
|
---|
979 | }
|
---|
980 | return VERR_NOT_FOUND;
|
---|
981 | }
|
---|
982 |
|
---|
983 |
|
---|
984 | int VBoxNetLwipNAT::processFrame(void *pvFrame, size_t cbFrame)
|
---|
985 | {
|
---|
986 | AssertPtrReturn(pvFrame, VERR_INVALID_PARAMETER);
|
---|
987 | AssertReturn(cbFrame != 0, VERR_INVALID_PARAMETER);
|
---|
988 |
|
---|
989 | struct pbuf *p = pbuf_alloc(PBUF_RAW, cbFrame + ETH_PAD_SIZE, PBUF_POOL);
|
---|
990 | if (RT_UNLIKELY(p == NULL))
|
---|
991 | return VERR_NO_MEMORY;
|
---|
992 |
|
---|
993 | /*
|
---|
994 | * The code below is inlined version of:
|
---|
995 | *
|
---|
996 | * pbuf_header(p, -ETH_PAD_SIZE); // hide padding
|
---|
997 | * pbuf_take(p, pvFrame, cbFrame);
|
---|
998 | * pbuf_header(p, ETH_PAD_SIZE); // reveal padding
|
---|
999 | */
|
---|
1000 | struct pbuf *q = p;
|
---|
1001 | uint8_t *pu8Chunk = (uint8_t *)pvFrame;
|
---|
1002 | do {
|
---|
1003 | uint8_t *payload = (uint8_t *)q->payload;
|
---|
1004 | size_t len = q->len;
|
---|
1005 |
|
---|
1006 | #if ETH_PAD_SIZE
|
---|
1007 | if (RT_LIKELY(q == p)) // single pbuf is large enough
|
---|
1008 | {
|
---|
1009 | payload += ETH_PAD_SIZE;
|
---|
1010 | len -= ETH_PAD_SIZE;
|
---|
1011 | }
|
---|
1012 | #endif
|
---|
1013 | memcpy(payload, pu8Chunk, len);
|
---|
1014 | pu8Chunk += len;
|
---|
1015 | q = q->next;
|
---|
1016 | } while (RT_UNLIKELY(q != NULL));
|
---|
1017 |
|
---|
1018 | m_LwipNetIf.input(p, &m_LwipNetIf);
|
---|
1019 | return VINF_SUCCESS;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 |
|
---|
1023 | int VBoxNetLwipNAT::processGSO(PCPDMNETWORKGSO pGso, size_t cbFrame)
|
---|
1024 | {
|
---|
1025 | if (!PDMNetGsoIsValid(pGso, cbFrame,
|
---|
1026 | cbFrame - sizeof(PDMNETWORKGSO)))
|
---|
1027 | return VERR_INVALID_PARAMETER;
|
---|
1028 |
|
---|
1029 | cbFrame -= sizeof(PDMNETWORKGSO);
|
---|
1030 | uint8_t abHdrScratch[256];
|
---|
1031 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso,
|
---|
1032 | cbFrame);
|
---|
1033 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
1034 | {
|
---|
1035 | uint32_t cbSegFrame;
|
---|
1036 | void *pvSegFrame =
|
---|
1037 | PDMNetGsoCarveSegmentQD(pGso,
|
---|
1038 | (uint8_t *)(pGso + 1),
|
---|
1039 | cbFrame,
|
---|
1040 | abHdrScratch,
|
---|
1041 | iSeg,
|
---|
1042 | cSegs,
|
---|
1043 | &cbSegFrame);
|
---|
1044 |
|
---|
1045 | int rc = processFrame(pvSegFrame, cbSegFrame);
|
---|
1046 | if (RT_FAILURE(rc))
|
---|
1047 | {
|
---|
1048 | return rc;
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | return VINF_SUCCESS;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | int VBoxNetLwipNAT::run()
|
---|
1057 | {
|
---|
1058 | /* Father starts receiving thread and enter event loop. */
|
---|
1059 | VBoxNetBaseService::run();
|
---|
1060 |
|
---|
1061 | vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
|
---|
1062 |
|
---|
1063 | m_vecPortForwardRule4.clear();
|
---|
1064 | m_vecPortForwardRule6.clear();
|
---|
1065 |
|
---|
1066 | destroyNatListener(m_NatListener, virtualbox);
|
---|
1067 | destroyNatListener(m_VBoxListener, virtualbox);
|
---|
1068 | destroyClientListener(m_VBoxClientListener, virtualboxClient);
|
---|
1069 |
|
---|
1070 | return VINF_SUCCESS;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 |
|
---|
1074 | /**
|
---|
1075 | * Entry point.
|
---|
1076 | */
|
---|
1077 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
1078 | {
|
---|
1079 | int rc;
|
---|
1080 |
|
---|
1081 | LogFlowFuncEnter();
|
---|
1082 |
|
---|
1083 | NOREF(envp);
|
---|
1084 |
|
---|
1085 | #ifdef RT_OS_WINDOWS
|
---|
1086 | WSADATA wsaData;
|
---|
1087 | int err;
|
---|
1088 |
|
---|
1089 | err = WSAStartup(MAKEWORD(2,2), &wsaData);
|
---|
1090 | if (err)
|
---|
1091 | {
|
---|
1092 | fprintf(stderr, "wsastartup: failed (%d)\n", err);
|
---|
1093 | return 1;
|
---|
1094 | }
|
---|
1095 | #endif
|
---|
1096 |
|
---|
1097 | SOCKET icmpsock4 = INVALID_SOCKET;
|
---|
1098 | SOCKET icmpsock6 = INVALID_SOCKET;
|
---|
1099 | #ifndef RT_OS_DARWIN
|
---|
1100 | const int icmpstype = SOCK_RAW;
|
---|
1101 | #else
|
---|
1102 | /* on OS X it's not privileged */
|
---|
1103 | const int icmpstype = SOCK_DGRAM;
|
---|
1104 | #endif
|
---|
1105 |
|
---|
1106 | icmpsock4 = socket(AF_INET, icmpstype, IPPROTO_ICMP);
|
---|
1107 | if (icmpsock4 == INVALID_SOCKET)
|
---|
1108 | {
|
---|
1109 | perror("IPPROTO_ICMP");
|
---|
1110 | #ifdef VBOX_RAWSOCK_DEBUG_HELPER
|
---|
1111 | icmpsock4 = getrawsock(AF_INET);
|
---|
1112 | #endif
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | if (icmpsock4 != INVALID_SOCKET)
|
---|
1116 | {
|
---|
1117 | #ifdef ICMP_FILTER // Linux specific
|
---|
1118 | struct icmp_filter flt = {
|
---|
1119 | ~(uint32_t)(
|
---|
1120 | (1U << ICMP_ECHOREPLY)
|
---|
1121 | | (1U << ICMP_DEST_UNREACH)
|
---|
1122 | | (1U << ICMP_TIME_EXCEEDED)
|
---|
1123 | )
|
---|
1124 | };
|
---|
1125 |
|
---|
1126 | int status = setsockopt(icmpsock4, SOL_RAW, ICMP_FILTER,
|
---|
1127 | &flt, sizeof(flt));
|
---|
1128 | if (status < 0)
|
---|
1129 | {
|
---|
1130 | perror("ICMP_FILTER");
|
---|
1131 | }
|
---|
1132 | #endif
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | icmpsock6 = socket(AF_INET6, icmpstype, IPPROTO_ICMPV6);
|
---|
1136 | if (icmpsock6 == INVALID_SOCKET)
|
---|
1137 | {
|
---|
1138 | perror("IPPROTO_ICMPV6");
|
---|
1139 | #ifdef VBOX_RAWSOCK_DEBUG_HELPER
|
---|
1140 | icmpsock6 = getrawsock(AF_INET6);
|
---|
1141 | #endif
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | if (icmpsock6 != INVALID_SOCKET)
|
---|
1145 | {
|
---|
1146 | #ifdef ICMP6_FILTER // Windows doesn't support RFC 3542 API
|
---|
1147 | /*
|
---|
1148 | * XXX: We do this here for now, not in pxping.c, to avoid
|
---|
1149 | * name clashes between lwIP and system headers.
|
---|
1150 | */
|
---|
1151 | struct icmp6_filter flt;
|
---|
1152 | ICMP6_FILTER_SETBLOCKALL(&flt);
|
---|
1153 |
|
---|
1154 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &flt);
|
---|
1155 |
|
---|
1156 | ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &flt);
|
---|
1157 | ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &flt);
|
---|
1158 | ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &flt);
|
---|
1159 | ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &flt);
|
---|
1160 |
|
---|
1161 | int status = setsockopt(icmpsock6, IPPROTO_ICMPV6, ICMP6_FILTER,
|
---|
1162 | &flt, sizeof(flt));
|
---|
1163 | if (status < 0)
|
---|
1164 | {
|
---|
1165 | perror("ICMP6_FILTER");
|
---|
1166 | }
|
---|
1167 | #endif
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | HRESULT hrc = com::Initialize();
|
---|
1171 | if (FAILED(hrc))
|
---|
1172 | {
|
---|
1173 | #ifdef VBOX_WITH_XPCOM
|
---|
1174 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
1175 | {
|
---|
1176 | char szHome[RTPATH_MAX] = "";
|
---|
1177 | int vrc = com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome), false);
|
---|
1178 | if (RT_SUCCESS(vrc))
|
---|
1179 | {
|
---|
1180 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
1181 | "Failed to initialize COM: %s: %Rhrf",
|
---|
1182 | szHome, hrc);
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | #endif // VBOX_WITH_XPCOM
|
---|
1186 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
1187 | "Failed to initialize COM: %Rhrf", hrc);
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | rc = vboxNetNATLogInit(argc, argv);
|
---|
1191 | // shall we bail if we failed to init logging?
|
---|
1192 |
|
---|
1193 | g_pLwipNat = new VBoxNetLwipNAT(icmpsock4, icmpsock6);
|
---|
1194 |
|
---|
1195 | Log2(("NAT: initialization\n"));
|
---|
1196 | rc = g_pLwipNat->parseArgs(argc - 1, argv + 1);
|
---|
1197 | rc = (rc == 0) ? VINF_SUCCESS : VERR_GENERAL_FAILURE; /* XXX: FIXME */
|
---|
1198 |
|
---|
1199 | if (RT_SUCCESS(rc))
|
---|
1200 | rc = g_pLwipNat->init();
|
---|
1201 |
|
---|
1202 | if (RT_SUCCESS(rc))
|
---|
1203 | g_pLwipNat->run();
|
---|
1204 |
|
---|
1205 | delete g_pLwipNat;
|
---|
1206 | return 0;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | static int vboxNetNATLogInit(int argc, char **argv)
|
---|
1211 | {
|
---|
1212 | size_t cch;
|
---|
1213 | int rc;
|
---|
1214 |
|
---|
1215 | char szHome[RTPATH_MAX];
|
---|
1216 | rc = com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome), false);
|
---|
1217 | if (RT_FAILURE(rc))
|
---|
1218 | return rc;
|
---|
1219 |
|
---|
1220 | const char *pcszNetwork = NULL;
|
---|
1221 |
|
---|
1222 | // XXX: This duplicates information from VBoxNetBaseService.cpp.
|
---|
1223 | // Perhaps option definitions should be exported as public static
|
---|
1224 | // member of VBoxNetBaseService?
|
---|
1225 | static const RTGETOPTDEF s_aOptions[] = {
|
---|
1226 | { "--network", 'n', RTGETOPT_REQ_STRING }
|
---|
1227 | };
|
---|
1228 |
|
---|
1229 | RTGETOPTSTATE GetState;
|
---|
1230 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
1231 | RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
1232 |
|
---|
1233 | RTGETOPTUNION ValueUnion;
|
---|
1234 | int ch;
|
---|
1235 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1236 | {
|
---|
1237 | if (ch == 'n')
|
---|
1238 | {
|
---|
1239 | pcszNetwork = ValueUnion.psz;
|
---|
1240 | break;
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | if (pcszNetwork == NULL)
|
---|
1245 | return VERR_MISSING;
|
---|
1246 |
|
---|
1247 | char szNetwork[RTPATH_MAX];
|
---|
1248 | rc = RTStrCopy(szNetwork, sizeof(szNetwork), pcszNetwork);
|
---|
1249 | if (RT_FAILURE(rc))
|
---|
1250 | return rc;
|
---|
1251 |
|
---|
1252 | // sanitize network name to be usable as a path component
|
---|
1253 | for (char *p = szNetwork; *p != '\0'; ++p)
|
---|
1254 | {
|
---|
1255 | if (RTPATH_IS_SEP(*p))
|
---|
1256 | *p = '_';
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | char szLogFile[RTPATH_MAX];
|
---|
1260 | cch = RTStrPrintf(szLogFile, sizeof(szLogFile),
|
---|
1261 | "%s%c%s.log", szHome, RTPATH_DELIMITER, szNetwork);
|
---|
1262 | if (cch >= sizeof(szLogFile))
|
---|
1263 | {
|
---|
1264 | return VERR_BUFFER_OVERFLOW;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | // sanitize network name some more to be usable as environment variable
|
---|
1268 | for (char *p = szNetwork; *p != '\0'; ++p)
|
---|
1269 | {
|
---|
1270 | if (*p != '_'
|
---|
1271 | && (*p < '0' || '9' < *p)
|
---|
1272 | && (*p < 'a' || 'z' < *p)
|
---|
1273 | && (*p < 'A' || 'Z' < *p))
|
---|
1274 | {
|
---|
1275 | *p = '_';
|
---|
1276 | }
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | char szEnvVarBase[128];
|
---|
1280 | cch = RTStrPrintf(szEnvVarBase, sizeof(szEnvVarBase),
|
---|
1281 | "VBOXNET_%s_RELEASE_LOG", szNetwork);
|
---|
1282 | if (cch >= sizeof(szEnvVarBase))
|
---|
1283 | return VERR_BUFFER_OVERFLOW;
|
---|
1284 |
|
---|
1285 | char szError[RTPATH_MAX + 128];
|
---|
1286 | rc = com::VBoxLogRelCreate("NAT Network",
|
---|
1287 | szLogFile,
|
---|
1288 | RTLOGFLAGS_PREFIX_TIME_PROG,
|
---|
1289 | "all all.restrict -default.restrict",
|
---|
1290 | szEnvVarBase,
|
---|
1291 | RTLOGDEST_FILE,
|
---|
1292 | 32768 /* cMaxEntriesPerGroup */,
|
---|
1293 | 0 /* cHistory */,
|
---|
1294 | 0 /* uHistoryFileTime */,
|
---|
1295 | 0 /* uHistoryFileSize */,
|
---|
1296 | szError, sizeof(szError));
|
---|
1297 | return rc;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 |
|
---|
1301 | static int fetchNatPortForwardRules(const ComNatPtr& nat, bool fIsIPv6, VECNATSERVICEPF& vec)
|
---|
1302 | {
|
---|
1303 | HRESULT hrc;
|
---|
1304 | com::SafeArray<BSTR> rules;
|
---|
1305 | if (fIsIPv6)
|
---|
1306 | hrc = nat->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
|
---|
1307 | else
|
---|
1308 | hrc = nat->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
|
---|
1309 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
1310 |
|
---|
1311 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
1312 | for (size_t idxRules = 0; idxRules < rules.size(); ++idxRules)
|
---|
1313 | {
|
---|
1314 | Log(("%d-%s rule: %ls\n", idxRules, (fIsIPv6 ? "IPv6" : "IPv4"), rules[idxRules]));
|
---|
1315 | RT_ZERO(Rule);
|
---|
1316 |
|
---|
1317 | int rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(),
|
---|
1318 | fIsIPv6, &Rule.Pfr);
|
---|
1319 | if (RT_FAILURE(rc))
|
---|
1320 | continue;
|
---|
1321 |
|
---|
1322 | vec.push_back(Rule);
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | return VINF_SUCCESS;
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 |
|
---|
1329 | #ifndef VBOX_WITH_HARDENING
|
---|
1330 |
|
---|
1331 | int main(int argc, char **argv, char **envp)
|
---|
1332 | {
|
---|
1333 | int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
1334 | if (RT_FAILURE(rc))
|
---|
1335 | return RTMsgInitFailure(rc);
|
---|
1336 |
|
---|
1337 | return TrustedMain(argc, argv, envp);
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | # if defined(RT_OS_WINDOWS)
|
---|
1341 |
|
---|
1342 | static LRESULT CALLBACK WindowProc(HWND hwnd,
|
---|
1343 | UINT uMsg,
|
---|
1344 | WPARAM wParam,
|
---|
1345 | LPARAM lParam
|
---|
1346 | )
|
---|
1347 | {
|
---|
1348 | if(uMsg == WM_DESTROY)
|
---|
1349 | {
|
---|
1350 | PostQuitMessage(0);
|
---|
1351 | return 0;
|
---|
1352 | }
|
---|
1353 | return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | static LPCWSTR g_WndClassName = L"VBoxNetNatLwipClass";
|
---|
1357 |
|
---|
1358 | static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
|
---|
1359 | {
|
---|
1360 | HWND hwnd = 0;
|
---|
1361 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
|
---|
1362 | bool bExit = false;
|
---|
1363 |
|
---|
1364 | /* Register the Window Class. */
|
---|
1365 | WNDCLASS wc;
|
---|
1366 | wc.style = 0;
|
---|
1367 | wc.lpfnWndProc = WindowProc;
|
---|
1368 | wc.cbClsExtra = 0;
|
---|
1369 | wc.cbWndExtra = sizeof(void *);
|
---|
1370 | wc.hInstance = hInstance;
|
---|
1371 | wc.hIcon = NULL;
|
---|
1372 | wc.hCursor = NULL;
|
---|
1373 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
1374 | wc.lpszMenuName = NULL;
|
---|
1375 | wc.lpszClassName = g_WndClassName;
|
---|
1376 |
|
---|
1377 | ATOM atomWindowClass = RegisterClass(&wc);
|
---|
1378 |
|
---|
1379 | if (atomWindowClass != 0)
|
---|
1380 | {
|
---|
1381 | /* Create the window. */
|
---|
1382 | hwnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
1383 | g_WndClassName, g_WndClassName, WS_POPUPWINDOW,
|
---|
1384 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
1385 |
|
---|
1386 | if (hwnd)
|
---|
1387 | {
|
---|
1388 | SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
1389 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
1390 |
|
---|
1391 | MSG msg;
|
---|
1392 | while (GetMessage(&msg, NULL, 0, 0))
|
---|
1393 | {
|
---|
1394 | TranslateMessage(&msg);
|
---|
1395 | DispatchMessage(&msg);
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | DestroyWindow (hwnd);
|
---|
1399 |
|
---|
1400 | bExit = true;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | UnregisterClass (g_WndClassName, hInstance);
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | if(bExit)
|
---|
1407 | {
|
---|
1408 | /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
|
---|
1409 | exit(0);
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | return 0;
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 |
|
---|
1416 |
|
---|
1417 | /** (We don't want a console usually.) */
|
---|
1418 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
1419 | {
|
---|
1420 | #if 0
|
---|
1421 | NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
|
---|
1422 |
|
---|
1423 | HANDLE hThread = CreateThread(
|
---|
1424 | NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
|
---|
1425 | 0, /*__in SIZE_T dwStackSize, */
|
---|
1426 | MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
|
---|
1427 | NULL, /*__in_opt LPVOID lpParameter,*/
|
---|
1428 | 0, /*__in DWORD dwCreationFlags,*/
|
---|
1429 | NULL /*__out_opt LPDWORD lpThreadId*/
|
---|
1430 | );
|
---|
1431 |
|
---|
1432 | if(hThread != NULL)
|
---|
1433 | CloseHandle(hThread);
|
---|
1434 |
|
---|
1435 | #endif
|
---|
1436 | return main(__argc, __argv, environ);
|
---|
1437 | }
|
---|
1438 | # endif /* RT_OS_WINDOWS */
|
---|
1439 |
|
---|
1440 | #endif /* !VBOX_WITH_HARDENING */
|
---|