VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/Dhcpd/VBoxNetDhcpd.cpp@ 71353

Last change on this file since 71353 was 71353, checked in by vboxsync, 7 years ago

NetworkServices/Dhcpd: stub for reading the config from a file.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette