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