1 | /* $Id: NATNetworkImpl.cpp 81883 2019-11-15 16:31:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * INATNetwork implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 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 | #define LOG_GROUP LOG_GROUP_MAIN_NATNETWORK
|
---|
19 | #include "NetworkServiceRunner.h"
|
---|
20 | #include "DHCPServerImpl.h"
|
---|
21 | #include "NATNetworkImpl.h"
|
---|
22 | #include "AutoCaller.h"
|
---|
23 |
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #include <iprt/cpp/utils.h>
|
---|
26 | #include <iprt/cidr.h>
|
---|
27 | #include <iprt/net.h>
|
---|
28 | #include <VBox/com/array.h>
|
---|
29 | #include <VBox/com/ptr.h>
|
---|
30 | #include <VBox/settings.h>
|
---|
31 |
|
---|
32 | #include "EventImpl.h"
|
---|
33 | #include "LoggingNew.h"
|
---|
34 |
|
---|
35 | #include "VirtualBoxImpl.h"
|
---|
36 | #include <algorithm>
|
---|
37 | #include <list>
|
---|
38 |
|
---|
39 | #ifndef RT_OS_WINDOWS
|
---|
40 | # include <netinet/in.h>
|
---|
41 | #else
|
---|
42 | # define IN_LOOPBACKNET 127
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | // constructor / destructor
|
---|
47 | /////////////////////////////////////////////////////////////////////////////
|
---|
48 | struct NATNetwork::Data
|
---|
49 | {
|
---|
50 | Data()
|
---|
51 | : pVirtualBox(NULL)
|
---|
52 | , offGateway(0)
|
---|
53 | , offDhcp(0)
|
---|
54 | {
|
---|
55 | }
|
---|
56 | virtual ~Data(){}
|
---|
57 | const ComObjPtr<EventSource> pEventSource;
|
---|
58 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
59 | NATNetworkServiceRunner NATRunner;
|
---|
60 | ComObjPtr<IDHCPServer> dhcpServer;
|
---|
61 | #endif
|
---|
62 | /** weak VirtualBox parent */
|
---|
63 | VirtualBox * const pVirtualBox;
|
---|
64 |
|
---|
65 | /** NATNetwork settings */
|
---|
66 | settings::NATNetwork s;
|
---|
67 |
|
---|
68 | com::Utf8Str IPv4Gateway;
|
---|
69 | com::Utf8Str IPv4NetworkMask;
|
---|
70 | com::Utf8Str IPv4DhcpServer;
|
---|
71 | com::Utf8Str IPv4DhcpServerLowerIp;
|
---|
72 | com::Utf8Str IPv4DhcpServerUpperIp;
|
---|
73 |
|
---|
74 | uint32_t offGateway;
|
---|
75 | uint32_t offDhcp;
|
---|
76 | };
|
---|
77 |
|
---|
78 |
|
---|
79 | NATNetwork::NATNetwork()
|
---|
80 | : m(NULL)
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | NATNetwork::~NATNetwork()
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | HRESULT NATNetwork::FinalConstruct()
|
---|
91 | {
|
---|
92 | return BaseFinalConstruct();
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | void NATNetwork::FinalRelease()
|
---|
97 | {
|
---|
98 | uninit();
|
---|
99 |
|
---|
100 | BaseFinalRelease();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | void NATNetwork::uninit()
|
---|
105 | {
|
---|
106 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
107 | AutoUninitSpan autoUninitSpan(this);
|
---|
108 | if (autoUninitSpan.uninitDone())
|
---|
109 | return;
|
---|
110 | unconst(m->pVirtualBox) = NULL;
|
---|
111 | delete m;
|
---|
112 | m = NULL;
|
---|
113 | }
|
---|
114 |
|
---|
115 | HRESULT NATNetwork::init(VirtualBox *aVirtualBox, com::Utf8Str aName)
|
---|
116 | {
|
---|
117 | AutoInitSpan autoInitSpan(this);
|
---|
118 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
119 |
|
---|
120 | m = new Data();
|
---|
121 | /* share VirtualBox weakly */
|
---|
122 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
123 | m->s.strNetworkName = aName;
|
---|
124 | m->s.strIPv4NetworkCidr = "10.0.2.0/24";
|
---|
125 | m->offGateway = 1;
|
---|
126 | i_recalculateIPv6Prefix(); /* set m->strIPv6Prefix based on IPv4 */
|
---|
127 |
|
---|
128 | settings::NATHostLoopbackOffset off;
|
---|
129 | off.strLoopbackHostAddress = "127.0.0.1";
|
---|
130 | off.u32Offset = (uint32_t)2;
|
---|
131 | m->s.llHostLoopbackOffsetList.push_back(off);
|
---|
132 |
|
---|
133 | i_recalculateIpv4AddressAssignments();
|
---|
134 |
|
---|
135 | HRESULT hrc = unconst(m->pEventSource).createObject();
|
---|
136 | if (FAILED(hrc)) throw hrc;
|
---|
137 |
|
---|
138 | hrc = m->pEventSource->init();
|
---|
139 | if (FAILED(hrc)) throw hrc;
|
---|
140 |
|
---|
141 | /* Confirm a successful initialization */
|
---|
142 | autoInitSpan.setSucceeded();
|
---|
143 |
|
---|
144 | return S_OK;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | HRESULT NATNetwork::i_loadSettings(const settings::NATNetwork &data)
|
---|
149 | {
|
---|
150 | AutoCaller autoCaller(this);
|
---|
151 | AssertComRCReturnRC(autoCaller.rc());
|
---|
152 |
|
---|
153 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
154 | m->s = data;
|
---|
155 | if ( m->s.strIPv6Prefix.isEmpty()
|
---|
156 | /* also clean up bogus old default */
|
---|
157 | || m->s.strIPv6Prefix == "fe80::/64")
|
---|
158 | i_recalculateIPv6Prefix(); /* set m->strIPv6Prefix based on IPv4 */
|
---|
159 | i_recalculateIpv4AddressAssignments();
|
---|
160 |
|
---|
161 | return S_OK;
|
---|
162 | }
|
---|
163 |
|
---|
164 | HRESULT NATNetwork::i_saveSettings(settings::NATNetwork &data)
|
---|
165 | {
|
---|
166 | AutoCaller autoCaller(this);
|
---|
167 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
168 |
|
---|
169 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
170 | AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL);
|
---|
171 | data = m->s;
|
---|
172 |
|
---|
173 | m->pVirtualBox->i_onNATNetworkSetting(Bstr(m->s.strNetworkName).raw(),
|
---|
174 | m->s.fEnabled,
|
---|
175 | Bstr(m->s.strIPv4NetworkCidr).raw(),
|
---|
176 | Bstr(m->IPv4Gateway).raw(),
|
---|
177 | m->s.fAdvertiseDefaultIPv6Route,
|
---|
178 | m->s.fNeedDhcpServer);
|
---|
179 |
|
---|
180 | /* Notify listerners listening on this network only */
|
---|
181 | fireNATNetworkSettingEvent(m->pEventSource,
|
---|
182 | Bstr(m->s.strNetworkName).raw(),
|
---|
183 | m->s.fEnabled,
|
---|
184 | Bstr(m->s.strIPv4NetworkCidr).raw(),
|
---|
185 | Bstr(m->IPv4Gateway).raw(),
|
---|
186 | m->s.fAdvertiseDefaultIPv6Route,
|
---|
187 | m->s.fNeedDhcpServer);
|
---|
188 |
|
---|
189 | return S_OK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | HRESULT NATNetwork::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
193 | {
|
---|
194 | /* event source is const, no need to lock */
|
---|
195 | m->pEventSource.queryInterfaceTo(aEventSource.asOutParam());
|
---|
196 | return S_OK;
|
---|
197 | }
|
---|
198 |
|
---|
199 | HRESULT NATNetwork::getNetworkName(com::Utf8Str &aNetworkName)
|
---|
200 | {
|
---|
201 | AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL);
|
---|
202 | aNetworkName = m->s.strNetworkName;
|
---|
203 | return S_OK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | HRESULT NATNetwork::setNetworkName(const com::Utf8Str &aNetworkName)
|
---|
207 | {
|
---|
208 | if (m->s.strNetworkName.isEmpty())
|
---|
209 | return setError(E_INVALIDARG,
|
---|
210 | tr("Network name cannot be empty"));
|
---|
211 | {
|
---|
212 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
213 | if (aNetworkName == m->s.strNetworkName)
|
---|
214 | return S_OK;
|
---|
215 |
|
---|
216 | m->s.strNetworkName = aNetworkName;
|
---|
217 | }
|
---|
218 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
219 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
220 | ComAssertComRCRetRC(rc);
|
---|
221 |
|
---|
222 | return S_OK;
|
---|
223 | }
|
---|
224 |
|
---|
225 | HRESULT NATNetwork::getEnabled(BOOL *aEnabled)
|
---|
226 | {
|
---|
227 | *aEnabled = m->s.fEnabled;
|
---|
228 |
|
---|
229 | i_recalculateIpv4AddressAssignments();
|
---|
230 | return S_OK;
|
---|
231 | }
|
---|
232 |
|
---|
233 | HRESULT NATNetwork::setEnabled(const BOOL aEnabled)
|
---|
234 | {
|
---|
235 | {
|
---|
236 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
237 | if (RT_BOOL(aEnabled) == m->s.fEnabled)
|
---|
238 | return S_OK;
|
---|
239 | m->s.fEnabled = RT_BOOL(aEnabled);
|
---|
240 | }
|
---|
241 |
|
---|
242 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
243 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
244 | ComAssertComRCRetRC(rc);
|
---|
245 | return S_OK;
|
---|
246 | }
|
---|
247 |
|
---|
248 | HRESULT NATNetwork::getGateway(com::Utf8Str &aIPv4Gateway)
|
---|
249 | {
|
---|
250 | aIPv4Gateway = m->IPv4Gateway;
|
---|
251 | return S_OK;
|
---|
252 | }
|
---|
253 |
|
---|
254 | HRESULT NATNetwork::getNetwork(com::Utf8Str &aNetwork)
|
---|
255 | {
|
---|
256 | aNetwork = m->s.strIPv4NetworkCidr;
|
---|
257 | return S_OK;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | HRESULT NATNetwork::setNetwork(const com::Utf8Str &aIPv4NetworkCidr)
|
---|
262 | {
|
---|
263 | {
|
---|
264 |
|
---|
265 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
266 |
|
---|
267 | if (aIPv4NetworkCidr == m->s.strIPv4NetworkCidr)
|
---|
268 | return S_OK;
|
---|
269 |
|
---|
270 | /* silently ignore network cidr update for now.
|
---|
271 | * todo: keep internally guest address of port forward rule
|
---|
272 | * as offset from network id.
|
---|
273 | */
|
---|
274 | if (!m->s.mapPortForwardRules4.empty())
|
---|
275 | return S_OK;
|
---|
276 |
|
---|
277 |
|
---|
278 | m->s.strIPv4NetworkCidr = aIPv4NetworkCidr;
|
---|
279 | i_recalculateIpv4AddressAssignments();
|
---|
280 | }
|
---|
281 |
|
---|
282 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
283 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
284 | ComAssertComRCRetRC(rc);
|
---|
285 | return S_OK;
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | HRESULT NATNetwork::getIPv6Enabled(BOOL *aIPv6Enabled)
|
---|
290 | {
|
---|
291 | *aIPv6Enabled = m->s.fIPv6Enabled;
|
---|
292 |
|
---|
293 | return S_OK;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | HRESULT NATNetwork::setIPv6Enabled(const BOOL aIPv6Enabled)
|
---|
298 | {
|
---|
299 | {
|
---|
300 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
301 |
|
---|
302 | if (RT_BOOL(aIPv6Enabled) == m->s.fIPv6Enabled)
|
---|
303 | return S_OK;
|
---|
304 |
|
---|
305 | m->s.fIPv6Enabled = RT_BOOL(aIPv6Enabled);
|
---|
306 | }
|
---|
307 |
|
---|
308 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
309 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
310 | ComAssertComRCRetRC(rc);
|
---|
311 |
|
---|
312 | return S_OK;
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | HRESULT NATNetwork::getIPv6Prefix(com::Utf8Str &aIPv6Prefix)
|
---|
317 | {
|
---|
318 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
319 |
|
---|
320 | aIPv6Prefix = m->s.strIPv6Prefix;
|
---|
321 | return S_OK;
|
---|
322 | }
|
---|
323 |
|
---|
324 | HRESULT NATNetwork::setIPv6Prefix(const com::Utf8Str &aIPv6Prefix)
|
---|
325 | {
|
---|
326 | {
|
---|
327 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
328 |
|
---|
329 | if (aIPv6Prefix == m->s.strIPv6Prefix)
|
---|
330 | return S_OK;
|
---|
331 |
|
---|
332 | /* silently ignore network IPv6 prefix update.
|
---|
333 | * todo: see similar todo in NATNetwork::COMSETTER(Network)(IN_BSTR)
|
---|
334 | */
|
---|
335 | if (!m->s.mapPortForwardRules6.empty())
|
---|
336 | return S_OK;
|
---|
337 |
|
---|
338 | m->s.strIPv6Prefix = aIPv6Prefix;
|
---|
339 | }
|
---|
340 |
|
---|
341 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
342 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
343 | ComAssertComRCRetRC(rc);
|
---|
344 |
|
---|
345 | return S_OK;
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | HRESULT NATNetwork::getAdvertiseDefaultIPv6RouteEnabled(BOOL *aAdvertiseDefaultIPv6Route)
|
---|
350 | {
|
---|
351 | *aAdvertiseDefaultIPv6Route = m->s.fAdvertiseDefaultIPv6Route;
|
---|
352 |
|
---|
353 | return S_OK;
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | HRESULT NATNetwork::setAdvertiseDefaultIPv6RouteEnabled(const BOOL aAdvertiseDefaultIPv6Route)
|
---|
358 | {
|
---|
359 | {
|
---|
360 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
361 |
|
---|
362 | if (RT_BOOL(aAdvertiseDefaultIPv6Route) == m->s.fAdvertiseDefaultIPv6Route)
|
---|
363 | return S_OK;
|
---|
364 |
|
---|
365 | m->s.fAdvertiseDefaultIPv6Route = RT_BOOL(aAdvertiseDefaultIPv6Route);
|
---|
366 |
|
---|
367 | }
|
---|
368 |
|
---|
369 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
370 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
371 | ComAssertComRCRetRC(rc);
|
---|
372 |
|
---|
373 | return S_OK;
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | HRESULT NATNetwork::getNeedDhcpServer(BOOL *aNeedDhcpServer)
|
---|
378 | {
|
---|
379 | *aNeedDhcpServer = m->s.fNeedDhcpServer;
|
---|
380 |
|
---|
381 | return S_OK;
|
---|
382 | }
|
---|
383 |
|
---|
384 | HRESULT NATNetwork::setNeedDhcpServer(const BOOL aNeedDhcpServer)
|
---|
385 | {
|
---|
386 | {
|
---|
387 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
388 |
|
---|
389 | if (RT_BOOL(aNeedDhcpServer) == m->s.fNeedDhcpServer)
|
---|
390 | return S_OK;
|
---|
391 |
|
---|
392 | m->s.fNeedDhcpServer = RT_BOOL(aNeedDhcpServer);
|
---|
393 |
|
---|
394 | i_recalculateIpv4AddressAssignments();
|
---|
395 |
|
---|
396 | }
|
---|
397 |
|
---|
398 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
399 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
400 | ComAssertComRCRetRC(rc);
|
---|
401 |
|
---|
402 | return S_OK;
|
---|
403 | }
|
---|
404 |
|
---|
405 | HRESULT NATNetwork::getLocalMappings(std::vector<com::Utf8Str> &aLocalMappings)
|
---|
406 | {
|
---|
407 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
408 |
|
---|
409 | aLocalMappings.resize(m->s.llHostLoopbackOffsetList.size());
|
---|
410 | size_t i = 0;
|
---|
411 | for (settings::NATLoopbackOffsetList::const_iterator it = m->s.llHostLoopbackOffsetList.begin();
|
---|
412 | it != m->s.llHostLoopbackOffsetList.end(); ++it, ++i)
|
---|
413 | {
|
---|
414 | aLocalMappings[i] = Utf8StrFmt("%s=%d",
|
---|
415 | (*it).strLoopbackHostAddress.c_str(),
|
---|
416 | (*it).u32Offset);
|
---|
417 | }
|
---|
418 |
|
---|
419 | return S_OK;
|
---|
420 | }
|
---|
421 |
|
---|
422 | HRESULT NATNetwork::addLocalMapping(const com::Utf8Str &aHostId, LONG aOffset)
|
---|
423 | {
|
---|
424 | RTNETADDRIPV4 addr, net, mask;
|
---|
425 |
|
---|
426 | int rc = RTNetStrToIPv4Addr(Utf8Str(aHostId).c_str(), &addr);
|
---|
427 | if (RT_FAILURE(rc))
|
---|
428 | return E_INVALIDARG;
|
---|
429 |
|
---|
430 | /* check against 127/8 */
|
---|
431 | if ((RT_N2H_U32(addr.u) >> IN_CLASSA_NSHIFT) != IN_LOOPBACKNET)
|
---|
432 | return E_INVALIDARG;
|
---|
433 |
|
---|
434 | /* check against networkid vs network mask */
|
---|
435 | rc = RTCidrStrToIPv4(Utf8Str(m->s.strIPv4NetworkCidr).c_str(), &net, &mask);
|
---|
436 | if (RT_FAILURE(rc))
|
---|
437 | return E_INVALIDARG;
|
---|
438 |
|
---|
439 | if (((net.u + aOffset) & mask.u) != net.u)
|
---|
440 | return E_INVALIDARG;
|
---|
441 |
|
---|
442 | settings::NATLoopbackOffsetList::iterator it;
|
---|
443 |
|
---|
444 | it = std::find(m->s.llHostLoopbackOffsetList.begin(),
|
---|
445 | m->s.llHostLoopbackOffsetList.end(),
|
---|
446 | aHostId);
|
---|
447 | if (it != m->s.llHostLoopbackOffsetList.end())
|
---|
448 | {
|
---|
449 | if (aOffset == 0) /* erase */
|
---|
450 | m->s.llHostLoopbackOffsetList.erase(it, it);
|
---|
451 | else /* modify */
|
---|
452 | {
|
---|
453 | settings::NATLoopbackOffsetList::iterator it1;
|
---|
454 | it1 = std::find(m->s.llHostLoopbackOffsetList.begin(),
|
---|
455 | m->s.llHostLoopbackOffsetList.end(),
|
---|
456 | (uint32_t)aOffset);
|
---|
457 | if (it1 != m->s.llHostLoopbackOffsetList.end())
|
---|
458 | return E_INVALIDARG; /* this offset is already registered. */
|
---|
459 |
|
---|
460 | (*it).u32Offset = aOffset;
|
---|
461 | }
|
---|
462 |
|
---|
463 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
464 | return m->pVirtualBox->i_saveSettings();
|
---|
465 | }
|
---|
466 |
|
---|
467 | /* injection */
|
---|
468 | it = std::find(m->s.llHostLoopbackOffsetList.begin(),
|
---|
469 | m->s.llHostLoopbackOffsetList.end(),
|
---|
470 | (uint32_t)aOffset);
|
---|
471 |
|
---|
472 | if (it != m->s.llHostLoopbackOffsetList.end())
|
---|
473 | return E_INVALIDARG; /* offset is already registered. */
|
---|
474 |
|
---|
475 | settings::NATHostLoopbackOffset off;
|
---|
476 | off.strLoopbackHostAddress = aHostId;
|
---|
477 | off.u32Offset = (uint32_t)aOffset;
|
---|
478 | m->s.llHostLoopbackOffsetList.push_back(off);
|
---|
479 |
|
---|
480 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
481 | return m->pVirtualBox->i_saveSettings();
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | HRESULT NATNetwork::getLoopbackIp6(LONG *aLoopbackIp6)
|
---|
486 | {
|
---|
487 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
488 |
|
---|
489 | *aLoopbackIp6 = m->s.u32HostLoopback6Offset;
|
---|
490 | return S_OK;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | HRESULT NATNetwork::setLoopbackIp6(LONG aLoopbackIp6)
|
---|
495 | {
|
---|
496 | {
|
---|
497 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
498 |
|
---|
499 | if (aLoopbackIp6 < 0)
|
---|
500 | return E_INVALIDARG;
|
---|
501 |
|
---|
502 | if (static_cast<uint32_t>(aLoopbackIp6) == m->s.u32HostLoopback6Offset)
|
---|
503 | return S_OK;
|
---|
504 |
|
---|
505 | m->s.u32HostLoopback6Offset = aLoopbackIp6;
|
---|
506 | }
|
---|
507 |
|
---|
508 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
509 | return m->pVirtualBox->i_saveSettings();
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | HRESULT NATNetwork::getPortForwardRules4(std::vector<com::Utf8Str> &aPortForwardRules4)
|
---|
514 | {
|
---|
515 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
516 | i_getPortForwardRulesFromMap(aPortForwardRules4,
|
---|
517 | m->s.mapPortForwardRules4);
|
---|
518 | return S_OK;
|
---|
519 | }
|
---|
520 |
|
---|
521 | HRESULT NATNetwork::getPortForwardRules6(std::vector<com::Utf8Str> &aPortForwardRules6)
|
---|
522 | {
|
---|
523 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
524 | i_getPortForwardRulesFromMap(aPortForwardRules6,
|
---|
525 | m->s.mapPortForwardRules6);
|
---|
526 | return S_OK;
|
---|
527 | }
|
---|
528 |
|
---|
529 | HRESULT NATNetwork::addPortForwardRule(BOOL aIsIpv6,
|
---|
530 | const com::Utf8Str &aPortForwardRuleName,
|
---|
531 | NATProtocol_T aProto,
|
---|
532 | const com::Utf8Str &aHostIp,
|
---|
533 | USHORT aHostPort,
|
---|
534 | const com::Utf8Str &aGuestIp,
|
---|
535 | USHORT aGuestPort)
|
---|
536 | {
|
---|
537 | {
|
---|
538 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
539 | Utf8Str name = aPortForwardRuleName;
|
---|
540 | Utf8Str proto;
|
---|
541 | settings::NATRule r;
|
---|
542 | settings::NATRulesMap &mapRules = aIsIpv6 ? m->s.mapPortForwardRules6 : m->s.mapPortForwardRules4;
|
---|
543 | switch (aProto)
|
---|
544 | {
|
---|
545 | case NATProtocol_TCP:
|
---|
546 | proto = "tcp";
|
---|
547 | break;
|
---|
548 | case NATProtocol_UDP:
|
---|
549 | proto = "udp";
|
---|
550 | break;
|
---|
551 | default:
|
---|
552 | return E_INVALIDARG;
|
---|
553 | }
|
---|
554 | if (name.isEmpty())
|
---|
555 | name = Utf8StrFmt("%s_[%s]%%%d_[%s]%%%d", proto.c_str(),
|
---|
556 | aHostIp.c_str(), aHostPort,
|
---|
557 | aGuestIp.c_str(), aGuestPort);
|
---|
558 |
|
---|
559 | for (settings::NATRulesMap::iterator it = mapRules.begin(); it != mapRules.end(); ++it)
|
---|
560 | {
|
---|
561 | r = it->second;
|
---|
562 | if (it->first == name)
|
---|
563 | return setError(E_INVALIDARG,
|
---|
564 | tr("A NAT rule of this name already exists"));
|
---|
565 | if ( r.strHostIP == aHostIp
|
---|
566 | && r.u16HostPort == aHostPort
|
---|
567 | && r.proto == aProto)
|
---|
568 | return setError(E_INVALIDARG,
|
---|
569 | tr("A NAT rule for this host port and this host IP already exists"));
|
---|
570 | }
|
---|
571 |
|
---|
572 | r.strName = name.c_str();
|
---|
573 | r.proto = aProto;
|
---|
574 | r.strHostIP = aHostIp;
|
---|
575 | r.u16HostPort = aHostPort;
|
---|
576 | r.strGuestIP = aGuestIp;
|
---|
577 | r.u16GuestPort = aGuestPort;
|
---|
578 | mapRules.insert(std::make_pair(name, r));
|
---|
579 | }
|
---|
580 | {
|
---|
581 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
582 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
583 | ComAssertComRCRetRC(rc);
|
---|
584 | }
|
---|
585 |
|
---|
586 | m->pVirtualBox->i_onNATNetworkPortForward(Bstr(m->s.strNetworkName).raw(), TRUE, aIsIpv6,
|
---|
587 | Bstr(aPortForwardRuleName).raw(), aProto,
|
---|
588 | Bstr(aHostIp).raw(), aHostPort,
|
---|
589 | Bstr(aGuestIp).raw(), aGuestPort);
|
---|
590 |
|
---|
591 | /* Notify listerners listening on this network only */
|
---|
592 | fireNATNetworkPortForwardEvent(m->pEventSource, Bstr(m->s.strNetworkName).raw(), TRUE,
|
---|
593 | aIsIpv6, Bstr(aPortForwardRuleName).raw(), aProto,
|
---|
594 | Bstr(aHostIp).raw(), aHostPort,
|
---|
595 | Bstr(aGuestIp).raw(), aGuestPort);
|
---|
596 |
|
---|
597 | return S_OK;
|
---|
598 | }
|
---|
599 |
|
---|
600 | HRESULT NATNetwork::removePortForwardRule(BOOL aIsIpv6, const com::Utf8Str &aPortForwardRuleName)
|
---|
601 | {
|
---|
602 | Utf8Str strHostIP;
|
---|
603 | Utf8Str strGuestIP;
|
---|
604 | uint16_t u16HostPort;
|
---|
605 | uint16_t u16GuestPort;
|
---|
606 | NATProtocol_T proto;
|
---|
607 |
|
---|
608 | {
|
---|
609 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
610 | settings::NATRulesMap &mapRules = aIsIpv6 ? m->s.mapPortForwardRules6 : m->s.mapPortForwardRules4;
|
---|
611 | settings::NATRulesMap::iterator it = mapRules.find(aPortForwardRuleName);
|
---|
612 |
|
---|
613 | if (it == mapRules.end())
|
---|
614 | return E_INVALIDARG;
|
---|
615 |
|
---|
616 | strHostIP = it->second.strHostIP;
|
---|
617 | strGuestIP = it->second.strGuestIP;
|
---|
618 | u16HostPort = it->second.u16HostPort;
|
---|
619 | u16GuestPort = it->second.u16GuestPort;
|
---|
620 | proto = it->second.proto;
|
---|
621 |
|
---|
622 | mapRules.erase(it);
|
---|
623 | }
|
---|
624 |
|
---|
625 | {
|
---|
626 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
627 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
628 | ComAssertComRCRetRC(rc);
|
---|
629 | }
|
---|
630 |
|
---|
631 | m->pVirtualBox->i_onNATNetworkPortForward(Bstr(m->s.strNetworkName).raw(), FALSE, aIsIpv6,
|
---|
632 | Bstr(aPortForwardRuleName).raw(), proto,
|
---|
633 | Bstr(strHostIP).raw(), u16HostPort,
|
---|
634 | Bstr(strGuestIP).raw(), u16GuestPort);
|
---|
635 |
|
---|
636 | /* Notify listerners listening on this network only */
|
---|
637 | fireNATNetworkPortForwardEvent(m->pEventSource, Bstr(m->s.strNetworkName).raw(), FALSE,
|
---|
638 | aIsIpv6, Bstr(aPortForwardRuleName).raw(), proto,
|
---|
639 | Bstr(strHostIP).raw(), u16HostPort,
|
---|
640 | Bstr(strGuestIP).raw(), u16GuestPort);
|
---|
641 | return S_OK;
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | void NATNetwork::i_updateDomainNameOption(ComPtr<IHost> &host)
|
---|
646 | {
|
---|
647 | com::Bstr domain;
|
---|
648 | if (FAILED(host->COMGETTER(DomainName)(domain.asOutParam())))
|
---|
649 | LogRel(("NATNetwork: Failed to get host's domain name\n"));
|
---|
650 | ComPtr<IDHCPGlobalConfig> pDHCPConfig;
|
---|
651 | HRESULT hrc = m->dhcpServer->COMGETTER(GlobalConfig)(pDHCPConfig.asOutParam());
|
---|
652 | if (FAILED(hrc))
|
---|
653 | {
|
---|
654 | LogRel(("NATNetwork: Failed to get global DHCP config when updating domain name option with %Rhrc\n", hrc));
|
---|
655 | return;
|
---|
656 | }
|
---|
657 | if (domain.isNotEmpty())
|
---|
658 | {
|
---|
659 | hrc = pDHCPConfig->SetOption(DHCPOption_DomainName, DHCPOptionEncoding_Normal, domain.raw());
|
---|
660 | if (FAILED(hrc))
|
---|
661 | LogRel(("NATNetwork: Failed to add domain name option with %Rhrc\n", hrc));
|
---|
662 | }
|
---|
663 | else
|
---|
664 | pDHCPConfig->RemoveOption(DHCPOption_DomainName);
|
---|
665 | }
|
---|
666 |
|
---|
667 | void NATNetwork::i_updateDomainNameServerOption(ComPtr<IHost> &host)
|
---|
668 | {
|
---|
669 | RTNETADDRIPV4 networkid, netmask;
|
---|
670 |
|
---|
671 | int rc = RTCidrStrToIPv4(m->s.strIPv4NetworkCidr.c_str(), &networkid, &netmask);
|
---|
672 | if (RT_FAILURE(rc))
|
---|
673 | {
|
---|
674 | LogRel(("NATNetwork: Failed to parse cidr %s with %Rrc\n", m->s.strIPv4NetworkCidr.c_str(), rc));
|
---|
675 | return;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* XXX: these are returned, surprisingly, in host order */
|
---|
679 | networkid.u = RT_H2N_U32(networkid.u);
|
---|
680 | netmask.u = RT_H2N_U32(netmask.u);
|
---|
681 |
|
---|
682 | com::SafeArray<BSTR> nameServers;
|
---|
683 | HRESULT hrc = host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(nameServers));
|
---|
684 | if (FAILED(hrc))
|
---|
685 | {
|
---|
686 | LogRel(("NATNetwork: Failed to get name servers from host with %Rhrc\n", hrc));
|
---|
687 | return;
|
---|
688 | }
|
---|
689 | ComPtr<IDHCPGlobalConfig> pDHCPConfig;
|
---|
690 | hrc = m->dhcpServer->COMGETTER(GlobalConfig)(pDHCPConfig.asOutParam());
|
---|
691 | if (FAILED(hrc))
|
---|
692 | {
|
---|
693 | LogRel(("NATNetwork: Failed to get global DHCP config when updating domain name server option with %Rhrc\n", hrc));
|
---|
694 | return;
|
---|
695 | }
|
---|
696 |
|
---|
697 | size_t cAddresses = nameServers.size();
|
---|
698 | if (cAddresses)
|
---|
699 | {
|
---|
700 | RTCList<RTCString> lstServers;
|
---|
701 | /* The following code was copied (and adapted a bit) from VBoxNetDhcp::hostDnsServers */
|
---|
702 | /*
|
---|
703 | * Recent fashion is to run dnsmasq on 127.0.1.1 which we
|
---|
704 | * currently can't map. If that's the only nameserver we've got,
|
---|
705 | * we need to use DNS proxy for VMs to reach it.
|
---|
706 | */
|
---|
707 | bool fUnmappedLoopback = false;
|
---|
708 |
|
---|
709 | for (size_t i = 0; i < cAddresses; ++i)
|
---|
710 | {
|
---|
711 | RTNETADDRIPV4 addr;
|
---|
712 |
|
---|
713 | com::Utf8Str strNameServerAddress(nameServers[i]);
|
---|
714 | rc = RTNetStrToIPv4Addr(strNameServerAddress.c_str(), &addr);
|
---|
715 | if (RT_FAILURE(rc))
|
---|
716 | {
|
---|
717 | LogRel(("NATNetwork: Failed to parse IP address %s with %Rrc\n", strNameServerAddress.c_str(), rc));
|
---|
718 | continue;
|
---|
719 | }
|
---|
720 |
|
---|
721 | if (addr.u == INADDR_ANY)
|
---|
722 | {
|
---|
723 | /*
|
---|
724 | * This doesn't seem to be very well documented except for
|
---|
725 | * RTFS of res_init.c, but INADDR_ANY is a valid value for
|
---|
726 | * for "nameserver".
|
---|
727 | */
|
---|
728 | addr.u = RT_H2N_U32_C(INADDR_LOOPBACK);
|
---|
729 | }
|
---|
730 |
|
---|
731 | if (addr.au8[0] == 127)
|
---|
732 | {
|
---|
733 | settings::NATLoopbackOffsetList::const_iterator it;
|
---|
734 |
|
---|
735 | it = std::find(m->s.llHostLoopbackOffsetList.begin(),
|
---|
736 | m->s.llHostLoopbackOffsetList.end(),
|
---|
737 | strNameServerAddress);
|
---|
738 | if (it == m->s.llHostLoopbackOffsetList.end())
|
---|
739 | {
|
---|
740 | fUnmappedLoopback = true;
|
---|
741 | continue;
|
---|
742 | }
|
---|
743 | addr.u = RT_H2N_U32(RT_N2H_U32(networkid.u) + it->u32Offset);
|
---|
744 | }
|
---|
745 | lstServers.append(RTCStringFmt("%RTnaipv4", addr));
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (lstServers.isEmpty() && fUnmappedLoopback)
|
---|
749 | lstServers.append(RTCStringFmt("%RTnaipv4", networkid.u | RT_H2N_U32_C(1U))); /* proxy */
|
---|
750 |
|
---|
751 | hrc = pDHCPConfig->SetOption(DHCPOption_DomainNameServers, DHCPOptionEncoding_Normal, Bstr(RTCString::join(lstServers, " ")).raw());
|
---|
752 | if (FAILED(hrc))
|
---|
753 | LogRel(("NATNetwork: Failed to add domain name server option '%s' with %Rhrc\n", RTCString::join(lstServers, " ").c_str(), hrc));
|
---|
754 | }
|
---|
755 | else
|
---|
756 | pDHCPConfig->RemoveOption(DHCPOption_DomainNameServers);
|
---|
757 | }
|
---|
758 |
|
---|
759 | void NATNetwork::i_updateDnsOptions()
|
---|
760 | {
|
---|
761 | ComPtr<IHost> host;
|
---|
762 | if (SUCCEEDED(m->pVirtualBox->COMGETTER(Host)(host.asOutParam())))
|
---|
763 | {
|
---|
764 | i_updateDomainNameOption(host);
|
---|
765 | i_updateDomainNameServerOption(host);
|
---|
766 | }
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | HRESULT NATNetwork::start()
|
---|
771 | {
|
---|
772 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
773 | if (!m->s.fEnabled) return S_OK;
|
---|
774 | AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL);
|
---|
775 |
|
---|
776 | m->NATRunner.addArgPair(NetworkServiceRunner::kpszKeyNetwork, Utf8Str(m->s.strNetworkName).c_str());
|
---|
777 | m->NATRunner.addArgPair(NetworkServiceRunner::kpszKeyTrunkType, Utf8Str(TRUNKTYPE_WHATEVER).c_str());
|
---|
778 | m->NATRunner.addArgPair(NetworkServiceRunner::kpszIpAddress, Utf8Str(m->IPv4Gateway).c_str());
|
---|
779 | m->NATRunner.addArgPair(NetworkServiceRunner::kpszIpNetmask, Utf8Str(m->IPv4NetworkMask).c_str());
|
---|
780 |
|
---|
781 | /* No portforwarding rules from command-line, all will be fetched via API */
|
---|
782 |
|
---|
783 | if (m->s.fNeedDhcpServer)
|
---|
784 | {
|
---|
785 | /*
|
---|
786 | * Just to as idea... via API (on creation user pass the cidr of network and)
|
---|
787 | * and we calculate it's addreses (mutable?).
|
---|
788 | */
|
---|
789 |
|
---|
790 | /*
|
---|
791 | * Configuration and running DHCP server:
|
---|
792 | * 1. find server first createDHCPServer
|
---|
793 | * 2. if return status is E_INVALARG => server already exists just find and start.
|
---|
794 | * 3. if return status neither E_INVALRG nor S_OK => return E_FAIL
|
---|
795 | * 4. if return status S_OK proceed to DHCP server configuration
|
---|
796 | * 5. call setConfiguration() and pass all required parameters
|
---|
797 | * 6. start dhcp server.
|
---|
798 | */
|
---|
799 | HRESULT hrc = m->pVirtualBox->FindDHCPServerByNetworkName(Bstr(m->s.strNetworkName).raw(),
|
---|
800 | m->dhcpServer.asOutParam());
|
---|
801 | switch (hrc)
|
---|
802 | {
|
---|
803 | case E_INVALIDARG:
|
---|
804 | /* server haven't beeen found let create it then */
|
---|
805 | hrc = m->pVirtualBox->CreateDHCPServer(Bstr(m->s.strNetworkName).raw(),
|
---|
806 | m->dhcpServer.asOutParam());
|
---|
807 | if (FAILED(hrc))
|
---|
808 | return E_FAIL;
|
---|
809 | /* breakthrough */
|
---|
810 |
|
---|
811 | {
|
---|
812 | LogFunc(("gateway: %s, dhcpserver:%s, dhcplowerip:%s, dhcpupperip:%s\n",
|
---|
813 | m->IPv4Gateway.c_str(),
|
---|
814 | m->IPv4DhcpServer.c_str(),
|
---|
815 | m->IPv4DhcpServerLowerIp.c_str(),
|
---|
816 | m->IPv4DhcpServerUpperIp.c_str()));
|
---|
817 |
|
---|
818 | hrc = m->dhcpServer->COMSETTER(Enabled)(true);
|
---|
819 |
|
---|
820 | hrc = m->dhcpServer->SetConfiguration(Bstr(m->IPv4DhcpServer).raw(),
|
---|
821 | Bstr(m->IPv4NetworkMask).raw(),
|
---|
822 | Bstr(m->IPv4DhcpServerLowerIp).raw(),
|
---|
823 | Bstr(m->IPv4DhcpServerUpperIp).raw());
|
---|
824 | }
|
---|
825 | case S_OK:
|
---|
826 | break;
|
---|
827 |
|
---|
828 | default:
|
---|
829 | return E_FAIL;
|
---|
830 | }
|
---|
831 |
|
---|
832 | #ifdef VBOX_WITH_DHCPD
|
---|
833 | i_updateDnsOptions();
|
---|
834 | #endif /* VBOX_WITH_DHCPD */
|
---|
835 | /* XXX: AddGlobalOption(DhcpOpt_Router,) - enables attachement of DhcpServer to Main (no longer true with VBoxNetDhcpd). */
|
---|
836 | ComPtr<IDHCPGlobalConfig> pDHCPConfig;
|
---|
837 | hrc = m->dhcpServer->COMGETTER(GlobalConfig)(pDHCPConfig.asOutParam());
|
---|
838 | if (FAILED(hrc))
|
---|
839 | {
|
---|
840 | LogRel(("NATNetwork: Failed to get global DHCP config when updating IPv4 gateway option with %Rhrc\n", hrc));
|
---|
841 | m->dhcpServer.setNull();
|
---|
842 | return E_FAIL;
|
---|
843 | }
|
---|
844 | pDHCPConfig->SetOption(DHCPOption_Routers, DHCPOptionEncoding_Normal, Bstr(m->IPv4Gateway).raw());
|
---|
845 |
|
---|
846 | hrc = m->dhcpServer->Start(Bstr::Empty.raw(), Bstr(TRUNKTYPE_WHATEVER).raw());
|
---|
847 | if (FAILED(hrc))
|
---|
848 | {
|
---|
849 | m->dhcpServer.setNull();
|
---|
850 | return E_FAIL;
|
---|
851 | }
|
---|
852 | }
|
---|
853 |
|
---|
854 | if (RT_SUCCESS(m->NATRunner.start(false /* KillProcOnStop */)))
|
---|
855 | {
|
---|
856 | m->pVirtualBox->i_onNATNetworkStartStop(Bstr(m->s.strNetworkName).raw(), TRUE);
|
---|
857 | return S_OK;
|
---|
858 | }
|
---|
859 | /** @todo missing setError()! */
|
---|
860 | return E_FAIL;
|
---|
861 | #else
|
---|
862 | ReturnComNotImplemented();
|
---|
863 | #endif
|
---|
864 | }
|
---|
865 |
|
---|
866 | HRESULT NATNetwork::stop()
|
---|
867 | {
|
---|
868 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
869 | m->pVirtualBox->i_onNATNetworkStartStop(Bstr(m->s.strNetworkName).raw(), FALSE);
|
---|
870 |
|
---|
871 | if (!m->dhcpServer.isNull())
|
---|
872 | m->dhcpServer->Stop();
|
---|
873 |
|
---|
874 | if (RT_SUCCESS(m->NATRunner.stop()))
|
---|
875 | return S_OK;
|
---|
876 |
|
---|
877 | /** @todo missing setError()! */
|
---|
878 | return E_FAIL;
|
---|
879 | #else
|
---|
880 | ReturnComNotImplemented();
|
---|
881 | #endif
|
---|
882 | }
|
---|
883 |
|
---|
884 |
|
---|
885 | void NATNetwork::i_getPortForwardRulesFromMap(std::vector<com::Utf8Str> &aPortForwardRules, settings::NATRulesMap &aRules)
|
---|
886 | {
|
---|
887 | aPortForwardRules.resize(aRules.size());
|
---|
888 | size_t i = 0;
|
---|
889 | for (settings::NATRulesMap::const_iterator it = aRules.begin();
|
---|
890 | it != aRules.end(); ++it, ++i)
|
---|
891 | {
|
---|
892 | settings::NATRule r = it->second;
|
---|
893 | aPortForwardRules[i] = Utf8StrFmt("%s:%s:[%s]:%d:[%s]:%d",
|
---|
894 | r.strName.c_str(),
|
---|
895 | (r.proto == NATProtocol_TCP ? "tcp" : "udp"),
|
---|
896 | r.strHostIP.c_str(),
|
---|
897 | r.u16HostPort,
|
---|
898 | r.strGuestIP.c_str(),
|
---|
899 | r.u16GuestPort);
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 |
|
---|
904 | int NATNetwork::i_findFirstAvailableOffset(ADDRESSLOOKUPTYPE addrType, uint32_t *poff)
|
---|
905 | {
|
---|
906 | RTNETADDRIPV4 network, netmask;
|
---|
907 |
|
---|
908 | int rc = RTCidrStrToIPv4(m->s.strIPv4NetworkCidr.c_str(),
|
---|
909 | &network,
|
---|
910 | &netmask);
|
---|
911 | AssertRCReturn(rc, rc);
|
---|
912 |
|
---|
913 | uint32_t off;
|
---|
914 | for (off = 1; off < ~netmask.u; ++off)
|
---|
915 | {
|
---|
916 | bool skip = false;
|
---|
917 | for (settings::NATLoopbackOffsetList::iterator it = m->s.llHostLoopbackOffsetList.begin();
|
---|
918 | it != m->s.llHostLoopbackOffsetList.end();
|
---|
919 | ++it)
|
---|
920 | {
|
---|
921 | if ((*it).u32Offset == off)
|
---|
922 | {
|
---|
923 | skip = true;
|
---|
924 | break;
|
---|
925 | }
|
---|
926 |
|
---|
927 | }
|
---|
928 |
|
---|
929 | if (skip)
|
---|
930 | continue;
|
---|
931 |
|
---|
932 | if (off == m->offGateway)
|
---|
933 | {
|
---|
934 | if (addrType == ADDR_GATEWAY)
|
---|
935 | break;
|
---|
936 | else
|
---|
937 | continue;
|
---|
938 | }
|
---|
939 |
|
---|
940 | if (off == m->offDhcp)
|
---|
941 | {
|
---|
942 | if (addrType == ADDR_DHCP)
|
---|
943 | break;
|
---|
944 | else
|
---|
945 | continue;
|
---|
946 | }
|
---|
947 |
|
---|
948 | if (!skip)
|
---|
949 | break;
|
---|
950 | }
|
---|
951 |
|
---|
952 | if (poff)
|
---|
953 | *poff = off;
|
---|
954 |
|
---|
955 | return VINF_SUCCESS;
|
---|
956 | }
|
---|
957 |
|
---|
958 | int NATNetwork::i_recalculateIpv4AddressAssignments()
|
---|
959 | {
|
---|
960 | RTNETADDRIPV4 network, netmask;
|
---|
961 | int rc = RTCidrStrToIPv4(m->s.strIPv4NetworkCidr.c_str(),
|
---|
962 | &network,
|
---|
963 | &netmask);
|
---|
964 | AssertRCReturn(rc, rc);
|
---|
965 |
|
---|
966 | i_findFirstAvailableOffset(ADDR_GATEWAY, &m->offGateway);
|
---|
967 | if (m->s.fNeedDhcpServer)
|
---|
968 | i_findFirstAvailableOffset(ADDR_DHCP, &m->offDhcp);
|
---|
969 |
|
---|
970 | /* I don't remember the reason CIDR calculated on the host. */
|
---|
971 | RTNETADDRIPV4 gateway = network;
|
---|
972 | gateway.u += m->offGateway;
|
---|
973 | gateway.u = RT_H2N_U32(gateway.u);
|
---|
974 | char szTmpIp[16];
|
---|
975 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", gateway);
|
---|
976 | m->IPv4Gateway = szTmpIp;
|
---|
977 |
|
---|
978 | if (m->s.fNeedDhcpServer)
|
---|
979 | {
|
---|
980 | RTNETADDRIPV4 dhcpserver = network;
|
---|
981 | dhcpserver.u += m->offDhcp;
|
---|
982 |
|
---|
983 | /* XXX: adding more services should change the math here */
|
---|
984 | RTNETADDRIPV4 dhcplowerip = network;
|
---|
985 | uint32_t offDhcpLowerIp;
|
---|
986 | i_findFirstAvailableOffset(ADDR_DHCPLOWERIP, &offDhcpLowerIp);
|
---|
987 | dhcplowerip.u = RT_H2N_U32(dhcplowerip.u + offDhcpLowerIp);
|
---|
988 |
|
---|
989 | RTNETADDRIPV4 dhcpupperip;
|
---|
990 | dhcpupperip.u = RT_H2N_U32((network.u | ~netmask.u) - 1);
|
---|
991 |
|
---|
992 | dhcpserver.u = RT_H2N_U32(dhcpserver.u);
|
---|
993 | network.u = RT_H2N_U32(network.u);
|
---|
994 |
|
---|
995 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcpserver);
|
---|
996 | m->IPv4DhcpServer = szTmpIp;
|
---|
997 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcplowerip);
|
---|
998 | m->IPv4DhcpServerLowerIp = szTmpIp;
|
---|
999 | RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcpupperip);
|
---|
1000 | m->IPv4DhcpServerUpperIp = szTmpIp;
|
---|
1001 |
|
---|
1002 | LogFunc(("network:%RTnaipv4, dhcpserver:%RTnaipv4, dhcplowerip:%RTnaipv4, dhcpupperip:%RTnaipv4\n",
|
---|
1003 | network, dhcpserver, dhcplowerip, dhcpupperip));
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | /* we need IPv4NetworkMask for NAT's gw service start */
|
---|
1007 | netmask.u = RT_H2N_U32(netmask.u);
|
---|
1008 | RTStrPrintf(szTmpIp, 16, "%RTnaipv4", netmask);
|
---|
1009 | m->IPv4NetworkMask = szTmpIp;
|
---|
1010 |
|
---|
1011 | LogFlowFunc(("getaway:%RTnaipv4, netmask:%RTnaipv4\n", gateway, netmask));
|
---|
1012 | return VINF_SUCCESS;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | int NATNetwork::i_recalculateIPv6Prefix()
|
---|
1017 | {
|
---|
1018 | int rc;
|
---|
1019 |
|
---|
1020 | RTNETADDRIPV4 net, mask;
|
---|
1021 | rc = RTCidrStrToIPv4(Utf8Str(m->s.strIPv4NetworkCidr).c_str(), &net, &mask);
|
---|
1022 | if (RT_FAILURE(rc))
|
---|
1023 | return rc;
|
---|
1024 |
|
---|
1025 | net.u = RT_H2N_U32(net.u); /* XXX: fix RTCidrStrToIPv4! */
|
---|
1026 |
|
---|
1027 | /*
|
---|
1028 | * [fd17:625c:f037:XXXX::/64] - RFC 4193 (ULA) Locally Assigned
|
---|
1029 | * Global ID where XXXX, 16 bit Subnet ID, are two bytes from the
|
---|
1030 | * middle of the IPv4 address, e.g. :dead: for 10.222.173.1
|
---|
1031 | */
|
---|
1032 | RTNETADDRIPV6 prefix;
|
---|
1033 | RT_ZERO(prefix);
|
---|
1034 |
|
---|
1035 | prefix.au8[0] = 0xFD;
|
---|
1036 | prefix.au8[1] = 0x17;
|
---|
1037 |
|
---|
1038 | prefix.au8[2] = 0x62;
|
---|
1039 | prefix.au8[3] = 0x5C;
|
---|
1040 |
|
---|
1041 | prefix.au8[4] = 0xF0;
|
---|
1042 | prefix.au8[5] = 0x37;
|
---|
1043 |
|
---|
1044 | prefix.au8[6] = net.au8[1];
|
---|
1045 | prefix.au8[7] = net.au8[2];
|
---|
1046 |
|
---|
1047 | char szBuf[32];
|
---|
1048 | RTStrPrintf(szBuf, sizeof(szBuf), "%RTnaipv6/64", &prefix);
|
---|
1049 |
|
---|
1050 | m->s.strIPv6Prefix = szBuf;
|
---|
1051 | return VINF_SUCCESS;
|
---|
1052 | }
|
---|