1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
|
/* $Id: VBoxNetDhcpd.cpp $ */
/** @file
* VBoxNetDhcpd - DHCP server for host-only and NAT networks.
*/
/*
* Copyright (C) 2009-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <iprt/cdefs.h>
/*
* Need to get host/network order conversion stuff from Windows headers,
* so we do not define them in LWIP and then try to re-define them in
* Windows headers.
*/
#ifdef RT_OS_WINDOWS
# include <iprt/win/winsock2.h>
#endif
#include <iprt/param.h>
#include <iprt/errcore.h>
#include <iprt/initterm.h>
#include <iprt/message.h>
#include <iprt/net.h>
#include <iprt/path.h>
#include <iprt/stream.h>
#include <VBox/sup.h>
#include <VBox/vmm/vmm.h>
#include <VBox/vmm/pdmnetinline.h>
#include <VBox/intnet.h>
#include <VBox/intnetinline.h>
#include "VBoxLwipCore.h"
#include "Config.h"
#include "DHCPD.h"
#include "DhcpMessage.h"
extern "C"
{
#include "lwip/sys.h"
#include "lwip/pbuf.h"
#include "lwip/netif.h"
#include "lwip/tcpip.h"
#include "lwip/udp.h"
#include "netif/etharp.h"
}
#include <string>
#include <vector>
#include <memory>
#ifdef RT_OS_WINDOWS
# include <iprt/win/windows.h>
#endif
struct delete_pbuf
{
delete_pbuf() {}
void operator()(struct pbuf *p) const { pbuf_free(p); }
};
typedef std::unique_ptr<pbuf, delete_pbuf> unique_ptr_pbuf;
#define CALL_VMMR0(op, req) \
(SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, (op), 0, &(req).Hdr))
class VBoxNetDhcpd
{
DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(VBoxNetDhcpd);
private:
PRTLOGGER m_pStderrReleaseLogger;
/* intnet plumbing */
PSUPDRVSESSION m_pSession;
INTNETIFHANDLE m_hIf;
PINTNETBUF m_pIfBuf;
/* lwip stack connected to the intnet */
struct netif m_LwipNetif;
Config *m_Config;
/* listening pcb */
struct udp_pcb *m_Dhcp4Pcb;
DHCPD m_server;
public:
VBoxNetDhcpd();
~VBoxNetDhcpd();
int main(int argc, char **argv);
private:
int logInitStderr();
/*
* Boilerplate code.
*/
int r3Init();
void r3Fini();
int vmmInit();
int ifInit(const std::string &strNetwork,
const std::string &strTrunk = std::string(),
INTNETTRUNKTYPE enmTrunkType = kIntNetTrunkType_WhateverNone);
int ifOpen(const std::string &strNetwork,
const std::string &strTrunk,
INTNETTRUNKTYPE enmTrunkType);
int ifGetBuf();
int ifActivate();
int ifWait(uint32_t cMillies = RT_INDEFINITE_WAIT);
int ifProcessInput();
int ifFlush();
int ifClose();
void ifPump();
int ifInput(void *pvSegFrame, uint32_t cbSegFrame);
int ifOutput(PCINTNETSEG paSegs, size_t cSegs, size_t cbFrame);
/*
* lwIP callbacks
*/
static DECLCALLBACK(void) lwipInitCB(void *pvArg);
void lwipInit();
static err_t netifInitCB(netif *pNetif);
err_t netifInit(netif *pNetif);
static err_t netifLinkOutputCB(netif *pNetif, pbuf *pPBuf);
err_t netifLinkOutput(pbuf *pPBuf);
static void dhcp4RecvCB(void *arg, struct udp_pcb *pcb, struct pbuf *p,
ip_addr_t *addr, u16_t port);
void dhcp4Recv(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
};
VBoxNetDhcpd::VBoxNetDhcpd()
: m_pStderrReleaseLogger(NULL),
m_pSession(NIL_RTR0PTR),
m_hIf(INTNET_HANDLE_INVALID),
m_pIfBuf(NULL),
m_LwipNetif(),
m_Config(NULL),
m_Dhcp4Pcb(NULL)
{
int rc;
logInitStderr();
rc = r3Init();
if (RT_FAILURE(rc))
return;
vmmInit();
}
VBoxNetDhcpd::~VBoxNetDhcpd()
{
ifClose();
r3Fini();
}
/*
* We don't know the name of the release log file until we parse our
* configuration because we use network name as basename. To get
* early logging to work, start with stderr-only release logger.
*
* We disable "sup" for this logger to avoid spam from SUPR3Init().
*/
int VBoxNetDhcpd::logInitStderr()
{
static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
PRTLOGGER pLogger;
int rc;
uint32_t fFlags = 0;
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
fFlags |= RTLOGFLAGS_USECRLF;
#endif
rc = RTLogCreate(&pLogger, fFlags,
"all -sup all.restrict -default.restrict",
NULL, /* environment base */
RT_ELEMENTS(s_apszGroups), s_apszGroups,
RTLOGDEST_STDERR, NULL);
if (RT_FAILURE(rc))
{
RTPrintf("Failed to init stderr logger: %Rrs\n", rc);
return rc;
}
m_pStderrReleaseLogger = pLogger;
RTLogRelSetDefaultInstance(m_pStderrReleaseLogger);
return VINF_SUCCESS;
}
int VBoxNetDhcpd::r3Init()
{
AssertReturn(m_pSession == NIL_RTR0PTR, VERR_GENERAL_FAILURE);
int rc = SUPR3Init(&m_pSession);
return rc;
}
void VBoxNetDhcpd::r3Fini()
{
if (m_pSession == NIL_RTR0PTR)
return;
SUPR3Term();
m_pSession = NIL_RTR0PTR;
}
int VBoxNetDhcpd::vmmInit()
{
int rc;
try {
std::vector<char> vExecDir(RTPATH_MAX);
rc = RTPathExecDir(&vExecDir.front(), vExecDir.size());
if (RT_FAILURE(rc))
return rc;
std::string strPath(&vExecDir.front());
strPath.append("/VMMR0.r0");
rc = SUPR3LoadVMM(strPath.c_str());
if (RT_FAILURE(rc))
return rc;
rc = VINF_SUCCESS;
}
catch (...)
{
rc = VERR_GENERAL_FAILURE;
}
return rc;
}
int VBoxNetDhcpd::ifInit(const std::string &strNetwork,
const std::string &strTrunk,
INTNETTRUNKTYPE enmTrunkType)
{
int rc;
rc = ifOpen(strNetwork, strTrunk, enmTrunkType);
if (RT_FAILURE(rc))
return rc;
rc = ifGetBuf();
if (RT_FAILURE(rc))
return rc;
rc = ifActivate();
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
int VBoxNetDhcpd::ifOpen(const std::string &strNetwork,
const std::string &strTrunk,
INTNETTRUNKTYPE enmTrunkType)
{
AssertReturn(m_pSession != NIL_RTR0PTR, VERR_GENERAL_FAILURE);
AssertReturn(m_hIf == INTNET_HANDLE_INVALID, VERR_GENERAL_FAILURE);
INTNETOPENREQ OpenReq;
int rc;
OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
OpenReq.Hdr.cbReq = sizeof(OpenReq);
OpenReq.pSession = m_pSession;
strncpy(OpenReq.szNetwork, strNetwork.c_str(), sizeof(OpenReq.szNetwork));
OpenReq.szNetwork[sizeof(OpenReq.szNetwork) - 1] = '\0';
strncpy(OpenReq.szTrunk, strTrunk.c_str(), sizeof(OpenReq.szTrunk));
OpenReq.szTrunk[sizeof(OpenReq.szTrunk) - 1] = '\0';
if (enmTrunkType != kIntNetTrunkType_Invalid)
OpenReq.enmTrunkType = enmTrunkType;
else
OpenReq.enmTrunkType = kIntNetTrunkType_WhateverNone;
OpenReq.fFlags = 0;
OpenReq.cbSend = 128 * _1K;
OpenReq.cbRecv = 256 * _1K;
OpenReq.hIf = INTNET_HANDLE_INVALID;
rc = CALL_VMMR0(VMMR0_DO_INTNET_OPEN, OpenReq);
if (RT_FAILURE(rc))
return rc;
m_hIf = OpenReq.hIf;
AssertReturn(m_hIf != INTNET_HANDLE_INVALID, VERR_GENERAL_FAILURE);
return VINF_SUCCESS;
}
int VBoxNetDhcpd::ifGetBuf()
{
AssertReturn(m_pSession != NIL_RTR0PTR, VERR_GENERAL_FAILURE);
AssertReturn(m_hIf != INTNET_HANDLE_INVALID, VERR_GENERAL_FAILURE);
AssertReturn(m_pIfBuf == NULL, VERR_GENERAL_FAILURE);
INTNETIFGETBUFFERPTRSREQ GetBufferPtrsReq;
int rc;
GetBufferPtrsReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
GetBufferPtrsReq.Hdr.cbReq = sizeof(GetBufferPtrsReq);
GetBufferPtrsReq.pSession = m_pSession;
GetBufferPtrsReq.hIf = m_hIf;
GetBufferPtrsReq.pRing0Buf = NIL_RTR0PTR;
GetBufferPtrsReq.pRing3Buf = NULL;
rc = CALL_VMMR0(VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS, GetBufferPtrsReq);
if (RT_FAILURE(rc))
return rc;
m_pIfBuf = GetBufferPtrsReq.pRing3Buf;
AssertReturn(m_pIfBuf != NULL, VERR_GENERAL_FAILURE);
return VINF_SUCCESS;
}
int VBoxNetDhcpd::ifActivate()
{
AssertReturn(m_pSession != NIL_RTR0PTR, VERR_GENERAL_FAILURE);
AssertReturn(m_hIf != INTNET_HANDLE_INVALID, VERR_GENERAL_FAILURE);
AssertReturn(m_pIfBuf != NULL, VERR_GENERAL_FAILURE);
INTNETIFSETACTIVEREQ ActiveReq;
int rc;
ActiveReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
ActiveReq.Hdr.cbReq = sizeof(ActiveReq);
ActiveReq.pSession = m_pSession;
ActiveReq.hIf = m_hIf;
ActiveReq.fActive = 1;
rc = CALL_VMMR0(VMMR0_DO_INTNET_IF_SET_ACTIVE, ActiveReq);
return rc;
}
void VBoxNetDhcpd::ifPump()
{
for (;;)
{
int rc = ifWait();
if (RT_UNLIKELY(rc == VERR_INTERRUPTED))
continue;
#if 0 /* we wait indefinitely */
if (rc == VERR_TIMEOUT)
...;
#endif
if (RT_FAILURE(rc))
return;
ifProcessInput();
}
}
int VBoxNetDhcpd::ifWait(uint32_t cMillies)
{
AssertReturn(m_pSession != NIL_RTR0PTR, VERR_GENERAL_FAILURE);
AssertReturn(m_hIf != INTNET_HANDLE_INVALID, VERR_GENERAL_FAILURE);
INTNETIFWAITREQ WaitReq;
int rc;
WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
WaitReq.Hdr.cbReq = sizeof(WaitReq);
WaitReq.pSession = m_pSession;
WaitReq.hIf = m_hIf;
WaitReq.cMillies = cMillies;
rc = CALL_VMMR0(VMMR0_DO_INTNET_IF_WAIT, WaitReq);
return rc;
}
int VBoxNetDhcpd::ifProcessInput()
{
AssertReturn(m_pSession != NIL_RTR0PTR, VERR_GENERAL_FAILURE);
AssertReturn(m_hIf != INTNET_HANDLE_INVALID, VERR_GENERAL_FAILURE);
AssertReturn(m_pIfBuf != NULL, VERR_GENERAL_FAILURE);
for (PCINTNETHDR pHdr;
(pHdr = IntNetRingGetNextFrameToRead(&m_pIfBuf->Recv)) != NULL;
IntNetRingSkipFrame(&m_pIfBuf->Recv))
{
const uint8_t u8Type = pHdr->u8Type;
void *pvSegFrame;
uint32_t cbSegFrame;
if (u8Type == INTNETHDR_TYPE_FRAME)
{
pvSegFrame = IntNetHdrGetFramePtr(pHdr, m_pIfBuf);
cbSegFrame = pHdr->cbFrame;
ifInput(pvSegFrame, cbSegFrame);
}
else if (u8Type == INTNETHDR_TYPE_GSO)
{
PCPDMNETWORKGSO pGso;
size_t cbGso = pHdr->cbFrame;
size_t cbFrame = cbGso - sizeof(PDMNETWORKGSO);
pGso = IntNetHdrGetGsoContext(pHdr, m_pIfBuf);
if (!PDMNetGsoIsValid(pGso, cbGso, cbFrame))
continue;
const uint32_t cSegs = PDMNetGsoCalcSegmentCount(pGso, cbFrame);
for (uint32_t i = 0; i < cSegs; ++i)
{
uint8_t abHdrScratch[256];
pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)(pGso + 1), cbFrame,
abHdrScratch,
i, cSegs,
&cbSegFrame);
ifInput(pvSegFrame, (uint32_t)cbFrame);
}
}
}
return VINF_SUCCESS;
}
/*
* Got a frame from the internal network, feed it to the lwIP stack.
*/
int VBoxNetDhcpd::ifInput(void *pvFrame, uint32_t cbFrame)
{
if (pvFrame == NULL)
return VERR_INVALID_PARAMETER;
if ( cbFrame <= sizeof(RTNETETHERHDR)
|| cbFrame > UINT16_MAX - ETH_PAD_SIZE)
return VERR_INVALID_PARAMETER;
struct pbuf *p = pbuf_alloc(PBUF_RAW, (u16_t)cbFrame + ETH_PAD_SIZE, PBUF_POOL);
if (RT_UNLIKELY(p == NULL))
return VERR_NO_MEMORY;
/*
* The code below is inlined version of:
*
* pbuf_header(p, -ETH_PAD_SIZE); // hide padding
* pbuf_take(p, pvFrame, cbFrame);
* pbuf_header(p, ETH_PAD_SIZE); // reveal padding
*/
struct pbuf *q = p;
uint8_t *pu8Chunk = (uint8_t *)pvFrame;
do {
uint8_t *payload = (uint8_t *)q->payload;
size_t len = q->len;
#if ETH_PAD_SIZE
if (RT_LIKELY(q == p)) /* single pbuf is large enough */
{
payload += ETH_PAD_SIZE;
len -= ETH_PAD_SIZE;
}
#endif
memcpy(payload, pu8Chunk, len);
pu8Chunk += len;
q = q->next;
} while (RT_UNLIKELY(q != NULL));
m_LwipNetif.input(p, &m_LwipNetif);
return VINF_SUCCESS;
}
/*
* Got a frame from the lwIP stack, feed it to the internal network.
*/
err_t VBoxNetDhcpd::netifLinkOutput(pbuf *pPBuf)
{
PINTNETHDR pHdr;
void *pvFrame;
u16_t cbFrame;
int rc;
if (pPBuf->tot_len < sizeof(struct eth_hdr)) /* includes ETH_PAD_SIZE */
return ERR_ARG;
cbFrame = pPBuf->tot_len - ETH_PAD_SIZE;
rc = IntNetRingAllocateFrame(&m_pIfBuf->Send, cbFrame, &pHdr, &pvFrame);
if (RT_FAILURE(rc))
return ERR_MEM;
pbuf_copy_partial(pPBuf, pvFrame, cbFrame, ETH_PAD_SIZE);
IntNetRingCommitFrameEx(&m_pIfBuf->Send, pHdr, cbFrame);
ifFlush();
return ERR_OK;
}
int VBoxNetDhcpd::ifFlush()
{
INTNETIFSENDREQ SendReq;
int rc;
SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
SendReq.Hdr.cbReq = sizeof(SendReq);
SendReq.pSession = m_pSession;
SendReq.hIf = m_hIf;
rc = CALL_VMMR0(VMMR0_DO_INTNET_IF_SEND, SendReq);
return rc;
}
int VBoxNetDhcpd::ifClose()
{
if (m_hIf == INTNET_HANDLE_INVALID)
return VINF_SUCCESS;
INTNETIFCLOSEREQ CloseReq;
CloseReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
CloseReq.Hdr.cbReq = sizeof(CloseReq);
CloseReq.pSession = m_pSession;
CloseReq.hIf = m_hIf;
m_hIf = INTNET_HANDLE_INVALID;
m_pIfBuf = NULL;
CALL_VMMR0(VMMR0_DO_INTNET_IF_CLOSE, CloseReq);
return VINF_SUCCESS;
}
/* static */ DECLCALLBACK(void) VBoxNetDhcpd::lwipInitCB(void *pvArg)
{
AssertPtrReturnVoid(pvArg);
VBoxNetDhcpd *self = static_cast<VBoxNetDhcpd *>(pvArg);
self->lwipInit();
}
/* static */ err_t VBoxNetDhcpd::netifInitCB(netif *pNetif)
{
AssertPtrReturn(pNetif, ERR_ARG);
VBoxNetDhcpd *self = static_cast<VBoxNetDhcpd *>(pNetif->state);
return self->netifInit(pNetif);
}
/* static */ err_t VBoxNetDhcpd::netifLinkOutputCB(netif *pNetif, pbuf *pPBuf)
{
AssertPtrReturn(pNetif, ERR_ARG);
AssertPtrReturn(pPBuf, ERR_ARG);
VBoxNetDhcpd *self = static_cast<VBoxNetDhcpd *>(pNetif->state);
AssertPtrReturn(self, ERR_IF);
return self->netifLinkOutput(pPBuf);
}
/* static */ void VBoxNetDhcpd::dhcp4RecvCB(void *arg, struct udp_pcb *pcb,
struct pbuf *p,
ip_addr_t *addr, u16_t port)
{
AssertPtrReturnVoid(arg);
VBoxNetDhcpd *self = static_cast<VBoxNetDhcpd *>(arg);
self->dhcp4Recv(pcb, p, addr, port);
pbuf_free(p);
}
int VBoxNetDhcpd::main(int argc, char **argv)
{
int rc;
ClientId::registerFormat();
/* XXX: We no longer need hardcoded and compat methods. We should remove them soon. */
if (argc < 2)
m_Config = Config::hardcoded();
else if ( strcmp(argv[1], "--config") == 0
|| strcmp(argv[1], "--comment") == 0)
m_Config = Config::create(argc, argv);
else
m_Config = Config::compat(argc, argv);
if (m_Config == NULL)
return VERR_GENERAL_FAILURE;
rc = m_server.init(m_Config);
/* connect to the intnet */
rc = ifInit(m_Config->getNetwork(),
m_Config->getTrunk(),
m_Config->getTrunkType());
if (RT_FAILURE(rc))
return rc;
/* setup lwip */
rc = vboxLwipCoreInitialize(lwipInitCB, this);
if (RT_FAILURE(rc))
return rc;
ifPump();
return VINF_SUCCESS;
}
void VBoxNetDhcpd::lwipInit()
{
err_t error;
ip_addr_t addr, mask;
ip4_addr_set_u32(&addr, m_Config->getIPv4Address().u);
ip4_addr_set_u32(&mask, m_Config->getIPv4Netmask().u);
netif *pNetif = netif_add(&m_LwipNetif,
&addr, &mask,
IP_ADDR_ANY, /* gateway */
this, /* state */
VBoxNetDhcpd::netifInitCB, /* netif_init_fn */
tcpip_input); /* netif_input_fn */
if (pNetif == NULL)
return;
netif_set_up(pNetif);
netif_set_link_up(pNetif);
m_Dhcp4Pcb = udp_new();
if (RT_UNLIKELY(m_Dhcp4Pcb == NULL))
return; /* XXX? */
ip_set_option(m_Dhcp4Pcb, SOF_BROADCAST);
udp_recv(m_Dhcp4Pcb, dhcp4RecvCB, this);
error = udp_bind(m_Dhcp4Pcb, IP_ADDR_ANY, RTNETIPV4_PORT_BOOTPS);
if (error != ERR_OK)
{
udp_remove(m_Dhcp4Pcb);
m_Dhcp4Pcb = NULL;
return; /* XXX? */
}
}
err_t VBoxNetDhcpd::netifInit(netif *pNetif)
{
pNetif->hwaddr_len = sizeof(RTMAC);
memcpy(pNetif->hwaddr, &m_Config->getMacAddress(), sizeof(RTMAC));
pNetif->mtu = 1500;
pNetif->flags = NETIF_FLAG_BROADCAST
| NETIF_FLAG_ETHARP
| NETIF_FLAG_ETHERNET;
pNetif->linkoutput = netifLinkOutputCB;
pNetif->output = etharp_output;
netif_set_default(pNetif);
return ERR_OK;
}
void VBoxNetDhcpd::dhcp4Recv(struct udp_pcb *pcb, struct pbuf *p,
ip_addr_t *addr, u16_t port)
{
err_t error;
int rc;
RT_NOREF(pcb, addr, port);
if (RT_UNLIKELY(p->next != NULL))
return; /* XXX: we want it in one chunk */
bool broadcasted = ip_addr_cmp(ip_current_dest_addr(), &ip_addr_broadcast)
|| ip_addr_cmp(ip_current_dest_addr(), &ip_addr_any);
DhcpClientMessage *msgIn = DhcpClientMessage::parse(broadcasted, p->payload, p->len);
if (msgIn == NULL)
return;
std::unique_ptr<DhcpClientMessage> autoFreeMsgIn(msgIn);
DhcpServerMessage *msgOut = m_server.process(*msgIn);
if (msgOut == NULL)
return;
std::unique_ptr<DhcpServerMessage> autoFreeMsgOut(msgOut);
ip_addr_t dst = { msgOut->dst().u };
if (ip_addr_cmp(&dst, &ip_addr_any))
ip_addr_copy(dst, ip_addr_broadcast);
octets_t data;
rc = msgOut->encode(data);
if (RT_FAILURE(rc))
return;
unique_ptr_pbuf q ( pbuf_alloc(PBUF_RAW, (u16_t)data.size(), PBUF_RAM) );
if (!q)
return;
error = pbuf_take(q.get(), &data.front(), (u16_t)data.size());
if (error != ERR_OK)
return;
error = udp_sendto(pcb, q.get(), &dst, RTNETIPV4_PORT_BOOTPC);
if (error != ERR_OK)
return;
}
/*
* Entry point.
*/
extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv)
{
VBoxNetDhcpd Dhcpd;
int rc = Dhcpd.main(argc, argv);
return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
}
#ifndef VBOX_WITH_HARDENING
int main(int argc, char **argv)
{
int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
return TrustedMain(argc, argv);
}
# ifdef RT_OS_WINDOWS
/** (We don't want a console usually.) */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
RT_NOREF(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
return main(__argc, __argv);
}
# endif /* RT_OS_WINDOWS */
#endif /* !VBOX_WITH_HARDENING */
|