1 | /* $Id: VBoxNetLwipNAT.cpp 49842 2013-12-09 13:49:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetNAT - NAT Service for connecting to IntNet.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "winutils.h"
|
---|
19 |
|
---|
20 | #include <VBox/com/assert.h>
|
---|
21 | #include <VBox/com/com.h>
|
---|
22 | #include <VBox/com/listeners.h>
|
---|
23 | #include <VBox/com/string.h>
|
---|
24 | #include <VBox/com/Guid.h>
|
---|
25 | #include <VBox/com/array.h>
|
---|
26 | #include <VBox/com/ErrorInfo.h>
|
---|
27 | #include <VBox/com/errorprint.h>
|
---|
28 | #include <VBox/com/VirtualBox.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 | # ifdef RT_OS_LINUX
|
---|
67 | # include <linux/icmp.h> /* ICMP_FILTER */
|
---|
68 | # endif
|
---|
69 | # include <netinet/icmp6.h>
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #include <map>
|
---|
73 | #include <vector>
|
---|
74 | #include <string>
|
---|
75 |
|
---|
76 | #include "../NetLib/VBoxNetLib.h"
|
---|
77 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
78 | #include "../NetLib/utils.h"
|
---|
79 | #include "VBoxLwipCore.h"
|
---|
80 |
|
---|
81 | extern "C"
|
---|
82 | {
|
---|
83 | /* bunch of LWIP headers */
|
---|
84 | #include "lwip/opt.h"
|
---|
85 | #ifdef LWIP_SOCKET
|
---|
86 | # undef LWIP_SOCKET
|
---|
87 | # define LWIP_SOCKET 0
|
---|
88 | #endif
|
---|
89 | #include "lwip/sys.h"
|
---|
90 | #include "lwip/pbuf.h"
|
---|
91 | #include "lwip/netif.h"
|
---|
92 | #include "lwip/ethip6.h"
|
---|
93 | #include "lwip/nd6.h" // for proxy_na_hook
|
---|
94 | #include "lwip/mld6.h"
|
---|
95 | #include "lwip/tcpip.h"
|
---|
96 | #include "netif/etharp.h"
|
---|
97 |
|
---|
98 | #include "proxy.h"
|
---|
99 | #include "pxremap.h"
|
---|
100 | #include "portfwd.h"
|
---|
101 | }
|
---|
102 |
|
---|
103 | #include "../NetLib/VBoxPortForwardString.h"
|
---|
104 |
|
---|
105 | static RTGETOPTDEF g_aGetOptDef[] =
|
---|
106 | {
|
---|
107 | { "--port-forward4", 'p', RTGETOPT_REQ_STRING },
|
---|
108 | { "--port-forward6", 'P', RTGETOPT_REQ_STRING }
|
---|
109 | };
|
---|
110 |
|
---|
111 | typedef struct NATSEVICEPORTFORWARDRULE
|
---|
112 | {
|
---|
113 | PORTFORWARDRULE Pfr;
|
---|
114 | fwspec FWSpec;
|
---|
115 | } NATSEVICEPORTFORWARDRULE, *PNATSEVICEPORTFORWARDRULE;
|
---|
116 |
|
---|
117 | typedef std::vector<NATSEVICEPORTFORWARDRULE> VECNATSERVICEPF;
|
---|
118 | typedef VECNATSERVICEPF::iterator ITERATORNATSERVICEPF;
|
---|
119 | typedef VECNATSERVICEPF::const_iterator CITERATORNATSERVICEPF;
|
---|
120 |
|
---|
121 | static int fetchNatPortForwardRules(const ComNatPtr&, bool, VECNATSERVICEPF&);
|
---|
122 |
|
---|
123 |
|
---|
124 | class VBoxNetLwipNAT: public VBoxNetBaseService, public NATNetworkEventAdapter
|
---|
125 | {
|
---|
126 | friend class NATNetworkListener;
|
---|
127 | public:
|
---|
128 | VBoxNetLwipNAT(SOCKET icmpsock4, SOCKET icmpsock6);
|
---|
129 | virtual ~VBoxNetLwipNAT();
|
---|
130 | void usage(){ /* @todo: should be implemented */ };
|
---|
131 | int run();
|
---|
132 | virtual int init(void);
|
---|
133 | virtual int parseOpt(int rc, const RTGETOPTUNION& getOptVal);
|
---|
134 | /* VBoxNetNAT always needs Main */
|
---|
135 | virtual bool isMainNeeded() const { return true; }
|
---|
136 | virtual int processFrame(void *, size_t);
|
---|
137 | virtual int processGSO(PCPDMNETWORKGSO, size_t);
|
---|
138 | virtual int processUDP(void *, size_t) { return VERR_IGNORED; }
|
---|
139 |
|
---|
140 | private:
|
---|
141 | struct proxy_options m_ProxyOptions;
|
---|
142 | struct sockaddr_in m_src4;
|
---|
143 | struct sockaddr_in6 m_src6;
|
---|
144 | /**
|
---|
145 | * place for registered local interfaces.
|
---|
146 | */
|
---|
147 | ip4_lomap m_lo2off[10];
|
---|
148 | ip4_lomap_desc m_loOptDescriptor;
|
---|
149 |
|
---|
150 | uint16_t m_u16Mtu;
|
---|
151 | netif m_LwipNetIf;
|
---|
152 |
|
---|
153 | /* Our NAT network descriptor in Main */
|
---|
154 | ComPtr<INATNetwork> m_net;
|
---|
155 | ComNatListenerPtr m_listener;
|
---|
156 |
|
---|
157 | ComPtr<IHost> m_host;
|
---|
158 | ComNatListenerPtr m_vboxListener;
|
---|
159 | static INTNETSEG aXmitSeg[64];
|
---|
160 |
|
---|
161 | HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent);
|
---|
162 |
|
---|
163 | const char **getHostNameservers();
|
---|
164 |
|
---|
165 | /* Only for debug needs, by default NAT service should load rules from SVC
|
---|
166 | * on startup, and then on sync them on events.
|
---|
167 | */
|
---|
168 | bool fDontLoadRulesOnStartup;
|
---|
169 | static void onLwipTcpIpInit(void *arg);
|
---|
170 | static void onLwipTcpIpFini(void *arg);
|
---|
171 | static err_t netifInit(netif *pNetif);
|
---|
172 | static err_t netifLinkoutput(netif *pNetif, pbuf *pBuf);
|
---|
173 | static int intNetThreadRecv(RTTHREAD, void *);
|
---|
174 |
|
---|
175 | VECNATSERVICEPF m_vecPortForwardRule4;
|
---|
176 | VECNATSERVICEPF m_vecPortForwardRule6;
|
---|
177 |
|
---|
178 | static int natServicePfRegister(NATSEVICEPORTFORWARDRULE& natServicePf);
|
---|
179 | static int natServiceProcessRegisteredPf(VECNATSERVICEPF& vecPf);
|
---|
180 | };
|
---|
181 |
|
---|
182 |
|
---|
183 | static VBoxNetLwipNAT *g_pLwipNat;
|
---|
184 | INTNETSEG VBoxNetLwipNAT::aXmitSeg[64];
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * @note: this work on Event thread.
|
---|
188 | */
|
---|
189 | HRESULT VBoxNetLwipNAT::HandleEvent(VBoxEventType_T aEventType,
|
---|
190 | IEvent *pEvent)
|
---|
191 | {
|
---|
192 | HRESULT hrc = S_OK;
|
---|
193 | switch (aEventType)
|
---|
194 | {
|
---|
195 | case VBoxEventType_OnNATNetworkSetting:
|
---|
196 | {
|
---|
197 | ComPtr<INATNetworkSettingEvent> evSettings(pEvent);
|
---|
198 | // XXX: only handle IPv6 default route for now
|
---|
199 |
|
---|
200 | if (!m_ProxyOptions.ipv6_enabled)
|
---|
201 | {
|
---|
202 | break;
|
---|
203 | }
|
---|
204 |
|
---|
205 | BOOL fIPv6DefaultRoute = FALSE;
|
---|
206 | hrc = evSettings->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
|
---|
207 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
208 |
|
---|
209 | if (m_ProxyOptions.ipv6_defroute == fIPv6DefaultRoute)
|
---|
210 | {
|
---|
211 | break;
|
---|
212 | }
|
---|
213 |
|
---|
214 | m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
|
---|
215 | tcpip_callback_with_block(proxy_rtadvd_do_quick, &m_LwipNetIf, 0);
|
---|
216 |
|
---|
217 | break;
|
---|
218 | }
|
---|
219 |
|
---|
220 | case VBoxEventType_OnNATNetworkPortForward:
|
---|
221 | {
|
---|
222 | com::Bstr name, strHostAddr, strGuestAddr;
|
---|
223 | LONG lHostPort, lGuestPort;
|
---|
224 | BOOL fCreateFW, fIPv6FW;
|
---|
225 | NATProtocol_T proto = NATProtocol_TCP;
|
---|
226 |
|
---|
227 |
|
---|
228 | ComPtr<INATNetworkPortForwardEvent> pfEvt = pEvent;
|
---|
229 |
|
---|
230 | hrc = pfEvt->COMGETTER(Name)(name.asOutParam());
|
---|
231 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
232 |
|
---|
233 | hrc = pfEvt->COMGETTER(Proto)(&proto);
|
---|
234 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
235 |
|
---|
236 | hrc = pfEvt->COMGETTER(HostIp)(strHostAddr.asOutParam());
|
---|
237 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
238 |
|
---|
239 | hrc = pfEvt->COMGETTER(HostPort)(&lHostPort);
|
---|
240 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
241 |
|
---|
242 | hrc = pfEvt->COMGETTER(GuestIp)(strGuestAddr.asOutParam());
|
---|
243 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
244 |
|
---|
245 | hrc = pfEvt->COMGETTER(GuestPort)(&lGuestPort);
|
---|
246 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
247 |
|
---|
248 | hrc = pfEvt->COMGETTER(Create)(&fCreateFW);
|
---|
249 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
250 |
|
---|
251 | hrc = pfEvt->COMGETTER(Ipv6)(&fIPv6FW);
|
---|
252 | AssertReturn(SUCCEEDED(hrc), hrc);
|
---|
253 |
|
---|
254 | VECNATSERVICEPF& rules = (fIPv6FW ?
|
---|
255 | m_vecPortForwardRule6 :
|
---|
256 | m_vecPortForwardRule4);
|
---|
257 |
|
---|
258 | NATSEVICEPORTFORWARDRULE r;
|
---|
259 |
|
---|
260 | RT_ZERO(r);
|
---|
261 |
|
---|
262 | if (name.length() > sizeof(r.Pfr.szPfrName))
|
---|
263 | {
|
---|
264 | hrc = E_INVALIDARG;
|
---|
265 | goto port_forward_done;
|
---|
266 | }
|
---|
267 |
|
---|
268 | r.Pfr.fPfrIPv6 = fIPv6FW;
|
---|
269 |
|
---|
270 | switch (proto)
|
---|
271 | {
|
---|
272 | case NATProtocol_TCP:
|
---|
273 | r.Pfr.iPfrProto = IPPROTO_TCP;
|
---|
274 | break;
|
---|
275 | case NATProtocol_UDP:
|
---|
276 | r.Pfr.iPfrProto = IPPROTO_UDP;
|
---|
277 | break;
|
---|
278 | default:
|
---|
279 | goto port_forward_done;
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | RTStrPrintf(r.Pfr.szPfrName, sizeof(r.Pfr.szPfrName),
|
---|
284 | "%s", com::Utf8Str(name).c_str());
|
---|
285 |
|
---|
286 | RTStrPrintf(r.Pfr.szPfrHostAddr, sizeof(r.Pfr.szPfrHostAddr),
|
---|
287 | "%s", com::Utf8Str(strHostAddr).c_str());
|
---|
288 |
|
---|
289 | /* XXX: limits should be checked */
|
---|
290 | r.Pfr.u16PfrHostPort = (uint16_t)lHostPort;
|
---|
291 |
|
---|
292 | RTStrPrintf(r.Pfr.szPfrGuestAddr, sizeof(r.Pfr.szPfrGuestAddr),
|
---|
293 | "%s", com::Utf8Str(strGuestAddr).c_str());
|
---|
294 |
|
---|
295 | /* XXX: limits should be checked */
|
---|
296 | r.Pfr.u16PfrGuestPort = (uint16_t)lGuestPort;
|
---|
297 |
|
---|
298 | if (fCreateFW) /* Addition */
|
---|
299 | {
|
---|
300 | rules.push_back(r);
|
---|
301 |
|
---|
302 | natServicePfRegister(rules.back());
|
---|
303 | }
|
---|
304 | else /* Deletion */
|
---|
305 | {
|
---|
306 | ITERATORNATSERVICEPF it;
|
---|
307 | for (it = rules.begin(); it != rules.end(); ++it)
|
---|
308 | {
|
---|
309 | /* compare */
|
---|
310 | NATSEVICEPORTFORWARDRULE& natFw = *it;
|
---|
311 | if ( natFw.Pfr.iPfrProto == r.Pfr.iPfrProto
|
---|
312 | && natFw.Pfr.u16PfrHostPort == r.Pfr.u16PfrHostPort
|
---|
313 | && (strncmp(natFw.Pfr.szPfrHostAddr, r.Pfr.szPfrHostAddr, INET6_ADDRSTRLEN) == 0)
|
---|
314 | && natFw.Pfr.u16PfrGuestPort == r.Pfr.u16PfrGuestPort
|
---|
315 | && (strncmp(natFw.Pfr.szPfrGuestAddr, r.Pfr.szPfrGuestAddr, INET6_ADDRSTRLEN) == 0))
|
---|
316 | {
|
---|
317 | fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
|
---|
318 | if (!pFwCopy)
|
---|
319 | {
|
---|
320 | break;
|
---|
321 | }
|
---|
322 | memcpy(pFwCopy, &natFw.FWSpec, sizeof(fwspec));
|
---|
323 |
|
---|
324 | /* We shouldn't care about pFwCopy this memory will be freed when
|
---|
325 | * will message will arrive to the destination.
|
---|
326 | */
|
---|
327 | portfwd_rule_del(pFwCopy);
|
---|
328 |
|
---|
329 | rules.erase(it);
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | } /* loop over vector elements */
|
---|
333 | } /* condition add or delete */
|
---|
334 | port_forward_done:
|
---|
335 | /* clean up strings */
|
---|
336 | name.setNull();
|
---|
337 | strHostAddr.setNull();
|
---|
338 | strGuestAddr.setNull();
|
---|
339 | break;
|
---|
340 | }
|
---|
341 |
|
---|
342 | case VBoxEventType_OnHostNameResolutionConfigurationChange:
|
---|
343 | {
|
---|
344 | const char **ppcszNameServers = getHostNameservers();
|
---|
345 | err_t error;
|
---|
346 |
|
---|
347 | error = tcpip_callback_with_block(pxdns_set_nameservers,
|
---|
348 | ppcszNameServers,
|
---|
349 | /* :block */ 0);
|
---|
350 | if (error != ERR_OK && ppcszNameServers != NULL)
|
---|
351 | {
|
---|
352 | RTMemFree(ppcszNameServers);
|
---|
353 | }
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | }
|
---|
357 | return hrc;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | void VBoxNetLwipNAT::onLwipTcpIpInit(void* arg)
|
---|
362 | {
|
---|
363 | AssertPtrReturnVoid(arg);
|
---|
364 | VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(arg);
|
---|
365 |
|
---|
366 | HRESULT hrc = com::Initialize();
|
---|
367 | Assert(!FAILED(hrc));
|
---|
368 |
|
---|
369 | proxy_arp_hook = pxremap_proxy_arp;
|
---|
370 | proxy_ip4_divert_hook = pxremap_ip4_divert;
|
---|
371 |
|
---|
372 | proxy_na_hook = pxremap_proxy_na;
|
---|
373 | proxy_ip6_divert_hook = pxremap_ip6_divert;
|
---|
374 |
|
---|
375 | /* lwip thread */
|
---|
376 | RTNETADDRIPV4 network;
|
---|
377 | RTNETADDRIPV4 address = g_pLwipNat->getIpv4Address();
|
---|
378 | RTNETADDRIPV4 netmask = g_pLwipNat->getIpv4Netmask();
|
---|
379 | network.u = address.u & netmask.u;
|
---|
380 |
|
---|
381 | ip_addr LwipIpAddr, LwipIpNetMask, LwipIpNetwork;
|
---|
382 |
|
---|
383 | memcpy(&LwipIpAddr, &address, sizeof(ip_addr));
|
---|
384 | memcpy(&LwipIpNetMask, &netmask, sizeof(ip_addr));
|
---|
385 | memcpy(&LwipIpNetwork, &network, sizeof(ip_addr));
|
---|
386 |
|
---|
387 | netif *pNetif = netif_add(&g_pLwipNat->m_LwipNetIf /* Lwip Interface */,
|
---|
388 | &LwipIpAddr /* IP address*/,
|
---|
389 | &LwipIpNetMask /* Network mask */,
|
---|
390 | &LwipIpAddr /* gateway address, @todo: is self IP acceptable? */,
|
---|
391 | g_pLwipNat /* state */,
|
---|
392 | VBoxNetLwipNAT::netifInit /* netif_init_fn */,
|
---|
393 | lwip_tcpip_input /* netif_input_fn */);
|
---|
394 |
|
---|
395 | AssertPtrReturnVoid(pNetif);
|
---|
396 |
|
---|
397 | netif_set_up(pNetif);
|
---|
398 | netif_set_link_up(pNetif);
|
---|
399 |
|
---|
400 | if (pNat->m_ProxyOptions.ipv6_enabled) {
|
---|
401 | /*
|
---|
402 | * XXX: lwIP currently only ever calls mld6_joingroup() in
|
---|
403 | * nd6_tmr() for fresh tentative addresses, which is a wrong place
|
---|
404 | * to do it - but I'm not keen on fixing this properly for now
|
---|
405 | * (with correct handling of interface up and down transitions,
|
---|
406 | * etc). So stick it here as a kludge.
|
---|
407 | */
|
---|
408 | for (int i = 0; i <= 1; ++i) {
|
---|
409 | ip6_addr_t *paddr = netif_ip6_addr(pNetif, i);
|
---|
410 |
|
---|
411 | ip6_addr_t solicited_node_multicast_address;
|
---|
412 | ip6_addr_set_solicitednode(&solicited_node_multicast_address,
|
---|
413 | paddr->addr[3]);
|
---|
414 | mld6_joingroup(paddr, &solicited_node_multicast_address);
|
---|
415 | }
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * XXX: We must join the solicited-node multicast for the
|
---|
419 | * addresses we do IPv6 NA-proxy for. We map IPv6 loopback to
|
---|
420 | * proxy address + 1. We only need the low 24 bits, and those are
|
---|
421 | * fixed.
|
---|
422 | */
|
---|
423 | {
|
---|
424 | ip6_addr_t solicited_node_multicast_address;
|
---|
425 |
|
---|
426 | ip6_addr_set_solicitednode(&solicited_node_multicast_address,
|
---|
427 | /* last 24 bits of the address */
|
---|
428 | PP_HTONL(0x00000002));
|
---|
429 | mld6_netif_joingroup(pNetif, &solicited_node_multicast_address);
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | proxy_init(&g_pLwipNat->m_LwipNetIf, &g_pLwipNat->m_ProxyOptions);
|
---|
434 |
|
---|
435 | natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule4);
|
---|
436 | natServiceProcessRegisteredPf(g_pLwipNat->m_vecPortForwardRule6);
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | void VBoxNetLwipNAT::onLwipTcpIpFini(void* arg)
|
---|
441 | {
|
---|
442 | AssertPtrReturnVoid(arg);
|
---|
443 | VBoxNetLwipNAT *pThis = (VBoxNetLwipNAT *)arg;
|
---|
444 |
|
---|
445 | /* XXX: proxy finalization */
|
---|
446 | netif_set_link_down(&g_pLwipNat->m_LwipNetIf);
|
---|
447 | netif_set_down(&g_pLwipNat->m_LwipNetIf);
|
---|
448 | netif_remove(&g_pLwipNat->m_LwipNetIf);
|
---|
449 |
|
---|
450 | }
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * Callback for netif_add() to initialize the interface.
|
---|
454 | */
|
---|
455 | err_t VBoxNetLwipNAT::netifInit(netif *pNetif)
|
---|
456 | {
|
---|
457 | err_t rcLwip = ERR_OK;
|
---|
458 |
|
---|
459 | AssertPtrReturn(pNetif, ERR_ARG);
|
---|
460 |
|
---|
461 | VBoxNetLwipNAT *pNat = static_cast<VBoxNetLwipNAT *>(pNetif->state);
|
---|
462 | AssertPtrReturn(pNat, ERR_ARG);
|
---|
463 |
|
---|
464 | LogFlowFunc(("ENTER: pNetif[%c%c%d]\n", pNetif->name[0], pNetif->name[1], pNetif->num));
|
---|
465 | /* validity */
|
---|
466 | AssertReturn( pNetif->name[0] == 'N'
|
---|
467 | && pNetif->name[1] == 'T', ERR_ARG);
|
---|
468 |
|
---|
469 |
|
---|
470 | pNetif->hwaddr_len = sizeof(RTMAC);
|
---|
471 | RTMAC mac = g_pLwipNat->getMacAddress();
|
---|
472 | memcpy(pNetif->hwaddr, &mac, sizeof(RTMAC));
|
---|
473 |
|
---|
474 | pNat->m_u16Mtu = 1500; // XXX: FIXME
|
---|
475 | pNetif->mtu = pNat->m_u16Mtu;
|
---|
476 |
|
---|
477 | pNetif->flags = NETIF_FLAG_BROADCAST
|
---|
478 | | NETIF_FLAG_ETHARP /* Don't bother driver with ARP and let Lwip resolve ARP handling */
|
---|
479 | | NETIF_FLAG_ETHERNET; /* Lwip works with ethernet too */
|
---|
480 |
|
---|
481 | pNetif->linkoutput = netifLinkoutput; /* ether-level-pipe */
|
---|
482 | pNetif->output = lwip_etharp_output; /* ip-pipe */
|
---|
483 |
|
---|
484 | if (pNat->m_ProxyOptions.ipv6_enabled) {
|
---|
485 | pNetif->output_ip6 = ethip6_output;
|
---|
486 |
|
---|
487 | /* IPv6 link-local address in slot 0 */
|
---|
488 | netif_create_ip6_linklocal_address(pNetif, /* :from_mac_48bit */ 1);
|
---|
489 | netif_ip6_addr_set_state(pNetif, 0, IP6_ADDR_PREFERRED); // skip DAD
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * RFC 4193 Locally Assigned Global ID (ULA) in slot 1
|
---|
493 | * [fd17:625c:f037:XXXX::1] where XXXX, 16 bit Subnet ID, are two
|
---|
494 | * bytes from the middle of the IPv4 address, e.g. :dead: for
|
---|
495 | * 10.222.173.1
|
---|
496 | */
|
---|
497 | u8_t nethi = ip4_addr2(&pNetif->ip_addr);
|
---|
498 | u8_t netlo = ip4_addr3(&pNetif->ip_addr);
|
---|
499 |
|
---|
500 | ip6_addr_t *paddr = netif_ip6_addr(pNetif, 1);
|
---|
501 | IP6_ADDR(paddr, 0, 0xFD, 0x17, 0x62, 0x5C);
|
---|
502 | IP6_ADDR(paddr, 1, 0xF0, 0x37, nethi, netlo);
|
---|
503 | IP6_ADDR(paddr, 2, 0x00, 0x00, 0x00, 0x00);
|
---|
504 | IP6_ADDR(paddr, 3, 0x00, 0x00, 0x00, 0x01);
|
---|
505 | netif_ip6_addr_set_state(pNetif, 1, IP6_ADDR_PREFERRED);
|
---|
506 |
|
---|
507 | #if LWIP_IPV6_SEND_ROUTER_SOLICIT
|
---|
508 | pNetif->rs_count = 0;
|
---|
509 | #endif
|
---|
510 | }
|
---|
511 |
|
---|
512 | LogFlowFunc(("LEAVE: %d\n", rcLwip));
|
---|
513 | return rcLwip;
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | err_t VBoxNetLwipNAT::netifLinkoutput(netif *pNetif, pbuf *pPBuf)
|
---|
518 | {
|
---|
519 | AssertPtrReturn(pNetif, ERR_ARG);
|
---|
520 | AssertPtrReturn(pPBuf, ERR_ARG);
|
---|
521 | AssertReturn((void *)g_pLwipNat == pNetif->state, ERR_ARG);
|
---|
522 |
|
---|
523 | LogFlowFunc(("ENTER: pNetif[%c%c%d], pPbuf:%p\n",
|
---|
524 | pNetif->name[0],
|
---|
525 | pNetif->name[1],
|
---|
526 | pNetif->num,
|
---|
527 | pPBuf));
|
---|
528 |
|
---|
529 | RT_ZERO(VBoxNetLwipNAT::aXmitSeg);
|
---|
530 |
|
---|
531 | unsigned idx = 0;
|
---|
532 | for( struct pbuf *pPBufPtr = pPBuf;
|
---|
533 | pPBufPtr;
|
---|
534 | pPBufPtr = pPBufPtr->next, ++idx)
|
---|
535 | {
|
---|
536 | AssertReturn(idx < RT_ELEMENTS(VBoxNetLwipNAT::aXmitSeg), ERR_MEM);
|
---|
537 | VBoxNetLwipNAT::aXmitSeg[idx].pv = pPBufPtr->payload;
|
---|
538 | VBoxNetLwipNAT::aXmitSeg[idx].cb = pPBufPtr->len;
|
---|
539 | }
|
---|
540 |
|
---|
541 | int rc = g_pLwipNat->sendBufferOnWire(VBoxNetLwipNAT::aXmitSeg, idx, pPBuf->tot_len);
|
---|
542 | AssertRCReturn(rc, ERR_IF);
|
---|
543 |
|
---|
544 | g_pLwipNat->flushWire();
|
---|
545 |
|
---|
546 | LogFlowFunc(("LEAVE: %d\n", ERR_OK));
|
---|
547 | return ERR_OK;
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | VBoxNetLwipNAT::VBoxNetLwipNAT(SOCKET icmpsock4, SOCKET icmpsock6) : VBoxNetBaseService("VBoxNetNAT", "nat-network")
|
---|
552 | {
|
---|
553 | LogFlowFuncEnter();
|
---|
554 |
|
---|
555 | m_ProxyOptions.ipv6_enabled = 0;
|
---|
556 | m_ProxyOptions.ipv6_defroute = 0;
|
---|
557 | m_ProxyOptions.icmpsock4 = icmpsock4;
|
---|
558 | m_ProxyOptions.icmpsock6 = icmpsock6;
|
---|
559 | m_ProxyOptions.tftp_root = NULL;
|
---|
560 | m_ProxyOptions.src4 = NULL;
|
---|
561 | m_ProxyOptions.src6 = NULL;
|
---|
562 | memset(&m_src4, 0, sizeof(m_src4));
|
---|
563 | memset(&m_src6, 0, sizeof(m_src6));
|
---|
564 | m_src4.sin_family = AF_INET;
|
---|
565 | m_src6.sin6_family = AF_INET6;
|
---|
566 | #if HAVE_SA_LEN
|
---|
567 | m_src4.sin_len = sizeof(m_src4);
|
---|
568 | m_src6.sin6_len = sizeof(m_src6);
|
---|
569 | #endif
|
---|
570 | m_ProxyOptions.nameservers = NULL;
|
---|
571 |
|
---|
572 | m_LwipNetIf.name[0] = 'N';
|
---|
573 | m_LwipNetIf.name[1] = 'T';
|
---|
574 |
|
---|
575 | RTMAC mac;
|
---|
576 | mac.au8[0] = 0x52;
|
---|
577 | mac.au8[1] = 0x54;
|
---|
578 | mac.au8[2] = 0;
|
---|
579 | mac.au8[3] = 0x12;
|
---|
580 | mac.au8[4] = 0x35;
|
---|
581 | mac.au8[5] = 0;
|
---|
582 | setMacAddress(mac);
|
---|
583 |
|
---|
584 | RTNETADDRIPV4 address;
|
---|
585 | address.u = RT_MAKE_U32_FROM_U8( 10, 0, 2, 2); // NB: big-endian
|
---|
586 | setIpv4Address(address);
|
---|
587 |
|
---|
588 | address.u = RT_H2N_U32_C(0xffffff00);
|
---|
589 | setIpv4Netmask(address);
|
---|
590 |
|
---|
591 | fDontLoadRulesOnStartup = false;
|
---|
592 |
|
---|
593 | for(unsigned int i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i)
|
---|
594 | addCommandLineOption(&g_aGetOptDef[i]);
|
---|
595 |
|
---|
596 | LogFlowFuncLeave();
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | VBoxNetLwipNAT::~VBoxNetLwipNAT()
|
---|
601 | {
|
---|
602 | if (m_ProxyOptions.tftp_root != NULL)
|
---|
603 | {
|
---|
604 | RTStrFree((char *)m_ProxyOptions.tftp_root);
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 |
|
---|
609 | int VBoxNetLwipNAT::natServicePfRegister(NATSEVICEPORTFORWARDRULE& natPf)
|
---|
610 | {
|
---|
611 | int lrc = 0;
|
---|
612 | int rc = VINF_SUCCESS;
|
---|
613 | int socketSpec = SOCK_STREAM;
|
---|
614 | char *pszHostAddr;
|
---|
615 | int sockFamily = (natPf.Pfr.fPfrIPv6 ? PF_INET6 : PF_INET);
|
---|
616 |
|
---|
617 | switch(natPf.Pfr.iPfrProto)
|
---|
618 | {
|
---|
619 | case IPPROTO_TCP:
|
---|
620 | socketSpec = SOCK_STREAM;
|
---|
621 | break;
|
---|
622 | case IPPROTO_UDP:
|
---|
623 | socketSpec = SOCK_DGRAM;
|
---|
624 | break;
|
---|
625 | default:
|
---|
626 | return VERR_IGNORED; /* Ah, just ignore the garbage */
|
---|
627 | }
|
---|
628 |
|
---|
629 | pszHostAddr = natPf.Pfr.szPfrHostAddr;
|
---|
630 |
|
---|
631 | /* XXX: workaround for inet_pton and an empty ipv4 address
|
---|
632 | * in rule declaration.
|
---|
633 | */
|
---|
634 | if ( sockFamily == PF_INET
|
---|
635 | && pszHostAddr[0] == 0)
|
---|
636 | pszHostAddr = (char *)"0.0.0.0"; /* XXX: fix it! without type cast */
|
---|
637 |
|
---|
638 |
|
---|
639 | lrc = fwspec_set(&natPf.FWSpec,
|
---|
640 | sockFamily,
|
---|
641 | socketSpec,
|
---|
642 | pszHostAddr,
|
---|
643 | natPf.Pfr.u16PfrHostPort,
|
---|
644 | natPf.Pfr.szPfrGuestAddr,
|
---|
645 | natPf.Pfr.u16PfrGuestPort);
|
---|
646 |
|
---|
647 | AssertReturn(!lrc, VERR_IGNORED);
|
---|
648 |
|
---|
649 | fwspec *pFwCopy = (fwspec *)RTMemAllocZ(sizeof(fwspec));
|
---|
650 | AssertPtrReturn(pFwCopy, VERR_IGNORED);
|
---|
651 |
|
---|
652 | /*
|
---|
653 | * We need pass the copy, because we can't be sure
|
---|
654 | * how much this pointer will be valid in LWIP environment.
|
---|
655 | */
|
---|
656 | memcpy(pFwCopy, &natPf.FWSpec, sizeof(fwspec));
|
---|
657 |
|
---|
658 | lrc = portfwd_rule_add(pFwCopy);
|
---|
659 |
|
---|
660 | AssertReturn(!lrc, VERR_IGNORED);
|
---|
661 |
|
---|
662 | return VINF_SUCCESS;
|
---|
663 | }
|
---|
664 |
|
---|
665 |
|
---|
666 | int VBoxNetLwipNAT::natServiceProcessRegisteredPf(VECNATSERVICEPF& vecRules){
|
---|
667 | ITERATORNATSERVICEPF it;
|
---|
668 | for (it = vecRules.begin();
|
---|
669 | it != vecRules.end(); ++it)
|
---|
670 | {
|
---|
671 | int rc = natServicePfRegister((*it));
|
---|
672 | if (RT_FAILURE(rc))
|
---|
673 | {
|
---|
674 | LogRel(("PF: %s is ignored\n", (*it).Pfr.szPfrName));
|
---|
675 | continue;
|
---|
676 | }
|
---|
677 | }
|
---|
678 | return VINF_SUCCESS;
|
---|
679 | }
|
---|
680 |
|
---|
681 |
|
---|
682 | /** This method executed on main thread, only at the end threr're one threads started explcitly (LWIP and later in ::run()
|
---|
683 | * RECV)
|
---|
684 | */
|
---|
685 | int VBoxNetLwipNAT::init()
|
---|
686 | {
|
---|
687 | LogFlowFuncEnter();
|
---|
688 |
|
---|
689 | /* virtualbox initialized in super class */
|
---|
690 | int rc = ::VBoxNetBaseService::init();
|
---|
691 | AssertRCReturn(rc, rc);
|
---|
692 |
|
---|
693 | std::string networkName = getNetwork();
|
---|
694 | rc = findNatNetwork(virtualbox, networkName, m_net);
|
---|
695 | AssertRCReturn(rc, rc);
|
---|
696 |
|
---|
697 | ComEventTypeArray aNetEvents;
|
---|
698 | aNetEvents.push_back(VBoxEventType_OnNATNetworkPortForward);
|
---|
699 | aNetEvents.push_back(VBoxEventType_OnNATNetworkSetting);
|
---|
700 | rc = createNatListener(m_listener, virtualbox, this, aNetEvents);
|
---|
701 | AssertRCReturn(rc, rc);
|
---|
702 |
|
---|
703 |
|
---|
704 | // resolver changes are reported on vbox but are retrieved from
|
---|
705 | // host so stash a pointer for future lookups
|
---|
706 | HRESULT hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam());
|
---|
707 | AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
|
---|
708 |
|
---|
709 | ComEventTypeArray aVBoxEvents;
|
---|
710 | aVBoxEvents.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
|
---|
711 | rc = createNatListener(m_vboxListener, virtualbox, this, aVBoxEvents);
|
---|
712 | AssertRCReturn(rc, rc);
|
---|
713 |
|
---|
714 | BOOL fIPv6Enabled = FALSE;
|
---|
715 | hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
|
---|
716 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
717 |
|
---|
718 | BOOL fIPv6DefaultRoute = FALSE;
|
---|
719 | if (fIPv6Enabled)
|
---|
720 | {
|
---|
721 | hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
|
---|
722 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
723 | }
|
---|
724 |
|
---|
725 | m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
|
---|
726 | m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
|
---|
727 |
|
---|
728 |
|
---|
729 | com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4", networkName.c_str());
|
---|
730 | com::Bstr bstrSourceIpX;
|
---|
731 | hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
|
---|
732 | if (SUCCEEDED(hrc))
|
---|
733 | {
|
---|
734 | RTNETADDRIPV4 addr;
|
---|
735 | rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
|
---|
736 | if (RT_SUCCESS(rc))
|
---|
737 | {
|
---|
738 | RT_ZERO(m_src4);
|
---|
739 |
|
---|
740 | m_src4.sin_addr.s_addr = addr.u;
|
---|
741 | m_ProxyOptions.src4 = &m_src4;
|
---|
742 |
|
---|
743 | bstrSourceIpX.setNull();
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | if (!fDontLoadRulesOnStartup)
|
---|
748 | {
|
---|
749 | fetchNatPortForwardRules(m_net, false, m_vecPortForwardRule4);
|
---|
750 | fetchNatPortForwardRules(m_net, true, m_vecPortForwardRule6);
|
---|
751 | } /* if (!fDontLoadRulesOnStartup) */
|
---|
752 |
|
---|
753 | AddressToOffsetMapping tmp;
|
---|
754 | rc = localMappings(m_net, tmp);
|
---|
755 | if (RT_SUCCESS(rc) && tmp.size() != 0)
|
---|
756 | {
|
---|
757 | unsigned long i = 0;
|
---|
758 | for (AddressToOffsetMapping::iterator it = tmp.begin();
|
---|
759 | it != tmp.begin() && i < RT_ELEMENTS(m_lo2off);
|
---|
760 | ++it, ++i)
|
---|
761 | {
|
---|
762 | ip4_addr_set_u32(&m_lo2off[i].loaddr, it->first.u);
|
---|
763 | m_lo2off[i].off = it->second;
|
---|
764 | }
|
---|
765 |
|
---|
766 | m_loOptDescriptor.lomap = m_lo2off;
|
---|
767 | m_loOptDescriptor.num_lomap = i;
|
---|
768 | m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
|
---|
769 | }
|
---|
770 |
|
---|
771 | com::Bstr bstr;
|
---|
772 | hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
|
---|
773 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
774 | if (!bstr.isEmpty())
|
---|
775 | {
|
---|
776 | com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
|
---|
777 | bstr.raw(), RTPATH_DELIMITER, "TFTP"));
|
---|
778 | char *pszStrTemp; // avoid const char ** vs char **
|
---|
779 | rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
|
---|
780 | AssertRC(rc);
|
---|
781 | m_ProxyOptions.tftp_root = pszStrTemp;
|
---|
782 | }
|
---|
783 |
|
---|
784 | m_ProxyOptions.nameservers = getHostNameservers();
|
---|
785 |
|
---|
786 | /* end of COM initialization */
|
---|
787 |
|
---|
788 | rc = g_pLwipNat->tryGoOnline();
|
---|
789 | if (RT_FAILURE(rc))
|
---|
790 | {
|
---|
791 | return rc;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /* this starts LWIP thread */
|
---|
795 | vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
|
---|
796 |
|
---|
797 | LogFlowFuncLeaveRC(rc);
|
---|
798 | return rc;
|
---|
799 | }
|
---|
800 |
|
---|
801 |
|
---|
802 | const char **VBoxNetLwipNAT::getHostNameservers()
|
---|
803 | {
|
---|
804 | HRESULT hrc;
|
---|
805 |
|
---|
806 | if (m_host.isNull())
|
---|
807 | {
|
---|
808 | return NULL;
|
---|
809 | }
|
---|
810 |
|
---|
811 | com::SafeArray<BSTR> aNameServers;
|
---|
812 | hrc = m_host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(aNameServers));
|
---|
813 | if (FAILED(hrc))
|
---|
814 | {
|
---|
815 | return NULL;
|
---|
816 | }
|
---|
817 |
|
---|
818 | const size_t cNameServers = aNameServers.size();
|
---|
819 | if (cNameServers == 0)
|
---|
820 | {
|
---|
821 | return NULL;
|
---|
822 | }
|
---|
823 |
|
---|
824 | const char **ppcszNameServers =
|
---|
825 | (const char **)RTMemAllocZ(sizeof(char *) * (cNameServers + 1));
|
---|
826 | if (ppcszNameServers == NULL)
|
---|
827 | {
|
---|
828 | return NULL;
|
---|
829 | }
|
---|
830 |
|
---|
831 | size_t idxLast = 0;
|
---|
832 | for (size_t i = 0; i < cNameServers; ++i)
|
---|
833 | {
|
---|
834 | com::Utf8Str strNameServer(aNameServers[i]);
|
---|
835 | ppcszNameServers[idxLast] = RTStrDup(strNameServer.c_str());
|
---|
836 | if (ppcszNameServers[idxLast] != NULL)
|
---|
837 | {
|
---|
838 | ++idxLast;
|
---|
839 | }
|
---|
840 | }
|
---|
841 |
|
---|
842 | if (idxLast == 0)
|
---|
843 | {
|
---|
844 | RTMemFree(ppcszNameServers);
|
---|
845 | return NULL;
|
---|
846 | }
|
---|
847 |
|
---|
848 | return ppcszNameServers;
|
---|
849 | }
|
---|
850 |
|
---|
851 |
|
---|
852 | int VBoxNetLwipNAT::parseOpt(int rc, const RTGETOPTUNION& Val)
|
---|
853 | {
|
---|
854 | switch (rc)
|
---|
855 | {
|
---|
856 | case 'p':
|
---|
857 | case 'P':
|
---|
858 | {
|
---|
859 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
860 | VECNATSERVICEPF& rules = (rc == 'P'?
|
---|
861 | m_vecPortForwardRule6
|
---|
862 | : m_vecPortForwardRule4);
|
---|
863 |
|
---|
864 | fDontLoadRulesOnStartup = true;
|
---|
865 |
|
---|
866 | RT_ZERO(Rule);
|
---|
867 |
|
---|
868 | int irc = netPfStrToPf(Val.psz, (rc == 'P'), &Rule.Pfr);
|
---|
869 | rules.push_back(Rule);
|
---|
870 | return VINF_SUCCESS;
|
---|
871 | }
|
---|
872 | default:;
|
---|
873 | }
|
---|
874 | return VERR_NOT_FOUND;
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | int VBoxNetLwipNAT::processFrame(void *pvFrame, size_t cbFrame)
|
---|
879 | {
|
---|
880 | AssertReturn(pvFrame && cbFrame, VERR_INVALID_PARAMETER);
|
---|
881 |
|
---|
882 | struct pbuf *pPbufHdr, *pPbuf;
|
---|
883 | pPbufHdr = pPbuf = pbuf_alloc(PBUF_RAW, cbFrame, PBUF_POOL);
|
---|
884 |
|
---|
885 | AssertMsgReturn(pPbuf, ("NAT: Can't allocate send buffer cbFrame=%u\n", cbFrame), VERR_INTERNAL_ERROR);
|
---|
886 | AssertReturn(pPbufHdr->tot_len == cbFrame, VERR_INTERNAL_ERROR);
|
---|
887 |
|
---|
888 | uint8_t *pu8Frame = (uint8_t *)pvFrame;
|
---|
889 | while(pPbuf)
|
---|
890 | {
|
---|
891 | memcpy(pPbuf->payload, pu8Frame, pPbuf->len);
|
---|
892 | pu8Frame += pPbuf->len;
|
---|
893 | pPbuf = pPbuf->next;
|
---|
894 | }
|
---|
895 |
|
---|
896 | m_LwipNetIf.input(pPbufHdr, &m_LwipNetIf);
|
---|
897 |
|
---|
898 | return VINF_SUCCESS;
|
---|
899 | }
|
---|
900 |
|
---|
901 |
|
---|
902 | int VBoxNetLwipNAT::processGSO(PCPDMNETWORKGSO pGso, size_t cbFrame)
|
---|
903 | {
|
---|
904 | if (!PDMNetGsoIsValid(pGso, cbFrame,
|
---|
905 | cbFrame - sizeof(PDMNETWORKGSO)))
|
---|
906 | return VERR_INVALID_PARAMETER;
|
---|
907 |
|
---|
908 | cbFrame -= sizeof(PDMNETWORKGSO);
|
---|
909 | uint8_t abHdrScratch[256];
|
---|
910 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso,
|
---|
911 | cbFrame);
|
---|
912 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
913 | {
|
---|
914 | uint32_t cbSegFrame;
|
---|
915 | void *pvSegFrame =
|
---|
916 | PDMNetGsoCarveSegmentQD(pGso,
|
---|
917 | (uint8_t *)(pGso + 1),
|
---|
918 | cbFrame,
|
---|
919 | abHdrScratch,
|
---|
920 | iSeg,
|
---|
921 | cSegs,
|
---|
922 | &cbSegFrame);
|
---|
923 |
|
---|
924 | struct pbuf *pPbuf = pbuf_alloc(PBUF_RAW, cbSegFrame, PBUF_POOL);
|
---|
925 |
|
---|
926 | AssertMsgReturn(pPbuf, ("NAT: Can't allocate send buffer cbFrame=%u\n", cbSegFrame), VERR_INTERNAL_ERROR);
|
---|
927 | AssertReturn(!pPbuf->next && pPbuf->len == cbSegFrame, VERR_INTERNAL_ERROR);
|
---|
928 |
|
---|
929 | memcpy(pPbuf->payload, pvSegFrame, cbSegFrame);
|
---|
930 | m_LwipNetIf.input(pPbuf, &g_pLwipNat->m_LwipNetIf);
|
---|
931 | }
|
---|
932 |
|
---|
933 | return VINF_SUCCESS;
|
---|
934 | }
|
---|
935 |
|
---|
936 |
|
---|
937 | int VBoxNetLwipNAT::run()
|
---|
938 | {
|
---|
939 | /* Father starts receiving thread and enter event loop. */
|
---|
940 | VBoxNetBaseService::run();
|
---|
941 |
|
---|
942 | vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
|
---|
943 |
|
---|
944 | m_vecPortForwardRule4.clear();
|
---|
945 | m_vecPortForwardRule6.clear();
|
---|
946 |
|
---|
947 | return VINF_SUCCESS;
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | /**
|
---|
952 | * Entry point.
|
---|
953 | */
|
---|
954 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
955 | {
|
---|
956 | LogFlowFuncEnter();
|
---|
957 |
|
---|
958 | NOREF(envp);
|
---|
959 |
|
---|
960 | #ifdef RT_OS_WINDOWS
|
---|
961 | WSADATA wsaData;
|
---|
962 | int err;
|
---|
963 |
|
---|
964 | err = WSAStartup(MAKEWORD(2,2), &wsaData);
|
---|
965 | if (err)
|
---|
966 | {
|
---|
967 | fprintf(stderr, "wsastartup: failed (%d)\n", err);
|
---|
968 | return 1;
|
---|
969 | }
|
---|
970 | #endif
|
---|
971 |
|
---|
972 | SOCKET icmpsock4 = INVALID_SOCKET;
|
---|
973 | SOCKET icmpsock6 = INVALID_SOCKET;
|
---|
974 | #ifndef RT_OS_DARWIN
|
---|
975 | const int icmpstype = SOCK_RAW;
|
---|
976 | #else
|
---|
977 | /* on OS X it's not privileged */
|
---|
978 | const int icmpstype = SOCK_DGRAM;
|
---|
979 | #endif
|
---|
980 |
|
---|
981 | icmpsock4 = socket(AF_INET, icmpstype, IPPROTO_ICMP);
|
---|
982 | if (icmpsock4 == INVALID_SOCKET)
|
---|
983 | {
|
---|
984 | perror("IPPROTO_ICMP");
|
---|
985 | }
|
---|
986 |
|
---|
987 | if (icmpsock4 != INVALID_SOCKET)
|
---|
988 | {
|
---|
989 | #ifdef ICMP_FILTER // Linux specific
|
---|
990 | struct icmp_filter flt = {
|
---|
991 | ~(uint32_t)(
|
---|
992 | (1U << ICMP_ECHOREPLY)
|
---|
993 | | (1U << ICMP_DEST_UNREACH)
|
---|
994 | | (1U << ICMP_TIME_EXCEEDED)
|
---|
995 | )
|
---|
996 | };
|
---|
997 |
|
---|
998 | int status = setsockopt(icmpsock4, SOL_RAW, ICMP_FILTER,
|
---|
999 | &flt, sizeof(flt));
|
---|
1000 | if (status < 0)
|
---|
1001 | {
|
---|
1002 | perror("ICMP_FILTER");
|
---|
1003 | }
|
---|
1004 | #endif
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | icmpsock6 = socket(AF_INET6, icmpstype, IPPROTO_ICMPV6);
|
---|
1008 | if (icmpsock6 == INVALID_SOCKET)
|
---|
1009 | {
|
---|
1010 | perror("IPPROTO_ICMPV6");
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | if (icmpsock6 != INVALID_SOCKET)
|
---|
1014 | {
|
---|
1015 | #ifdef ICMP6_FILTER // Windows doesn't support RFC 3542 API
|
---|
1016 | /*
|
---|
1017 | * XXX: We do this here for now, not in pxping.c, to avoid
|
---|
1018 | * name clashes between lwIP and system headers.
|
---|
1019 | */
|
---|
1020 | struct icmp6_filter flt;
|
---|
1021 | ICMP6_FILTER_SETBLOCKALL(&flt);
|
---|
1022 |
|
---|
1023 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &flt);
|
---|
1024 |
|
---|
1025 | ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &flt);
|
---|
1026 | ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &flt);
|
---|
1027 | ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &flt);
|
---|
1028 | ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &flt);
|
---|
1029 |
|
---|
1030 | int status = setsockopt(icmpsock6, IPPROTO_ICMPV6, ICMP6_FILTER,
|
---|
1031 | &flt, sizeof(flt));
|
---|
1032 | if (status < 0)
|
---|
1033 | {
|
---|
1034 | perror("ICMP6_FILTER");
|
---|
1035 | }
|
---|
1036 | #endif
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | HRESULT hrc = com::Initialize();
|
---|
1040 | #ifdef VBOX_WITH_XPCOM
|
---|
1041 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
1042 | {
|
---|
1043 | char szHome[RTPATH_MAX] = "";
|
---|
1044 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
1045 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
1046 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
1047 | }
|
---|
1048 | #endif
|
---|
1049 | if (FAILED(hrc))
|
---|
1050 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
|
---|
1051 |
|
---|
1052 | g_pLwipNat = new VBoxNetLwipNAT(icmpsock4, icmpsock6);
|
---|
1053 |
|
---|
1054 | Log2(("NAT: initialization\n"));
|
---|
1055 | int rc = g_pLwipNat->parseArgs(argc - 1, argv + 1);
|
---|
1056 | rc = (rc == 0) ? VINF_SUCCESS : VERR_GENERAL_FAILURE; /* XXX: FIXME */
|
---|
1057 |
|
---|
1058 | if (RT_SUCCESS(rc))
|
---|
1059 | {
|
---|
1060 | rc = g_pLwipNat->init();
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | if (RT_SUCCESS(rc))
|
---|
1064 | {
|
---|
1065 | g_pLwipNat->run();
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | delete g_pLwipNat;
|
---|
1069 | return 0;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 |
|
---|
1073 | static int fetchNatPortForwardRules(const ComNatPtr& nat, bool fIsIPv6, VECNATSERVICEPF& vec)
|
---|
1074 | {
|
---|
1075 | HRESULT hrc;
|
---|
1076 | com::SafeArray<BSTR> rules;
|
---|
1077 | if (fIsIPv6)
|
---|
1078 | hrc = nat->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
|
---|
1079 | else
|
---|
1080 | hrc = nat->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
|
---|
1081 | AssertReturn(SUCCEEDED(hrc), VERR_INTERNAL_ERROR);
|
---|
1082 |
|
---|
1083 | NATSEVICEPORTFORWARDRULE Rule;
|
---|
1084 | for (size_t idxRules = 0; idxRules < rules.size(); ++idxRules)
|
---|
1085 | {
|
---|
1086 | Log(("%d-%s rule: %ls\n", idxRules, (fIsIPv6 ? "IPv6" : "IPv4"), rules[idxRules]));
|
---|
1087 | RT_ZERO(Rule);
|
---|
1088 |
|
---|
1089 | int rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 0, &Rule.Pfr);
|
---|
1090 | if (RT_FAILURE(rc))
|
---|
1091 | continue;
|
---|
1092 |
|
---|
1093 | vec.push_back(Rule);
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | return VINF_SUCCESS;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 |
|
---|
1100 | #ifndef VBOX_WITH_HARDENING
|
---|
1101 |
|
---|
1102 | int main(int argc, char **argv, char **envp)
|
---|
1103 | {
|
---|
1104 | int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
1105 | if (RT_FAILURE(rc))
|
---|
1106 | return RTMsgInitFailure(rc);
|
---|
1107 |
|
---|
1108 | return TrustedMain(argc, argv, envp);
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | # if defined(RT_OS_WINDOWS)
|
---|
1112 |
|
---|
1113 | static LRESULT CALLBACK WindowProc(HWND hwnd,
|
---|
1114 | UINT uMsg,
|
---|
1115 | WPARAM wParam,
|
---|
1116 | LPARAM lParam
|
---|
1117 | )
|
---|
1118 | {
|
---|
1119 | if(uMsg == WM_DESTROY)
|
---|
1120 | {
|
---|
1121 | PostQuitMessage(0);
|
---|
1122 | return 0;
|
---|
1123 | }
|
---|
1124 | return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | static LPCWSTR g_WndClassName = L"VBoxNetNatLwipClass";
|
---|
1128 |
|
---|
1129 | static DWORD WINAPI MsgThreadProc(__in LPVOID lpParameter)
|
---|
1130 | {
|
---|
1131 | HWND hwnd = 0;
|
---|
1132 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
|
---|
1133 | bool bExit = false;
|
---|
1134 |
|
---|
1135 | /* Register the Window Class. */
|
---|
1136 | WNDCLASS wc;
|
---|
1137 | wc.style = 0;
|
---|
1138 | wc.lpfnWndProc = WindowProc;
|
---|
1139 | wc.cbClsExtra = 0;
|
---|
1140 | wc.cbWndExtra = sizeof(void *);
|
---|
1141 | wc.hInstance = hInstance;
|
---|
1142 | wc.hIcon = NULL;
|
---|
1143 | wc.hCursor = NULL;
|
---|
1144 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
1145 | wc.lpszMenuName = NULL;
|
---|
1146 | wc.lpszClassName = g_WndClassName;
|
---|
1147 |
|
---|
1148 | ATOM atomWindowClass = RegisterClass(&wc);
|
---|
1149 |
|
---|
1150 | if (atomWindowClass != 0)
|
---|
1151 | {
|
---|
1152 | /* Create the window. */
|
---|
1153 | hwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
1154 | g_WndClassName, g_WndClassName,
|
---|
1155 | WS_POPUPWINDOW,
|
---|
1156 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
1157 |
|
---|
1158 | if (hwnd)
|
---|
1159 | {
|
---|
1160 | SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
1161 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
1162 |
|
---|
1163 | MSG msg;
|
---|
1164 | while (GetMessage(&msg, NULL, 0, 0))
|
---|
1165 | {
|
---|
1166 | TranslateMessage(&msg);
|
---|
1167 | DispatchMessage(&msg);
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | DestroyWindow (hwnd);
|
---|
1171 |
|
---|
1172 | bExit = true;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | UnregisterClass (g_WndClassName, hInstance);
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | if(bExit)
|
---|
1179 | {
|
---|
1180 | /* no need any accuracy here, in anyway the DHCP server usually gets terminated with TerminateProcess */
|
---|
1181 | exit(0);
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | return 0;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 |
|
---|
1188 |
|
---|
1189 | /** (We don't want a console usually.) */
|
---|
1190 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
1191 | {
|
---|
1192 | #if 0
|
---|
1193 | NOREF(hInstance); NOREF(hPrevInstance); NOREF(lpCmdLine); NOREF(nCmdShow);
|
---|
1194 |
|
---|
1195 | HANDLE hThread = CreateThread(
|
---|
1196 | NULL, /*__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, */
|
---|
1197 | 0, /*__in SIZE_T dwStackSize, */
|
---|
1198 | MsgThreadProc, /*__in LPTHREAD_START_ROUTINE lpStartAddress,*/
|
---|
1199 | NULL, /*__in_opt LPVOID lpParameter,*/
|
---|
1200 | 0, /*__in DWORD dwCreationFlags,*/
|
---|
1201 | NULL /*__out_opt LPDWORD lpThreadId*/
|
---|
1202 | );
|
---|
1203 |
|
---|
1204 | if(hThread != NULL)
|
---|
1205 | CloseHandle(hThread);
|
---|
1206 |
|
---|
1207 | #endif
|
---|
1208 | return main(__argc, __argv, environ);
|
---|
1209 | }
|
---|
1210 | # endif /* RT_OS_WINDOWS */
|
---|
1211 |
|
---|
1212 | #endif /* !VBOX_WITH_HARDENING */
|
---|