1 | /* $Id: VBoxNetLwipNAT.cpp 49149 2013-10-17 03:53:24Z 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 | #include "winutils.h"
|
---|
19 |
|
---|
20 | #include <VBox/com/com.h>
|
---|
21 | #include <VBox/com/listeners.h>
|
---|
22 | #include <VBox/com/string.h>
|
---|
23 | #include <VBox/com/Guid.h>
|
---|
24 | #include <VBox/com/array.h>
|
---|
25 | #include <VBox/com/ErrorInfo.h>
|
---|
26 | #include <VBox/com/errorprint.h>
|
---|
27 | #include <VBox/com/VirtualBox.h>
|
---|
28 | #include <VBox/com/NativeEventQueue.h>
|
---|
29 |
|
---|
30 | #include <iprt/net.h>
|
---|
31 | #include <iprt/initterm.h>
|
---|
32 | #include <iprt/alloca.h>
|
---|
33 | #ifndef RT_OS_WINDOWS
|
---|
34 | # include <arpa/inet.h>
|
---|
35 | #endif
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/time.h>
|
---|
38 | #include <iprt/timer.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/path.h>
|
---|
42 | #include <iprt/param.h>
|
---|
43 | #include <iprt/pipe.h>
|
---|
44 | #include <iprt/getopt.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/mem.h>
|
---|
47 | #include <iprt/message.h>
|
---|
48 | #include <iprt/req.h>
|
---|
49 | #include <iprt/file.h>
|
---|
50 | #include <iprt/semaphore.h>
|
---|
51 | #include <iprt/cpp/utils.h>
|
---|
52 | #define LOG_GROUP LOG_GROUP_NAT_SERVICE
|
---|
53 | #include <VBox/log.h>
|
---|
54 |
|
---|
55 | #include <VBox/sup.h>
|
---|
56 | #include <VBox/intnet.h>
|
---|
57 | #include <VBox/intnetinline.h>
|
---|
58 | #include <VBox/vmm/pdmnetinline.h>
|
---|
59 | #include <VBox/vmm/vmm.h>
|
---|
60 | #include <VBox/version.h>
|
---|
61 |
|
---|
62 | #ifndef RT_OS_WINDOWS
|
---|
63 | # include <sys/poll.h>
|
---|
64 | # include <sys/socket.h>
|
---|
65 | # include <netinet/in.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #include <vector>
|
---|
69 | #include <string>
|
---|
70 |
|
---|
71 | #include "../NetLib/VBoxNetLib.h"
|
---|
72 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
73 | #include "VBoxLwipCore.h"
|
---|
74 |
|
---|
75 | extern "C"
|
---|
76 | {
|
---|
77 | /* bunch of LWIP headers */
|
---|
78 | #include "lwip/opt.h"
|
---|
79 | #ifdef LWIP_SOCKET
|
---|
80 | # undef LWIP_SOCKET
|
---|
81 | # define LWIP_SOCKET 0
|
---|
82 | #endif
|
---|
83 | #include "lwip/sys.h"
|
---|
84 | #include "lwip/stats.h"
|
---|
85 | #include "lwip/mem.h"
|
---|
86 | #include "lwip/memp.h"
|
---|
87 | #include "lwip/pbuf.h"
|
---|
88 | #include "lwip/netif.h"
|
---|
89 | #include "lwip/api.h"
|
---|
90 | #include "lwip/tcp_impl.h"
|
---|
91 | #include "ipv6/lwip/ethip6.h"
|
---|
92 | #include "lwip/nd6.h" // for proxy_na_hook
|
---|
93 | #include "lwip/mld6.h"
|
---|
94 | #include "lwip/udp.h"
|
---|
95 | #include "lwip/tcp.h"
|
---|
96 | #include "lwip/tcpip.h"
|
---|
97 | #include "lwip/sockets.h"
|
---|
98 | #include "netif/etharp.h"
|
---|
99 |
|
---|
100 | #include "proxy.h"
|
---|
101 | #include "pxremap.h"
|
---|
102 | #include "portfwd.h"
|
---|
103 | }
|
---|
104 |
|
---|
105 | #include "../NetLib/VBoxPortForwardString.h"
|
---|
106 |
|
---|
107 | static RTGETOPTDEF g_aGetOptDef[] =
|
---|
108 | {
|
---|
109 | { "--port-forward4", 'p', RTGETOPT_REQ_STRING },
|
---|
110 | { "--port-forward6", 'P', RTGETOPT_REQ_STRING }
|
---|
111 | };
|
---|
112 |
|
---|
113 | typedef struct NATSEVICEPORTFORWARDRULE
|
---|
114 | {
|
---|
115 | PORTFORWARDRULE Pfr;
|
---|
116 | fwspec FWSpec;
|
---|
117 | } NATSEVICEPORTFORWARDRULE, *PNATSEVICEPORTFORWARDRULE;
|
---|
118 |
|
---|
119 | typedef std::vector<NATSEVICEPORTFORWARDRULE> VECNATSERVICEPF;
|
---|
120 | typedef VECNATSERVICEPF::iterator ITERATORNATSERVICEPF;
|
---|
121 | typedef VECNATSERVICEPF::const_iterator CITERATORNATSERVICEPF;
|
---|
122 |
|
---|
123 |
|
---|
124 | class VBoxNetLwipNAT;
|
---|
125 |
|
---|
126 |
|
---|
127 | class NATNetworkListener
|
---|
128 | {
|
---|
129 | public:
|
---|
130 | NATNetworkListener():m_pNAT(NULL){}
|
---|
131 |
|
---|
132 | HRESULT init(VBoxNetLwipNAT *pNAT)
|
---|
133 | {
|
---|
134 | AssertPtrReturn(pNAT, E_INVALIDARG);
|
---|
135 |
|
---|
136 | m_pNAT = pNAT;
|
---|
137 | return S_OK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | HRESULT init()
|
---|
141 | {
|
---|
142 | m_pNAT = NULL;
|
---|
143 | return S_OK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | void uninit() { m_pNAT = NULL; }
|
---|
147 |
|
---|
148 | STDMETHOD(HandleEvent)(VBoxEventType_T aEventType, IEvent *pEvent);
|
---|
149 |
|
---|
150 | private:
|
---|
151 | VBoxNetLwipNAT *m_pNAT;
|
---|
152 | };
|
---|
153 | typedef ListenerImpl<NATNetworkListener, VBoxNetLwipNAT *> NATNetworkListenerImpl;
|
---|
154 | VBOX_LISTENER_DECLARE(NATNetworkListenerImpl)
|
---|
155 |
|
---|
156 |
|
---|
157 | class VBoxNetLwipNAT: public VBoxNetBaseService
|
---|
158 | {
|
---|
159 | friend class NATNetworkListener;
|
---|
160 | public:
|
---|
161 | VBoxNetLwipNAT();
|
---|
162 | virtual ~VBoxNetLwipNAT();
|
---|
163 | void usage(){ /* @todo: should be implemented */ };
|
---|
164 | int run();
|
---|
165 | virtual int init(void);
|
---|
166 | /* @todo: when configuration would be really needed */
|
---|
167 | virtual int parseOpt(int rc, const RTGETOPTUNION& getOptVal);
|
---|
168 |
|
---|
169 | private:
|
---|
170 | struct proxy_options m_ProxyOptions;
|
---|
171 | struct sockaddr_in m_src4;
|
---|
172 | struct sockaddr_in6 m_src6;
|
---|
173 | /**
|
---|
174 | * place for registered local interfaces.
|
---|
175 | */
|
---|
176 | ip4_lomap m_lo2off[10];
|
---|
177 | ip4_lomap_desc m_loOptDescriptor;
|
---|
178 |
|
---|
179 | uint16_t m_u16Mtu;
|
---|
180 | netif m_LwipNetIf;
|
---|
181 | /* Queues */
|
---|
182 | RTREQQUEUE hReqIntNet;
|
---|
183 | /* thread where we're waiting for a frames, no semaphores needed */
|
---|
184 | RTTHREAD hThrIntNetRecv;
|
---|
185 |
|
---|
186 | /* Our NAT network descriptor in Main */
|
---|
187 | ComPtr<INATNetwork> net;
|
---|
188 | ComPtr<NATNetworkListenerImpl> m_listener;
|
---|
189 | STDMETHOD(HandleEvent)(VBoxEventType_T aEventType, IEvent *pEvent);
|
---|
190 |
|
---|
191 | RTSEMEVENT hSemSVC;
|
---|
192 | /* Only for debug needs, by default NAT service should load rules from SVC
|
---|
193 | * on startup, and then on sync them on events.
|
---|
194 | */
|
---|
195 | bool fDontLoadRulesOnStartup;
|
---|
196 | static void onLwipTcpIpInit(void *arg);
|
---|
197 | static void onLwipTcpIpFini(void *arg);
|
---|
198 | static err_t netifInit(netif *pNetif);
|
---|
199 | static err_t netifLinkoutput(netif *pNetif, pbuf *pBuf);
|
---|
200 | static int intNetThreadRecv(RTTHREAD, void *);
|
---|
201 | static void vboxNetLwipNATProcessXmit(void);
|
---|
202 |
|
---|
203 | VECNATSERVICEPF m_vecPortForwardRule4;
|
---|
204 | VECNATSERVICEPF m_vecPortForwardRule6;
|
---|
205 |
|
---|
206 | static int natServicePfRegister(NATSEVICEPORTFORWARDRULE& natServicePf);
|
---|
207 | static int natServiceProcessRegisteredPf(VECNATSERVICEPF& vecPf);
|
---|
208 | };
|
---|
209 |
|
---|
210 |
|
---|
211 | static VBoxNetLwipNAT *g_pLwipNat;
|
---|
212 |
|
---|
213 | STDMETHODIMP NATNetworkListener::HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
|
---|
214 | {
|
---|
215 | if (m_pNAT)
|
---|
216 | return m_pNAT->HandleEvent(aEventType, pEvent);
|
---|
217 | else
|
---|
218 | return E_FAIL;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 |
|
---|
223 | STDMETHODIMP VBoxNetLwipNAT::HandleEvent(VBoxEventType_T aEventType,
|
---|
224 | IEvent *pEvent)
|
---|
225 | {
|
---|
226 | HRESULT hrc = S_OK;
|
---|
227 | switch (aEventType)
|
---|
228 | {
|
---|
229 | case VBoxEventType_OnNATNetworkSetting:
|
---|
230 | {
|
---|
231 | ComPtr<INATNetworkSettingEvent> evSettings(pEvent);
|
---|
232 | // XXX: only handle IPv6 default route for now
|
---|
233 |
|
---|
234 | if (!m_ProxyOptions.ipv6_enabled)
|
---|
235 | {
|
---|
236 | break;
|
---|
237 | }
|
---|
238 |
|
---|
239 | BOOL fIPv6DefaultRoute = FALSE;
|
---|
240 | hrc = evSettings->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
|
---|
241 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
242 |
|
---|
243 | if (m_ProxyOptions.ipv6_defroute == fIPv6DefaultRoute)
|
---|
244 | {
|
---|
245 | break;
|
---|
246 | }
|
---|
247 |
|
---|
248 | m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
|
---|
249 | tcpip_callback_with_block(proxy_rtadvd_do_quick, &m_LwipNetIf, 0);
|
---|
250 |
|
---|
251 | break;
|
---|
252 | }
|
---|
253 |
|
---|
254 | case VBoxEventType_OnNATNetworkPortForward:
|
---|
255 | {
|
---|
256 | com::Bstr name, strHostAddr, strGuestAddr;
|
---|
257 | LONG lHostPort, lGuestPort;
|
---|
258 | BOOL fCreateFW, fIPv6FW;
|
---|
259 | NATProtocol_T proto = NATProtocol_TCP;
|
---|
260 |
|
---|
261 |
|
---|
262 | ComPtr<INATNetworkPortForwardEvent> pfEvt = pEvent;
|
---|
263 |
|
---|
264 | hrc = pfEvt->COMGETTER(Name)(name.asOutParam());
|
---|
265 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
266 |
|
---|
267 | hrc = pfEvt->COMGETTER(Proto)(&proto);
|
---|
268 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
269 |
|
---|
270 | hrc = pfEvt->COMGETTER(HostIp)(strHostAddr.asOutParam());
|
---|
271 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
272 |
|
---|
273 | hrc = pfEvt->COMGETTER(HostPort)(&lHostPort);
|
---|
274 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
275 |
|
---|
276 | hrc = pfEvt->COMGETTER(GuestIp)(strGuestAddr.asOutParam());
|
---|
277 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
278 |
|
---|
279 | hrc = pfEvt->COMGETTER(GuestPort)(&lGuestPort);
|
---|
280 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
281 |
|
---|
282 | hrc = pfEvt->COMGETTER(Create)(&fCreateFW);
|
---|
283 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
284 |
|
---|
285 | hrc = pfEvt->COMGETTER(Ipv6)(&fIPv6FW);
|
---|
286 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
287 |
|
---|
288 | VECNATSERVICEPF& rules = (fIPv6FW ?
|
---|
289 | m_vecPortForwardRule6 :
|
---|
290 | m_vecPortForwardRule4);
|
---|
291 |
|
---|
292 | NATSEVICEPORTFORWARDRULE r;
|
---|
293 |
|
---|
294 | RT_ZERO(r);
|
---|
295 |
|
---|
296 | if (name.length() > sizeof(r.Pfr.szPfrName))
|
---|
297 | {
|
---|
298 | hrc = E_INVALIDARG;
|
---|
299 | goto port_forward_done;
|
---|
300 | }
|
---|
301 |
|
---|
302 | r.Pfr.fPfrIPv6 = fIPv6FW;
|
---|
303 |
|
---|
304 | switch (proto)
|
---|
305 | {
|
---|
306 | case NATProtocol_TCP:
|
---|
307 | r.Pfr.iPfrProto = IPPROTO_TCP;
|
---|
308 | break;
|
---|
309 | case NATProtocol_UDP:
|
---|
310 | r.Pfr.iPfrProto = IPPROTO_UDP;
|
---|
311 | break;
|
---|
312 | default:
|
---|
313 | goto port_forward_done;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | RTStrPrintf(r.Pfr.szPfrName, sizeof(r.Pfr.szPfrName),
|
---|
318 | "%s", com::Utf8Str(name).c_str());
|
---|
319 |
|
---|
320 | RTStrPrintf(r.Pfr.szPfrHostAddr, sizeof(r.Pfr.szPfrHostAddr),
|
---|
321 | "%s", com::Utf8Str(strHostAddr).c_str());
|
---|
322 |
|
---|
323 | /* XXX: limits should be checked */
|
---|
324 | r.Pfr.u16PfrHostPort = (uint16_t)lHostPort;
|
---|
325 |
|
---|
326 | RTStrPrintf(r.Pfr.szPfrGuestAddr, sizeof(r.Pfr.szPfrGuestAddr),
|
---|
327 | "%s", com::Utf8Str(strGuestAddr).c_str());
|
---|
328 |
|
---|
329 | /* XXX: limits should be checked */
|
---|
330 | r.Pfr.u16PfrGuestPort = (uint16_t)lGuestPort;
|
---|
331 |
|
---|
332 | if (fCreateFW) /* Addition */
|
---|
333 | {
|
---|
334 | rules.push_back(r);
|
---|
335 |
|
---|
336 | natServicePfRegister(rules.back());
|
---|
337 | }
|
---|
338 | else /* Deletion */
|
---|
339 | {
|
---|
340 | ITERATORNATSERVICEPF it;
|
---|
341 | for (it = rules.begin(); it != rules.end(); ++it)
|
---|
342 | {
|
---|
343 | /* compare */
|
---|
344 | NATSEVICEPORTFORWARDRULE& natFw = *it;
|
---|
345 | if ( natFw.Pfr.iPfrProto == r.Pfr.iPfrProto
|
---|
346 | && natFw.Pfr.u16PfrHostPort == r.Pfr.u16PfrHostPort
|
---|
347 | && (strncmp(natFw.Pfr.szPfrHostAddr, r.Pfr.szPfrHostAddr, INET6_ADDRSTRLEN) == 0)
|
---|
348 | && natFw.Pfr.u16PfrGuestPort == r.Pfr.u16PfrGuestPort
|
---|
349 | && (strncmp(natFw.Pfr.szPfrGuestAddr, r.Pfr.szPfrGuestAddr, INET6_ADDRSTRLEN) == 0))
|
---|
350 | {
|
---|
351 | fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
|
---|
352 | if (!pFwCopy)
|
---|
353 | {
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | memcpy(pFwCopy, &natFw.FWSpec, sizeof(fwspec));
|
---|
357 |
|
---|
358 | /* We shouldn't care about pFwCopy this memory will be freed when
|
---|
359 | * will message will arrive to the destination.
|
---|
360 | */
|
---|
361 | portfwd_rule_del(pFwCopy);
|
---|
362 |
|
---|
363 | rules.erase(it);
|
---|
364 | break;
|
---|
365 | }
|
---|
366 | } /* loop over vector elements */
|
---|
367 | } /* condition add or delete */
|
---|
368 | port_forward_done:
|
---|
369 | /* clean up strings */
|
---|
370 | name.setNull();
|
---|
371 | strHostAddr.setNull();
|
---|
372 | strGuestAddr.setNull();
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | return hrc;
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | void VBoxNetLwipNAT::onLwipTcpIpInit(void* arg)
|
---|
381 | {
|
---|
382 | AssertPtrReturnVoid(arg);
|
---|
383 | VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(arg);
|
---|
384 |
|
---|
385 | HRESULT hrc = com::Initialize();
|
---|
386 | Assert(!FAILED(hrc));
|
---|
387 |
|
---|
388 | proxy_arp_hook = pxremap_proxy_arp;
|
---|
389 | proxy_ip4_divert_hook = pxremap_ip4_divert;
|
---|
390 |
|
---|
391 | proxy_na_hook = pxremap_proxy_na;
|
---|
392 | proxy_ip6_divert_hook = pxremap_ip6_divert;
|
---|
393 |
|
---|
394 | /* lwip thread */
|
---|
395 | RTNETADDRIPV4 IpNetwork;
|
---|
396 | IpNetwork.u = g_pLwipNat->m_Ipv4Address.u & g_pLwipNat->m_Ipv4Netmask.u;
|
---|
397 |
|
---|
398 | ip_addr LwipIpAddr, LwipIpNetMask, LwipIpNetwork;
|
---|
399 |
|
---|
400 | memcpy(&LwipIpAddr, &g_pLwipNat->m_Ipv4Address, sizeof(ip_addr));
|
---|
401 | memcpy(&LwipIpNetMask, &g_pLwipNat->m_Ipv4Netmask, sizeof(ip_addr));
|
---|
402 | memcpy(&LwipIpNetwork, &IpNetwork, sizeof(ip_addr));
|
---|
403 |
|
---|
404 | netif *pNetif = netif_add(&g_pLwipNat->m_LwipNetIf /* Lwip Interface */,
|
---|
405 | &LwipIpAddr /* IP address*/,
|
---|
406 | &LwipIpNetMask /* Network mask */,
|
---|
407 | &LwipIpAddr /* gateway address, @todo: is self IP acceptable? */,
|
---|
408 | g_pLwipNat /* state */,
|
---|
409 | VBoxNetLwipNAT::netifInit /* netif_init_fn */,
|
---|
410 | lwip_tcpip_input /* netif_input_fn */);
|
---|
411 |
|
---|
412 | AssertPtrReturnVoid(pNetif);
|
---|
413 |
|
---|
414 | netif_set_up(pNetif);
|
---|
415 | netif_set_link_up(pNetif);
|
---|
416 |
|
---|
417 | if (pNat->m_ProxyOptions.ipv6_enabled) {
|
---|
418 | /*
|
---|
419 | * XXX: lwIP currently only ever calls mld6_joingroup() in
|
---|
420 | * nd6_tmr() for fresh tentative addresses, which is a wrong place
|
---|
421 | * to do it - but I'm not keen on fixing this properly for now
|
---|
422 | * (with correct handling of interface up and down transitions,
|
---|
423 | * etc). So stick it here as a kludge.
|
---|
424 | */
|
---|
425 | for (int i = 0; i <= 1; ++i) {
|
---|
426 | ip6_addr_t *paddr = netif_ip6_addr(pNetif, i);
|
---|
427 |
|
---|
428 | ip6_addr_t solicited_node_multicast_address;
|
---|
429 | ip6_addr_set_solicitednode(&solicited_node_multicast_address,
|
---|
430 | paddr->addr[3]);
|
---|
431 | mld6_joingroup(paddr, &solicited_node_multicast_address);
|
---|
432 | }
|
---|
433 |
|
---|
434 | /*
|
---|
435 | * XXX: We must join the solicited-node multicast for the
|
---|
436 | * addresses we do IPv6 NA-proxy for. We map IPv6 loopback to
|
---|
437 | * proxy address + 1. We only need the low 24 bits, and those are
|
---|
438 | * fixed.
|
---|
439 | */
|
---|
440 | {
|
---|
441 | ip6_addr_t solicited_node_multicast_address;
|
---|
442 |
|
---|
443 | ip6_addr_set_solicitednode(&solicited_node_multicast_address,
|
---|
444 | /* last 24 bits of the address */
|
---|
445 | PP_HTONL(0x00000002));
|
---|
446 | mld6_netif_joingroup(pNetif, &solicited_node_multicast_address);
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | proxy_init(&g_pLwipNat->m_LwipNetIf, &g_pLwipNat->m_ProxyOptions);
|
---|
451 |
|
---|
452 | natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule4);
|
---|
453 | natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule6);
|
---|
454 | }
|
---|
455 |
|
---|
456 |
|
---|
457 | void VBoxNetLwipNAT::onLwipTcpIpFini(void* arg)
|
---|
458 | {
|
---|
459 | AssertPtrReturnVoid(arg);
|
---|
460 | VBoxNetLwipNAT *pThis = (VBoxNetLwipNAT *)arg;
|
---|
461 |
|
---|
462 | /* XXX: proxy finalization */
|
---|
463 | netif_set_link_down(&g_pLwipNat->m_LwipNetIf);
|
---|
464 | netif_set_down(&g_pLwipNat->m_LwipNetIf);
|
---|
465 | netif_remove(&g_pLwipNat->m_LwipNetIf);
|
---|
466 |
|
---|
467 | }
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Callback for netif_add() to initialize the interface.
|
---|
471 | */
|
---|
472 | err_t VBoxNetLwipNAT::netifInit(netif *pNetif)
|
---|
473 | {
|
---|
474 | err_t rcLwip = ERR_OK;
|
---|
475 |
|
---|
476 | AssertPtrReturn(pNetif, ERR_ARG);
|
---|
477 |
|
---|
478 | VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(pNetif->state);
|
---|
479 | AssertPtrReturn(pNat, ERR_ARG);
|
---|
480 |
|
---|
481 | LogFlowFunc(("ENTER: pNetif[%c%c%d]\n", pNetif->name[0], pNetif->name[1], pNetif->num));
|
---|
482 | /* validity */
|
---|
483 | AssertReturn( pNetif->name[0] == 'N'
|
---|
484 | && pNetif->name[1] == 'T', ERR_ARG);
|
---|
485 |
|
---|
486 |
|
---|
487 | pNetif->hwaddr_len = sizeof(RTMAC);
|
---|
488 | memcpy(pNetif->hwaddr, &pNat->m_MacAddress, sizeof(RTMAC));
|
---|
489 |
|
---|
490 | pNat->m_u16Mtu = 1500; // XXX: FIXME
|
---|
491 | pNetif->mtu = pNat->m_u16Mtu;
|
---|
492 |
|
---|
493 | pNetif->flags = NETIF_FLAG_BROADCAST
|
---|
494 | | NETIF_FLAG_ETHARP /* Don't bother driver with ARP and let Lwip resolve ARP handling */
|
---|
495 | | NETIF_FLAG_ETHERNET; /* Lwip works with ethernet too */
|
---|
496 |
|
---|
497 | pNetif->linkoutput = netifLinkoutput; /* ether-level-pipe */
|
---|
498 | pNetif->output = lwip_etharp_output; /* ip-pipe */
|
---|
499 |
|
---|
500 | if (pNat->m_ProxyOptions.ipv6_enabled) {
|
---|
501 | pNetif->output_ip6 = ethip6_output;
|
---|
502 |
|
---|
503 | /* IPv6 link-local address in slot 0 */
|
---|
504 | netif_create_ip6_linklocal_address(pNetif, /* :from_mac_48bit */ 1);
|
---|
505 | netif_ip6_addr_set_state(pNetif, 0, IP6_ADDR_PREFERRED); // skip DAD
|
---|
506 |
|
---|
507 | /*
|
---|
508 | * RFC 4193 Locally Assigned Global ID (ULA) in slot 1
|
---|
509 | * [fd17:625c:f037:XXXX::1] where XXXX, 16 bit Subnet ID, are two
|
---|
510 | * bytes from the middle of the IPv4 address, e.g. :dead: for
|
---|
511 | * 10.222.173.1
|
---|
512 | */
|
---|
513 | u8_t nethi = ip4_addr2(&pNetif->ip_addr);
|
---|
514 | u8_t netlo = ip4_addr3(&pNetif->ip_addr);
|
---|
515 |
|
---|
516 | ip6_addr_t *paddr = netif_ip6_addr(pNetif, 1);
|
---|
517 | IP6_ADDR(paddr, 0, 0xFD, 0x17, 0x62, 0x5C);
|
---|
518 | IP6_ADDR(paddr, 1, 0xF0, 0x37, nethi, netlo);
|
---|
519 | IP6_ADDR(paddr, 2, 0x00, 0x00, 0x00, 0x00);
|
---|
520 | IP6_ADDR(paddr, 3, 0x00, 0x00, 0x00, 0x01);
|
---|
521 | netif_ip6_addr_set_state(pNetif, 1, IP6_ADDR_PREFERRED);
|
---|
522 |
|
---|
523 | #if LWIP_IPV6_SEND_ROUTER_SOLICIT
|
---|
524 | pNetif->rs_count = 0;
|
---|
525 | #endif
|
---|
526 | }
|
---|
527 |
|
---|
528 | LogFlowFunc(("LEAVE: %d\n", rcLwip));
|
---|
529 | return rcLwip;
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * Intnet-recv thread
|
---|
535 | */
|
---|
536 | int VBoxNetLwipNAT::intNetThreadRecv(RTTHREAD, void *)
|
---|
537 | {
|
---|
538 | int rc = VINF_SUCCESS;
|
---|
539 |
|
---|
540 | /* 1. initialization and connection */
|
---|
541 | HRESULT hrc = com::Initialize();
|
---|
542 | if (FAILED(hrc))
|
---|
543 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
|
---|
544 |
|
---|
545 | /* Well we're ready */
|
---|
546 | PINTNETRINGBUF pRingBuf = &g_pLwipNat->m_pIfBuf->Recv;
|
---|
547 |
|
---|
548 | for (;;)
|
---|
549 | {
|
---|
550 | /*
|
---|
551 | * Wait for a packet to become available.
|
---|
552 | */
|
---|
553 | /* 2. waiting for request for */
|
---|
554 | rc = g_pLwipNat->waitForIntNetEvent(2000);
|
---|
555 | if (RT_FAILURE(rc))
|
---|
556 | {
|
---|
557 | if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
|
---|
558 | {
|
---|
559 | /* do we want interrupt anyone ??? */
|
---|
560 | continue;
|
---|
561 | }
|
---|
562 | LogRel(("VBoxNetNAT: waitForIntNetEvent returned %Rrc\n", rc));
|
---|
563 | AssertRCReturn(rc,ERR_IF);
|
---|
564 | }
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * Process the receive buffer.
|
---|
568 | */
|
---|
569 | PCINTNETHDR pHdr;
|
---|
570 |
|
---|
571 | while ((pHdr = IntNetRingGetNextFrameToRead(pRingBuf)) != NULL)
|
---|
572 | {
|
---|
573 | uint8_t const u8Type = pHdr->u8Type;
|
---|
574 | size_t cbFrame = pHdr->cbFrame;
|
---|
575 | uint8_t *pu8Frame = NULL;
|
---|
576 | pbuf *pPbufHdr = NULL;
|
---|
577 | pbuf *pPbuf = NULL;
|
---|
578 | switch (u8Type)
|
---|
579 | {
|
---|
580 |
|
---|
581 | case INTNETHDR_TYPE_FRAME:
|
---|
582 | /* @todo:should it be really here?
|
---|
583 | * Well well well, we're accessing lwip code here
|
---|
584 | */
|
---|
585 | pPbufHdr = pPbuf = pbuf_alloc(PBUF_RAW, pHdr->cbFrame, PBUF_POOL);
|
---|
586 | if (!pPbuf)
|
---|
587 | {
|
---|
588 | LogRel(("NAT: Can't allocate send buffer cbFrame=%u\n", cbFrame));
|
---|
589 | break;
|
---|
590 | }
|
---|
591 | Assert(pPbufHdr->tot_len == cbFrame);
|
---|
592 | pu8Frame = (uint8_t *)IntNetHdrGetFramePtr(pHdr, g_pLwipNat->m_pIfBuf);
|
---|
593 | while(pPbuf)
|
---|
594 | {
|
---|
595 | memcpy(pPbuf->payload, pu8Frame, pPbuf->len);
|
---|
596 | pu8Frame += pPbuf->len;
|
---|
597 | pPbuf = pPbuf->next;
|
---|
598 | }
|
---|
599 |
|
---|
600 | g_pLwipNat->m_LwipNetIf.input(pPbufHdr, &g_pLwipNat->m_LwipNetIf);
|
---|
601 |
|
---|
602 | AssertReleaseRC(rc);
|
---|
603 | break;
|
---|
604 | case INTNETHDR_TYPE_GSO:
|
---|
605 | {
|
---|
606 | PCPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr,
|
---|
607 | g_pLwipNat->m_pIfBuf);
|
---|
608 | if (!PDMNetGsoIsValid(pGso, cbFrame,
|
---|
609 | cbFrame - sizeof(PDMNETWORKGSO)))
|
---|
610 | break;
|
---|
611 | cbFrame -= sizeof(PDMNETWORKGSO);
|
---|
612 | uint8_t abHdrScratch[256];
|
---|
613 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso,
|
---|
614 | cbFrame);
|
---|
615 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
616 | {
|
---|
617 | uint32_t cbSegFrame;
|
---|
618 | void *pvSegFrame =
|
---|
619 | PDMNetGsoCarveSegmentQD(pGso,
|
---|
620 | (uint8_t *)(pGso + 1),
|
---|
621 | cbFrame,
|
---|
622 | abHdrScratch,
|
---|
623 | iSeg,
|
---|
624 | cSegs,
|
---|
625 | &cbSegFrame);
|
---|
626 |
|
---|
627 | pPbuf = pbuf_alloc(PBUF_RAW, cbSegFrame, PBUF_POOL);
|
---|
628 | if (!pPbuf)
|
---|
629 | {
|
---|
630 | LogRel(("NAT: Can't allocate send buffer cbFrame=%u\n", cbSegFrame));
|
---|
631 | break;
|
---|
632 | }
|
---|
633 | Assert( !pPbuf->next
|
---|
634 | && pPbuf->len == cbSegFrame);
|
---|
635 | memcpy(pPbuf->payload, pvSegFrame, cbSegFrame);
|
---|
636 | g_pLwipNat->m_LwipNetIf.input(pPbuf, &g_pLwipNat->m_LwipNetIf);
|
---|
637 |
|
---|
638 | }
|
---|
639 |
|
---|
640 | }
|
---|
641 | break;
|
---|
642 | case INTNETHDR_TYPE_PADDING:
|
---|
643 | break;
|
---|
644 | default:
|
---|
645 | STAM_REL_COUNTER_INC(&g_pLwipNat->m_pIfBuf->cStatBadFrames);
|
---|
646 | break;
|
---|
647 | }
|
---|
648 | IntNetRingSkipFrame(&g_pLwipNat->m_pIfBuf->Recv);
|
---|
649 |
|
---|
650 | } /* loop */
|
---|
651 | }
|
---|
652 | /* 3. deinitilization and termination */
|
---|
653 | LogFlowFuncLeaveRC(rc);
|
---|
654 | return rc;
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | /**
|
---|
659 | *
|
---|
660 | */
|
---|
661 | void VBoxNetLwipNAT::vboxNetLwipNATProcessXmit()
|
---|
662 | {
|
---|
663 | int rc = VINF_SUCCESS;
|
---|
664 | INTNETIFSENDREQ SendReq;
|
---|
665 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
666 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
667 | SendReq.pSession = g_pLwipNat->m_pSession;
|
---|
668 | SendReq.hIf = g_pLwipNat->m_hIf;
|
---|
669 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
670 | AssertRC(rc);
|
---|
671 | }
|
---|
672 |
|
---|
673 |
|
---|
674 | err_t VBoxNetLwipNAT::netifLinkoutput(netif *pNetif, pbuf *pPBuf)
|
---|
675 | {
|
---|
676 | int rc = VINF_SUCCESS;
|
---|
677 | err_t rcLwip = ERR_OK;
|
---|
678 | AssertPtrReturn(pNetif, ERR_ARG);
|
---|
679 | AssertPtrReturn(pPBuf, ERR_ARG);
|
---|
680 | AssertReturn((void *)g_pLwipNat == pNetif->state, ERR_ARG);
|
---|
681 | LogFlowFunc(("ENTER: pNetif[%c%c%d], pPbuf:%p\n",
|
---|
682 | pNetif->name[0],
|
---|
683 | pNetif->name[1],
|
---|
684 | pNetif->num,
|
---|
685 | pPBuf));
|
---|
686 |
|
---|
687 | /*
|
---|
688 | * We're on the lwip thread ...
|
---|
689 | * try accure Xmit lock (actually we DO accure the lock ... )
|
---|
690 | * 1. we've entered csXmit so we should create frame
|
---|
691 | * 1.a. Frame creation success see 2.
|
---|
692 | * 1.b. (hm ... what about queue processing in place)
|
---|
693 | * 1.c. 2nd attempt create frame
|
---|
694 | * 1.d. Unlock the Xmit
|
---|
695 | * 1.e. goto BUSY.1
|
---|
696 | * 2. Copy pbuf to the frame
|
---|
697 | * 3. Send
|
---|
698 | * 4. leave csXmit & return.
|
---|
699 | *
|
---|
700 | * @todo: perhaps we can use it for optimization,
|
---|
701 | * e.g. drop UDP and reoccure lock on TCP NOTE: now BUSY is unachievable!
|
---|
702 | * Otherwise (BUSY)
|
---|
703 | * 1. Unbuffered (drop)
|
---|
704 | * (buffered)
|
---|
705 | * 1. Copy pbuf to entermediate buffer.
|
---|
706 | * 2. Add call buffer to the queue
|
---|
707 | * 3. return.
|
---|
708 | */
|
---|
709 | /* see p.1 */
|
---|
710 | rc = VINF_SUCCESS;
|
---|
711 | PINTNETHDR pHdr = NULL;
|
---|
712 | uint8_t *pu8Frame = NULL;
|
---|
713 | int offFrame = 0;
|
---|
714 | int idxSg = 0;
|
---|
715 | struct pbuf *pPBufPtr = pPBuf;
|
---|
716 | /* Allocate frame, and pad it if required. */
|
---|
717 | rc = IntNetRingAllocateFrame(&g_pLwipNat->m_pIfBuf->Send, pPBuf->tot_len, &pHdr, (void **)&pu8Frame);
|
---|
718 | if (RT_SUCCESS(rc))
|
---|
719 | {
|
---|
720 | /* see p. 2 */
|
---|
721 | while (pPBufPtr)
|
---|
722 | {
|
---|
723 | memcpy(&pu8Frame[offFrame], pPBufPtr->payload, pPBufPtr->len);
|
---|
724 | offFrame += pPBufPtr->len;
|
---|
725 | pPBufPtr = pPBufPtr->next;
|
---|
726 | }
|
---|
727 | }
|
---|
728 | if (RT_FAILURE(rc))
|
---|
729 | {
|
---|
730 | /* Could it be that some frames are still in the ring buffer */
|
---|
731 | /* 1.c */
|
---|
732 | AssertMsgFailed(("Debug Me!"));
|
---|
733 | }
|
---|
734 |
|
---|
735 | /* Commit - what really this function do */
|
---|
736 | IntNetRingCommitFrameEx(&g_pLwipNat->m_pIfBuf->Send, pHdr, pPBuf->tot_len);
|
---|
737 |
|
---|
738 | g_pLwipNat->vboxNetLwipNATProcessXmit();
|
---|
739 |
|
---|
740 | AssertRCReturn(rc, ERR_IF);
|
---|
741 | LogFlowFunc(("LEAVE: %d\n", rcLwip));
|
---|
742 | return rcLwip;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | VBoxNetLwipNAT::VBoxNetLwipNAT()
|
---|
747 | {
|
---|
748 | LogFlowFuncEnter();
|
---|
749 |
|
---|
750 | m_ProxyOptions.ipv6_enabled = 0;
|
---|
751 | m_ProxyOptions.ipv6_defroute = 0;
|
---|
752 | m_ProxyOptions.tftp_root = NULL;
|
---|
753 | m_ProxyOptions.src4 = NULL;
|
---|
754 | m_ProxyOptions.src6 = NULL;
|
---|
755 | memset(&m_src4, 0, sizeof(m_src4));
|
---|
756 | memset(&m_src6, 0, sizeof(m_src6));
|
---|
757 | m_src4.sin_family = AF_INET;
|
---|
758 | m_src6.sin6_family = AF_INET6;
|
---|
759 | #if HAVE_SA_LEN
|
---|
760 | m_src4.sin_len = sizeof(m_src4);
|
---|
761 | m_src6.sin6_len = sizeof(m_src6);
|
---|
762 | #endif
|
---|
763 |
|
---|
764 | m_LwipNetIf.name[0] = 'N';
|
---|
765 | m_LwipNetIf.name[1] = 'T';
|
---|
766 | m_MacAddress.au8[0] = 0x52;
|
---|
767 | m_MacAddress.au8[1] = 0x54;
|
---|
768 | m_MacAddress.au8[2] = 0;
|
---|
769 | m_MacAddress.au8[3] = 0x12;
|
---|
770 | m_MacAddress.au8[4] = 0x35;
|
---|
771 | m_MacAddress.au8[5] = 0;
|
---|
772 | m_Ipv4Address.u = RT_MAKE_U32_FROM_U8( 10, 0, 2, 2); // NB: big-endian
|
---|
773 | m_Ipv4Netmask.u = RT_H2N_U32_C(0xffffff00);
|
---|
774 |
|
---|
775 | fDontLoadRulesOnStartup = false;
|
---|
776 |
|
---|
777 | for(unsigned int i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i)
|
---|
778 | m_vecOptionDefs.push_back(&g_aGetOptDef[i]);
|
---|
779 |
|
---|
780 | m_enmTrunkType = kIntNetTrunkType_SrvNat;
|
---|
781 |
|
---|
782 | LogFlowFuncLeave();
|
---|
783 | }
|
---|
784 |
|
---|
785 |
|
---|
786 | VBoxNetLwipNAT::~VBoxNetLwipNAT()
|
---|
787 | {
|
---|
788 | if (m_ProxyOptions.tftp_root != NULL)
|
---|
789 | {
|
---|
790 | RTStrFree((char *)m_ProxyOptions.tftp_root);
|
---|
791 | }
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | int VBoxNetLwipNAT::natServicePfRegister(NATSEVICEPORTFORWARDRULE& natPf)
|
---|
796 | {
|
---|
797 | int lrc = 0;
|
---|
798 | int rc = VINF_SUCCESS;
|
---|
799 | int socketSpec = SOCK_STREAM;
|
---|
800 | char *pszHostAddr;
|
---|
801 | int sockFamily = (natPf.Pfr.fPfrIPv6 ? PF_INET6 : PF_INET);
|
---|
802 |
|
---|
803 | switch(natPf.Pfr.iPfrProto)
|
---|
804 | {
|
---|
805 | case IPPROTO_TCP:
|
---|
806 | socketSpec = SOCK_STREAM;
|
---|
807 | break;
|
---|
808 | case IPPROTO_UDP:
|
---|
809 | socketSpec = SOCK_DGRAM;
|
---|
810 | break;
|
---|
811 | default:
|
---|
812 | return VERR_IGNORED; /* Ah, just ignore the garbage */
|
---|
813 | }
|
---|
814 |
|
---|
815 | pszHostAddr = natPf.Pfr.szPfrHostAddr;
|
---|
816 |
|
---|
817 | /* XXX: workaround for inet_pton and an empty ipv4 address
|
---|
818 | * in rule declaration.
|
---|
819 | */
|
---|
820 | if ( sockFamily == PF_INET
|
---|
821 | && pszHostAddr[0] == 0)
|
---|
822 | pszHostAddr = (char *)"0.0.0.0"; /* XXX: fix it! without type cast */
|
---|
823 |
|
---|
824 |
|
---|
825 | lrc = fwspec_set(&natPf.FWSpec,
|
---|
826 | sockFamily,
|
---|
827 | socketSpec,
|
---|
828 | pszHostAddr,
|
---|
829 | natPf.Pfr.u16PfrHostPort,
|
---|
830 | natPf.Pfr.szPfrGuestAddr,
|
---|
831 | natPf.Pfr.u16PfrGuestPort);
|
---|
832 |
|
---|
833 | AssertReturn(!lrc, VERR_IGNORED);
|
---|
834 |
|
---|
835 | fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
|
---|
836 | AssertPtrReturn(pFwCopy, VERR_IGNORED);
|
---|
837 |
|
---|
838 | /*
|
---|
839 | * We need pass the copy, because we can't be sure
|
---|
840 | * how much this pointer will be valid in LWIP environment.
|
---|
841 | */
|
---|
842 | memcpy(pFwCopy, &natPf.FWSpec, sizeof(fwspec));
|
---|
843 |
|
---|
844 | lrc = portfwd_rule_add(pFwCopy);
|
---|
845 |
|
---|
846 | AssertReturn(!lrc, VERR_IGNORED);
|
---|
847 |
|
---|
848 | return VINF_SUCCESS;
|
---|
849 | }
|
---|
850 |
|
---|
851 |
|
---|
852 | int VBoxNetLwipNAT::natServiceProcessRegisteredPf(VECNATSERVICEPF& vecRules){
|
---|
853 | ITERATORNATSERVICEPF it;
|
---|
854 | for (it = vecRules.begin();
|
---|
855 | it != vecRules.end(); ++it)
|
---|
856 | {
|
---|
857 | int rc = natServicePfRegister((*it));
|
---|
858 | if (RT_FAILURE(rc))
|
---|
859 | {
|
---|
860 | LogRel(("PF: %s is ignored\n", (*it).Pfr.szPfrName));
|
---|
861 | continue;
|
---|
862 | }
|
---|
863 | }
|
---|
864 | return VINF_SUCCESS;
|
---|
865 | }
|
---|
866 |
|
---|
867 |
|
---|
868 | int VBoxNetLwipNAT::init()
|
---|
869 | {
|
---|
870 | com::Bstr bstr;
|
---|
871 | int rc = VINF_SUCCESS;
|
---|
872 | LogFlowFuncEnter();
|
---|
873 |
|
---|
874 |
|
---|
875 | /* virtualbox initialized in super class */
|
---|
876 |
|
---|
877 | HRESULT hrc;
|
---|
878 | hrc = virtualbox->FindNATNetworkByName(com::Bstr(m_Network.c_str()).raw(),
|
---|
879 | net.asOutParam());
|
---|
880 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
881 |
|
---|
882 | BOOL fIPv6Enabled = FALSE;
|
---|
883 | hrc = net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
|
---|
884 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
885 |
|
---|
886 | BOOL fIPv6DefaultRoute = FALSE;
|
---|
887 | if (fIPv6Enabled)
|
---|
888 | {
|
---|
889 | hrc = net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
|
---|
890 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
891 | }
|
---|
892 |
|
---|
893 | m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
|
---|
894 | m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
|
---|
895 |
|
---|
896 | /* XXX: Temporaly disabled this code on Windows for further debugging */
|
---|
897 | ComPtr<IEventSource> pES;
|
---|
898 |
|
---|
899 | hrc = net->COMGETTER(EventSource)(pES.asOutParam());
|
---|
900 | AssertComRC(hrc);
|
---|
901 |
|
---|
902 | ComObjPtr<NATNetworkListenerImpl> listener;
|
---|
903 | hrc = listener.createObject();
|
---|
904 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
905 |
|
---|
906 | hrc = listener->init(new NATNetworkListener(), this);
|
---|
907 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
908 |
|
---|
909 | com::SafeArray<VBoxEventType_T> events;
|
---|
910 | events.push_back(VBoxEventType_OnNATNetworkPortForward);
|
---|
911 | events.push_back(VBoxEventType_OnNATNetworkSetting);
|
---|
912 |
|
---|
913 | hrc = pES->RegisterListener(listener, ComSafeArrayAsInParam(events), true);
|
---|
914 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
915 |
|
---|
916 | m_listener = listener;
|
---|
917 |
|
---|
918 | com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4",m_Network.c_str());
|
---|
919 | com::Bstr bstrSourceIpX;
|
---|
920 | RTNETADDRIPV4 addr;
|
---|
921 | hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
|
---|
922 | if (SUCCEEDED(hrc))
|
---|
923 | {
|
---|
924 | rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
|
---|
925 | if (RT_SUCCESS(rc))
|
---|
926 | {
|
---|
927 | RT_ZERO(m_src4);
|
---|
928 |
|
---|
929 | m_src4.sin_addr.s_addr = addr.u;
|
---|
930 | m_ProxyOptions.src4 = &m_src4;
|
---|
931 |
|
---|
932 | bstrSourceIpX.setNull();
|
---|
933 | }
|
---|
934 | }
|
---|
935 |
|
---|
936 | if (!fDontLoadRulesOnStartup)
|
---|
937 | {
|
---|
938 | /* XXX: extract function and do not duplicate */
|
---|
939 | com::SafeArray<BSTR> rules;
|
---|
940 | hrc = net->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
|
---|
941 | Assert(SUCCEEDED(hrc));
|
---|
942 |
|
---|
943 | size_t idxRules = 0;
|
---|
944 | for (idxRules = 0; idxRules < rules.size(); ++idxRules)
|
---|
945 | {
|
---|
946 | Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
|
---|
947 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
948 | RT_ZERO(Rule);
|
---|
949 | rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 0, &Rule.Pfr);
|
---|
950 | AssertRC(rc);
|
---|
951 | m_vecPortForwardRule4.push_back(Rule);
|
---|
952 | }
|
---|
953 |
|
---|
954 | rules.setNull();
|
---|
955 | hrc = net->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
|
---|
956 | Assert(SUCCEEDED(hrc));
|
---|
957 |
|
---|
958 | for (idxRules = 0; idxRules < rules.size(); ++idxRules)
|
---|
959 | {
|
---|
960 | Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
|
---|
961 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
962 | netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 1, &Rule.Pfr);
|
---|
963 | m_vecPortForwardRule6.push_back(Rule);
|
---|
964 | }
|
---|
965 | } /* if (!fDontLoadRulesOnStartup) */
|
---|
966 |
|
---|
967 | com::SafeArray<BSTR> strs;
|
---|
968 | int count_strs;
|
---|
969 | hrc = net->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
|
---|
970 | if ( SUCCEEDED(hrc)
|
---|
971 | && (count_strs = strs.size()))
|
---|
972 | {
|
---|
973 | unsigned int j = 0;
|
---|
974 | int i;
|
---|
975 |
|
---|
976 | for (i = 0; i < count_strs && j < RT_ELEMENTS(m_lo2off); ++i)
|
---|
977 | {
|
---|
978 | char szAddr[17];
|
---|
979 | RTNETADDRIPV4 ip4addr;
|
---|
980 | char *pszTerm;
|
---|
981 | uint32_t u32Off;
|
---|
982 | com::Utf8Str strLo2Off(strs[i]);
|
---|
983 | const char *pszLo2Off = strLo2Off.c_str();
|
---|
984 |
|
---|
985 | RT_ZERO(szAddr);
|
---|
986 |
|
---|
987 | pszTerm = RTStrStr(pszLo2Off, "=");
|
---|
988 |
|
---|
989 | if ( !pszTerm
|
---|
990 | || (pszTerm - pszLo2Off) >= 17)
|
---|
991 | continue;
|
---|
992 |
|
---|
993 | memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
|
---|
994 | rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
|
---|
995 | if (RT_FAILURE(rc))
|
---|
996 | continue;
|
---|
997 |
|
---|
998 | u32Off = RTStrToUInt32(pszTerm + 1);
|
---|
999 | if (u32Off == 0)
|
---|
1000 | continue;
|
---|
1001 |
|
---|
1002 | ip4_addr_set_u32(&m_lo2off[j].loaddr, ip4addr.u);
|
---|
1003 | m_lo2off[j].off = u32Off;
|
---|
1004 | ++j;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | m_loOptDescriptor.lomap = m_lo2off;
|
---|
1008 | m_loOptDescriptor.num_lomap = j;
|
---|
1009 | m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
|
---|
1013 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
1014 | if (!bstr.isEmpty())
|
---|
1015 | {
|
---|
1016 | com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
|
---|
1017 | bstr.raw(), RTPATH_DELIMITER, "TFTP"));
|
---|
1018 | char *pszStrTemp; // avoid const char ** vs char **
|
---|
1019 | rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
|
---|
1020 | AssertRC(rc);
|
---|
1021 | m_ProxyOptions.tftp_root = pszStrTemp;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /* end of COM initialization */
|
---|
1025 |
|
---|
1026 | rc = RTSemEventCreate(&hSemSVC);
|
---|
1027 | AssertRCReturn(rc, rc);
|
---|
1028 |
|
---|
1029 | rc = RTReqQueueCreate(&hReqIntNet);
|
---|
1030 | AssertRCReturn(rc, rc);
|
---|
1031 |
|
---|
1032 | g_pLwipNat->tryGoOnline();
|
---|
1033 | vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
|
---|
1034 |
|
---|
1035 | rc = RTThreadCreate(&g_pLwipNat->hThrIntNetRecv, /* thread handle*/
|
---|
1036 | VBoxNetLwipNAT::intNetThreadRecv, /* routine */
|
---|
1037 | NULL, /* user data */
|
---|
1038 | 128 * _1K, /* stack size */
|
---|
1039 | RTTHREADTYPE_IO, /* type */
|
---|
1040 | 0, /* flags, @todo: waitable ?*/
|
---|
1041 | "INTNET-RECV");
|
---|
1042 | AssertRCReturn(rc,rc);
|
---|
1043 |
|
---|
1044 |
|
---|
1045 |
|
---|
1046 | LogFlowFuncLeaveRC(rc);
|
---|
1047 | return rc;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | int VBoxNetLwipNAT::parseOpt(int rc, const RTGETOPTUNION& Val)
|
---|
1052 | {
|
---|
1053 | switch (rc)
|
---|
1054 | {
|
---|
1055 | case 'p':
|
---|
1056 | case 'P':
|
---|
1057 | {
|
---|
1058 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
1059 | VECNATSERVICEPF& rules = (rc == 'P'?
|
---|
1060 | m_vecPortForwardRule6
|
---|
1061 | : m_vecPortForwardRule4);
|
---|
1062 |
|
---|
1063 | fDontLoadRulesOnStartup = true;
|
---|
1064 |
|
---|
1065 | RT_ZERO(Rule);
|
---|
1066 |
|
---|
1067 | int irc = netPfStrToPf(Val.psz, (rc == 'P'), &Rule.Pfr);
|
---|
1068 | rules.push_back(Rule);
|
---|
1069 | return VINF_SUCCESS;
|
---|
1070 | }
|
---|
1071 | default:;
|
---|
1072 | }
|
---|
1073 | return VERR_NOT_FOUND;
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 |
|
---|
1077 | int VBoxNetLwipNAT::run()
|
---|
1078 | {
|
---|
1079 | /* EventQueue processing from VBoxHeadless.cpp */
|
---|
1080 | com::NativeEventQueue *gEventQ = NULL;
|
---|
1081 | gEventQ = com::NativeEventQueue::getMainEventQueue();
|
---|
1082 | while(true)
|
---|
1083 | {
|
---|
1084 | /* XXX:todo: graceful termination */
|
---|
1085 | gEventQ->processEventQueue(0);
|
---|
1086 | gEventQ->processEventQueue(500);
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
|
---|
1090 |
|
---|
1091 | m_vecPortForwardRule4.clear();
|
---|
1092 | m_vecPortForwardRule6.clear();
|
---|
1093 |
|
---|
1094 | return VINF_SUCCESS;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * Entry point.
|
---|
1100 | */
|
---|
1101 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
1102 | {
|
---|
1103 | LogFlowFuncEnter();
|
---|
1104 |
|
---|
1105 | NOREF(envp);
|
---|
1106 |
|
---|
1107 | #ifdef RT_OS_WINDOWS
|
---|
1108 | WSADATA wsaData;
|
---|
1109 | int err;
|
---|
1110 |
|
---|
1111 | err = WSAStartup(MAKEWORD(2,2), &wsaData);
|
---|
1112 | if (err)
|
---|
1113 | {
|
---|
1114 | fprintf(stderr, "wsastartup: failed (%d)\n", err);
|
---|
1115 | return 1;
|
---|
1116 | }
|
---|
1117 | #endif
|
---|
1118 |
|
---|
1119 | HRESULT hrc = com::Initialize();
|
---|
1120 | #ifdef VBOX_WITH_XPCOM
|
---|
1121 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
1122 | {
|
---|
1123 | char szHome[RTPATH_MAX] = "";
|
---|
1124 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
1125 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
1126 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
1127 | }
|
---|
1128 | #endif
|
---|
1129 | if (FAILED(hrc))
|
---|
1130 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
|
---|
1131 |
|
---|
1132 | g_pLwipNat = new VBoxNetLwipNAT();
|
---|
1133 |
|
---|
1134 | Log2(("NAT: initialization\n"));
|
---|
1135 | int rc = g_pLwipNat->parseArgs(argc - 1, argv + 1);
|
---|
1136 | AssertRC(rc);
|
---|
1137 |
|
---|
1138 |
|
---|
1139 | if (!rc)
|
---|
1140 | {
|
---|
1141 |
|
---|
1142 | g_pLwipNat->init();
|
---|
1143 | g_pLwipNat->run();
|
---|
1144 |
|
---|
1145 | }
|
---|
1146 | delete g_pLwipNat;
|
---|
1147 | return 0;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 |
|
---|
1151 | #ifndef VBOX_WITH_HARDENING
|
---|
1152 |
|
---|
1153 | int main(int argc, char **argv, char **envp)
|
---|
1154 | {
|
---|
1155 | int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
1156 | if (RT_FAILURE(rc))
|
---|
1157 | return RTMsgInitFailure(rc);
|
---|
1158 |
|
---|
1159 | return TrustedMain(argc, argv, envp);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | # if defined(RT_OS_WINDOWS)
|
---|
1163 |
|
---|
1164 | static LRESULT CALLBACK WindowProc(HWND hwnd,
|
---|
1165 | UINT uMsg,
|
---|
1166 | WPARAM wParam,
|
---|
1167 | LPARAM lParam
|
---|
1168 | )
|
---|
1169 | {
|
---|
1170 | if(uMsg == WM_DESTROY)
|
---|
1171 | {
|
---|
1172 | PostQuitMessage(0);
|
---|
1173 | return 0;
|
---|
1174 | }
|
---|
1175 | return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | static LPCWSTR g_WndClassName = L"VBoxNetNatLwipClass";
|
---|
1179 |
|
---|
1180 | static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
|
---|
1181 | {
|
---|
1182 | HWND hwnd = 0;
|
---|
1183 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
|
---|
1184 | bool bExit = false;
|
---|
1185 |
|
---|
1186 | /* Register the Window Class. */
|
---|
1187 | WNDCLASS wc;
|
---|
1188 | wc.style = 0;
|
---|
1189 | wc.lpfnWndProc = WindowProc;
|
---|
1190 | wc.cbClsExtra = 0;
|
---|
1191 | wc.cbWndExtra = sizeof(void *);
|
---|
1192 | wc.hInstance = hInstance;
|
---|
1193 | wc.hIcon = NULL;
|
---|
1194 | wc.hCursor = NULL;
|
---|
1195 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
1196 | wc.lpszMenuName = NULL;
|
---|
1197 | wc.lpszClassName = g_WndClassName;
|
---|
1198 |
|
---|
1199 | ATOM atomWindowClass = RegisterClass(&wc);
|
---|
1200 |
|
---|
1201 | if (atomWindowClass != 0)
|
---|
1202 | {
|
---|
1203 | /* Create the window. */
|
---|
1204 | hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
1205 | g_WndClassName, g_WndClassName,
|
---|
1206 | WS_POPUPWINDOW,
|
---|
1207 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
1208 |
|
---|
1209 | if (hwnd)
|
---|
1210 | {
|
---|
1211 | SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
1212 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
1213 |
|
---|
1214 | MSG msg;
|
---|
1215 | while (GetMessage(&msg, NULL, 0, 0))
|
---|
1216 | {
|
---|
1217 | TranslateMessage(&msg);
|
---|
1218 | DispatchMessage(&msg);
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | DestroyWindow (hwnd);
|
---|
1222 |
|
---|
1223 | bExit = true;
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | UnregisterClass (g_WndClassName, hInstance);
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | if(bExit)
|
---|
1230 | {
|
---|
1231 | /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
|
---|
1232 | exit(0);
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | return 0;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 |
|
---|
1239 |
|
---|
1240 | /** (We don't want a console usually.) */
|
---|
1241 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
1242 | {
|
---|
1243 | #if 0
|
---|
1244 | NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
|
---|
1245 |
|
---|
1246 | HANDLE hThread = CreateThread(
|
---|
1247 | NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
|
---|
1248 | 0, /*__in SIZE_T dwStackSize, */
|
---|
1249 | MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
|
---|
1250 | NULL, /*__in_opt LPVOID lpParameter,*/
|
---|
1251 | 0, /*__in DWORD dwCreationFlags,*/
|
---|
1252 | NULL /*__out_opt LPDWORD lpThreadId*/
|
---|
1253 | );
|
---|
1254 |
|
---|
1255 | if(hThread != NULL)
|
---|
1256 | CloseHandle(hThread);
|
---|
1257 |
|
---|
1258 | #endif
|
---|
1259 | return main(__argc, __argv, environ);
|
---|
1260 | }
|
---|
1261 | # endif /* RT_OS_WINDOWS */
|
---|
1262 |
|
---|
1263 | #endif /* !VBOX_WITH_HARDENING */
|
---|
1264 |
|
---|