1 | /* $Id: NetworkManagerDhcp.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NetworkManagerDhcp - Network Manager part handling Dhcp.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2015 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <iprt/asm.h>
|
---|
23 | #include <iprt/cdefs.h>
|
---|
24 | #include <iprt/getopt.h>
|
---|
25 | #include <iprt/net.h>
|
---|
26 | #include <iprt/param.h>
|
---|
27 | #include <iprt/path.h>
|
---|
28 | #include <iprt/stream.h>
|
---|
29 | #include <iprt/time.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 |
|
---|
32 | #include "../NetLib/shared_ptr.h"
|
---|
33 |
|
---|
34 | #include <vector>
|
---|
35 | #include <list>
|
---|
36 | #include <string>
|
---|
37 | #include <map>
|
---|
38 |
|
---|
39 | #include <VBox/sup.h>
|
---|
40 | #include <VBox/intnet.h>
|
---|
41 |
|
---|
42 | #define BASE_SERVICES_ONLY
|
---|
43 | #include "../NetLib/VBoxNetBaseService.h"
|
---|
44 | #include "Config.h"
|
---|
45 | #include "ClientDataInt.h"
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * The client is requesting an offer.
|
---|
49 | *
|
---|
50 | * @returns true.
|
---|
51 | *
|
---|
52 | * @param pDhcpMsg The message.
|
---|
53 | * @param cb The message size.
|
---|
54 | */
|
---|
55 | bool NetworkManager::handleDhcpReqDiscover(PCRTNETBOOTP pDhcpMsg, size_t cb)
|
---|
56 | {
|
---|
57 | RawOption opt;
|
---|
58 | RT_ZERO(opt);
|
---|
59 |
|
---|
60 | /* 1. Find client */
|
---|
61 | ConfigurationManager *confManager = ConfigurationManager::getConfigurationManager();
|
---|
62 | Client client = confManager->getClientByDhcpPacket(pDhcpMsg, cb);
|
---|
63 |
|
---|
64 | /* 2. Find/Bind lease for client */
|
---|
65 | Lease lease = confManager->allocateLease4Client(client, pDhcpMsg, cb);
|
---|
66 | AssertReturn(lease != Lease::NullLease, VINF_SUCCESS);
|
---|
67 |
|
---|
68 | int rc = ConfigurationManager::extractRequestList(pDhcpMsg, cb, opt);
|
---|
69 |
|
---|
70 | /* 3. Send of offer */
|
---|
71 |
|
---|
72 | lease.bindingPhase(true);
|
---|
73 | lease.phaseStart(RTTimeMilliTS());
|
---|
74 | lease.setExpiration(300); /* 3 min. */
|
---|
75 | offer4Client(client, pDhcpMsg->bp_xid, opt.au8RawOpt, opt.cbRawOpt);
|
---|
76 |
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * The client is requesting an offer.
|
---|
83 | *
|
---|
84 | * @returns true.
|
---|
85 | *
|
---|
86 | * @param pDhcpMsg The message.
|
---|
87 | * @param cb The message size.
|
---|
88 | */
|
---|
89 | bool NetworkManager::handleDhcpReqRequest(PCRTNETBOOTP pDhcpMsg, size_t cb)
|
---|
90 | {
|
---|
91 | ConfigurationManager *confManager = ConfigurationManager::getConfigurationManager();
|
---|
92 |
|
---|
93 | /* 1. find client */
|
---|
94 | Client client = confManager->getClientByDhcpPacket(pDhcpMsg, cb);
|
---|
95 |
|
---|
96 | /* 2. find bound lease */
|
---|
97 | Lease l = client.lease();
|
---|
98 | if (l != Lease::NullLease)
|
---|
99 | {
|
---|
100 |
|
---|
101 | if (l.isExpired())
|
---|
102 | {
|
---|
103 | /* send client to INIT state */
|
---|
104 | Client c(client);
|
---|
105 | nak(client, pDhcpMsg->bp_xid);
|
---|
106 | confManager->expireLease4Client(c);
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | /* XXX: Validate request */
|
---|
111 | RawOption opt;
|
---|
112 | RT_ZERO(opt);
|
---|
113 |
|
---|
114 | Client c(client);
|
---|
115 | int rc = confManager->commitLease4Client(c);
|
---|
116 | AssertRCReturn(rc, false);
|
---|
117 |
|
---|
118 | rc = ConfigurationManager::extractRequestList(pDhcpMsg, cb, opt);
|
---|
119 | AssertRCReturn(rc, false);
|
---|
120 |
|
---|
121 | ack(client, pDhcpMsg->bp_xid, opt.au8RawOpt, opt.cbRawOpt);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | nak(client, pDhcpMsg->bp_xid);
|
---|
127 | }
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * The client is declining an offer we've made.
|
---|
134 | *
|
---|
135 | * @returns true.
|
---|
136 | *
|
---|
137 | * @param pDhcpMsg The message.
|
---|
138 | * @param cb The message size.
|
---|
139 | */
|
---|
140 | bool NetworkManager::handleDhcpReqDecline(PCRTNETBOOTP, size_t)
|
---|
141 | {
|
---|
142 | /** @todo Probably need to match the server IP here to work correctly with
|
---|
143 | * other servers. */
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * The client is supposed to pass us option 50, requested address,
|
---|
147 | * from the offer. We also match the lease state. Apparently the
|
---|
148 | * MAC address is not supposed to be checked here.
|
---|
149 | */
|
---|
150 |
|
---|
151 | /** @todo this is not required in the initial implementation, do it later. */
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * The client is releasing its lease - good boy.
|
---|
158 | *
|
---|
159 | * @returns true.
|
---|
160 | *
|
---|
161 | * @param pDhcpMsg The message.
|
---|
162 | * @param cb The message size.
|
---|
163 | */
|
---|
164 | bool NetworkManager::handleDhcpReqRelease(PCRTNETBOOTP, size_t)
|
---|
165 | {
|
---|
166 | /** @todo Probably need to match the server IP here to work correctly with
|
---|
167 | * other servers. */
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * The client may pass us option 61, client identifier, which we should
|
---|
171 | * use to find the lease by.
|
---|
172 | *
|
---|
173 | * We're matching MAC address and lease state as well.
|
---|
174 | */
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * If no client identifier or if we couldn't find a lease by using it,
|
---|
178 | * we will try look it up by the client IP address.
|
---|
179 | */
|
---|
180 |
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * If found, release it.
|
---|
184 | */
|
---|
185 |
|
---|
186 |
|
---|
187 | /** @todo this is not required in the initial implementation, do it later. */
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|