Kea 3.2.0-git
lease_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
11#include <cc/data.h>
12#include <dhcp/hwaddr.h>
13#include <dhcpsrv/lease.h>
14#include <dhcpsrv/cfgmgr.h>
16#include <dhcpsrv/lease_mgr.h>
18#include <lease_parser.h>
19
20using namespace std;
21using namespace isc::dhcp;
22using namespace isc::data;
23using namespace isc::asiolink;
24
25namespace isc {
26namespace lease_cmds {
27
30 const ConstElementPtr& lease_info,
31 bool& force_create) {
32 if (!lease_info) {
33 isc_throw(BadValue, "lease information missing");
34 }
35
36 // These are mandatory parameters.
37 IOAddress addr = getAddress(lease_info, "ip-address");
38 if (!addr.isV4()) {
39 isc_throw(BadValue, "Non-IPv4 address specified: " << addr);
40 }
41
42 // Not a most straightforward conversion, but it works.
43 string hwaddr_txt = getString(lease_info, "hw-address");
44 HWAddr hwaddr = HWAddr::fromText(hwaddr_txt);
45 HWAddrPtr hwaddr_ptr = HWAddrPtr(new HWAddr(hwaddr));
46
47 // Now sort out the subnet-id. If specified, it must have correct value.
48 // If not specified, Kea will try to sort it out.
49 SubnetID subnet_id = 0;
50 if (lease_info->contains("subnet-id")) {
51 subnet_id = getUint32(lease_info, "subnet-id");
52 }
53
54 uint32_t pool_id = 0;
55 if (lease_info->contains("pool-id")) {
56 pool_id = getUint32(lease_info, "pool-id");
57 }
58
59 // Check if the subnet-id specified is sane.
60 ConstSubnet4Ptr subnet;
61 if (subnet_id) {
62 // If subnet-id is specified, it has to match.
63 subnet = cfg->getCfgSubnets4()->getBySubnetId(subnet_id);
64 if (!subnet) {
65 isc_throw(LeaseCmdsConflict, "Invalid subnet-id: No IPv4 subnet with subnet-id="
66 << subnet_id << " currently configured.");
67 }
68
69 // Check if the address specified really belongs to the subnet.
70 if (!subnet->inRange(addr)) {
71 isc_throw(LeaseCmdsConflict, "The address " << addr.toText() << " does not belong "
72 "to subnet " << subnet->toText() << ", subnet-id=" << subnet_id);
73 }
74
75 } else {
76 // Subnet-id was not specified. Let's try to figure it out on our own.
77 subnet = cfg->getCfgSubnets4()->selectSubnet(addr);
78 if (!subnet) {
79 isc_throw(LeaseCmdsConflict, "subnet-id not specified and failed to find a"
80 << " subnet for address " << addr);
81 }
82 subnet_id = subnet->getID();
83 }
84
85 // Client-id is optional.
86 ClientIdPtr client_id;
87 if (lease_info->contains("client-id")) {
88 string txt = getString(lease_info, "client-id");
89 client_id = ClientId::fromText(txt);
90 }
91
92 // These parameters are optional. If not specified, we'll derive them from
93 // the current subnet configuration, if possible.
94 uint32_t valid_lft = 0;
95 if (lease_info->contains("valid-lft")) {
96 valid_lft = getUint32(lease_info, "valid-lft");
97 } else {
98 valid_lft = subnet->getValid();
99 }
100
107 time_t cltt;
108 if (lease_info->contains("expire")) {
109 int64_t expire_time = getInteger(lease_info, "expire");
110 if (expire_time <= 0) {
111 isc_throw(BadValue , "expiration time must be positive for address "
112 << addr);
113
114 } else if (expire_time < valid_lft) {
115 isc_throw(BadValue, "expiration time must be greater than valid lifetime"
116 " for address " << addr);
117 }
118 cltt = static_cast<time_t>(expire_time - valid_lft);
119 } else {
120 cltt = time(NULL);
121 }
122
123 bool fqdn_fwd = false;
124 if (lease_info->contains("fqdn-fwd")) {
125 fqdn_fwd = getBoolean(lease_info, "fqdn-fwd");
126 }
127 bool fqdn_rev = false;
128 if (lease_info->contains("fqdn-rev")) {
129 fqdn_rev = getBoolean(lease_info, "fqdn-rev");
130 }
131 string hostname;
132 if (lease_info->contains("hostname")) {
133 hostname = getString(lease_info, "hostname");
134 }
135 if (hostname.empty() && (fqdn_fwd || fqdn_rev)) {
136 isc_throw(BadValue, "No hostname specified and either forward or reverse"
137 " fqdn was set to true.");
138 }
139
140 uint32_t state = 0;
141 if (lease_info->contains("state")) {
142 state = getUint8(lease_info, "state");
143 }
144
145 // Check if the state value is sane.
146 if (state > Lease::STATE_REGISTERED) {
147 isc_throw(BadValue, "Invalid state value: " << state << ", supported "
148 "values are: 0 (default), 1 (declined), 2 (expired-reclaimed),"
149 " 3 (released) and 4 (registered)");
150 }
151
152 // Handle user context.
153 ConstElementPtr ctx = lease_info->get("user-context");
154 if (ctx && (ctx->getType() != Element::map)) {
155 isc_throw(BadValue, "Invalid user context '" << ctx->str()
156 << "' is not a JSON map.");
157 }
158
159 // Handle comment.
160 ConstElementPtr comment = lease_info->get("comment");
161 if (comment) {
162 if (ctx && ctx->contains("comment")) {
163 isc_throw(BadValue, "Duplicated comment entry '" << comment->str()
164 << "' in user context '" << ctx->str() << "'");
165 }
166 ElementPtr copied;
167 if (ctx) {
168 copied = copy(ctx, 0);
169 } else {
170 copied = Element::createMap();
171 }
172 copied->set("comment", comment);
173 ctx = copied;
174 }
175
176 // Let's fabricate some data and we're ready to go.
177 Lease4Ptr l(new Lease4(addr, hwaddr_ptr, client_id, valid_lft,
178 cltt, subnet_id,
179 fqdn_fwd, fqdn_rev, hostname));
180 l->state_ = state;
181 l->setContext(ctx);
182 l->pool_id_ = pool_id;
183
184 // Sanitize extended info.
185 if (ctx) {
186 auto check = cfg->getConsistency()->getExtendedInfoSanityCheck();
189 }
190
191 // Retrieve the optional flag indicating if the lease must be created when it
192 // doesn't exist during the update.
193 force_create = false;
194 if (lease_info->contains("force-create")) {
195 force_create = getBoolean(lease_info, "force-create");
196 }
197
198 return (l);
199}
200
203 const ConstElementPtr& lease_info,
204 bool& force_create) {
205 if (!lease_info) {
206 isc_throw(BadValue, "lease information missing");
207 }
208
209 // These are mandatory parameters.
210 IOAddress addr = getAddress(lease_info, "ip-address");
211 if (addr.isV4()) {
212 isc_throw(BadValue, "Non-IPv6 address specified: " << addr);
213 }
214
215 // Not a most straightforward conversion, but it works.
216 string duid_txt = getString(lease_info, "duid");
217 DUID duid = DUID::fromText(duid_txt);
218 DuidPtr duid_ptr = DuidPtr(new DUID(duid));
219
221 uint8_t prefix_len = 128;
222 if (lease_info->contains("type")) {
223 string txt = getString(lease_info, "type");
224 if (txt == "IA_NA") {
225 type = Lease::TYPE_NA;
226 } else if (txt == "IA_PD") {
227 type = Lease::TYPE_PD;
228
229 prefix_len = getUint8(lease_info, "prefix-len");
230 } else {
231 isc_throw(BadValue, "Incorrect lease type: " << txt << ", the only "
232 "supported values are: IA_NA and IA_PD");
233 }
234 }
235
236 // Now sort out the subnet-id. If specified, it must have correct value.
237 // If not specified, Kea will try to sort it out.
238 SubnetID subnet_id = 0;
239 if (lease_info->contains("subnet-id")) {
240 subnet_id = getUint32(lease_info, "subnet-id");
241 }
242
243 uint32_t pool_id = 0;
244 if (lease_info->contains("pool-id")) {
245 pool_id = getUint32(lease_info, "pool-id");
246 }
247
248 // Check if the subnet-id specified is sane.
249 ConstSubnet6Ptr subnet;
250 if (subnet_id) {
251 // If subnet-id is specified, it has to match.
252 subnet = cfg->getCfgSubnets6()->getBySubnetId(subnet_id);
253 if (!subnet) {
254 isc_throw(LeaseCmdsConflict, "Invalid subnet-id: No IPv6 subnet with subnet-id="
255 << subnet_id << " currently configured.");
256 }
257
258 // Check if the address specified really belongs to the subnet.
259 if ((type == Lease::TYPE_NA) && !subnet->inRange(addr)) {
260 isc_throw(LeaseCmdsConflict, "The address " << addr.toText() << " does not belong "
261 "to subnet " << subnet->toText() << ", subnet-id=" << subnet_id);
262 }
263
264 } else {
265 if (type != Lease::TYPE_NA) {
266 isc_throw(BadValue, "Subnet-id is 0 or not specified. This is allowed for"
267 " address leases only, not prefix leases.");
268 }
269 // Subnet-id was not specified. Let's try to figure it out on our own.
270 subnet = cfg->getCfgSubnets6()->selectSubnet(addr);
271 if (!subnet) {
272 isc_throw(LeaseCmdsConflict, "subnet-id not specified and failed to find a "
273 "subnet for address " << addr);
274 }
275 subnet_id = subnet->getID();
276 }
277
278 uint32_t iaid = getUint32(lease_info, "iaid");
279
280 // Hw-address is optional in v6 leases.
281 HWAddrPtr hwaddr_ptr;
282 if (lease_info->contains("hw-address")) {
283 string hwaddr_txt = getString(lease_info, "hw-address");
284 HWAddr hwaddr = HWAddr::fromText(hwaddr_txt);
285 hwaddr_ptr = HWAddrPtr(new HWAddr(hwaddr));
286 }
287
288 // These parameters are optional. If not specified, we'll derive them
289 // from the current subnet configuration, if possible.
290 uint32_t valid_lft = 0;
291 if (lease_info->contains("valid-lft")) {
292 valid_lft = getUint32(lease_info, "valid-lft");
293 } else {
294 valid_lft = subnet->getValid();
295 }
296
297 // These parameters are optional. If not specified, we'll derive them
298 // from the current subnet configuration, if possible.
299 uint32_t pref_lft = 0;
300 if (lease_info->contains("preferred-lft")) {
301 pref_lft = getUint32(lease_info, "preferred-lft");
302 } else {
303 pref_lft = subnet->getValid();
304 }
305
312 time_t cltt;
313 if (lease_info->contains("expire")) {
314 int64_t expire_time = getInteger(lease_info, "expire");
315 if (expire_time <= 0) {
316 isc_throw(BadValue , "expiration time must be positive for address "
317 << addr);
318
319 } else if (expire_time < valid_lft) {
320 isc_throw(BadValue, "expiration time must be greater than valid lifetime"
321 " for address " << addr);
322 }
323
324 cltt = static_cast<time_t>(expire_time - valid_lft);
325 } else {
326 cltt = time(NULL);
327 }
328
329 bool fqdn_fwd = false;
330 if (lease_info->contains("fqdn-fwd")) {
331 fqdn_fwd = getBoolean(lease_info, "fqdn-fwd");
332 }
333 bool fqdn_rev = false;
334 if (lease_info->contains("fqdn-rev")) {
335 fqdn_rev = getBoolean(lease_info, "fqdn-rev");
336 }
337 string hostname;
338 if (lease_info->contains("hostname")) {
339 hostname = getString(lease_info, "hostname");
340 }
341 if (hostname.empty() && (fqdn_fwd || fqdn_rev)) {
342 isc_throw(BadValue, "No hostname specified and either forward or reverse"
343 " fqdn was set to true.");
344 }
345
346 uint32_t state = 0;
347 if (lease_info->contains("state")) {
348 state = getUint8(lease_info, "state");
349 }
350
351 // Check if the state value is sane.
352 if (state > Lease::STATE_REGISTERED) {
353 isc_throw(BadValue, "Invalid state value: " << state << ", supported "
354 "values are: 0 (default), 1 (declined), 2 (expired-reclaimed),"
355 " 3 (released) and 4 (registered)");
356 }
357
358 if ((state == Lease::STATE_DECLINED) && (type == Lease::TYPE_PD)) {
360 "Invalid declined state for PD prefix.");
361 }
362
363 // Handle user context.
364 ConstElementPtr ctx = lease_info->get("user-context");
365 if (ctx && (ctx->getType() != Element::map)) {
366 isc_throw(BadValue, "Invalid user context '" << ctx->str()
367 << "' is not a JSON map.");
368 }
369
370 // Handle comment.
371 ConstElementPtr comment = lease_info->get("comment");
372 if (comment) {
373 if (ctx && ctx->contains("comment")) {
374 isc_throw(BadValue, "Duplicated comment entry '" << comment->str()
375 << "' in user context '" << ctx->str() << "'");
376 }
377 ElementPtr copied;
378 if (ctx) {
379 copied = copy(ctx, 0);
380 } else {
381 copied = Element::createMap();
382 }
383 copied->set("comment", comment);
384 ctx = copied;
385 }
386
387 // Check if the prefix length is sane
388 if (prefix_len == 0 || prefix_len > 128) {
389 isc_throw(BadValue, "Invalid prefix length: "
390 << static_cast<unsigned>(prefix_len));
391 }
392
393 if (prefix_len != 128) {
394 IOAddress first_address = firstAddrInPrefix(addr, prefix_len);
395 if (first_address != addr) {
396 isc_throw(BadValue, "Prefix address: " << addr
397 << " exceeds prefix/prefix-len pair: " << first_address
398 << "/" << static_cast<uint32_t>(prefix_len));
399 }
400 }
401
402 // Let's fabricate some data and we're ready to go.
403 Lease6Ptr l(new Lease6(type, addr, duid_ptr, iaid, pref_lft, valid_lft,
404 subnet_id, fqdn_fwd, fqdn_rev, hostname,
405 hwaddr_ptr, prefix_len));
406 l->cltt_ = cltt;
407 l->state_ = state;
408 l->setContext(ctx);
409 l->pool_id_ = pool_id;
410
411 // Sanitize extended info.
412 if (ctx) {
413 auto check = cfg->getConsistency()->getExtendedInfoSanityCheck();
415 }
416
417 // Retrieve the optional flag indicating if the lease must be created when it
418 // doesn't exist during the update.
419 force_create = false;
420 if (lease_info->contains("force-create")) {
421 force_create = getBoolean(lease_info, "force-create");
422 }
423
424 return (l);
425}
426
427} // end of namespace lease_cmds
428} // end of namespace isc
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition duid.cc:50
@ map
Definition data.h:160
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
Exception thrown when a command failed due to a conflict.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
uint8_t getUint8(ConstElementPtr scope, const std::string &name)
Get an uint8_t value.
static isc::asiolink::IOAddress getAddress(const ConstElementPtr &scope, const std::string &name)
Returns a IOAddress parameter from a scope.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
uint32_t getUint32(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint32_t.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
static int64_t getInteger(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer parameter from a scope.
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition duid.cc:73
Holds DUID (DHCPv6 Unique Identifier).
Definition duid.h:142
static bool upgradeLease6ExtendedInfo(const Lease6Ptr &lease, CfgConsistency::ExtendedInfoSanity check=CfgConsistency::EXTENDED_INFO_CHECK_FIX)
Upgrade a V6 lease user context to the new extended info entry.
Definition lease_mgr.cc:803
static void extractLease4ExtendedInfo(const Lease4Ptr &lease, bool ignore_errors=true)
Extract relay and remote identifiers from the extended info.
static bool upgradeLease4ExtendedInfo(const Lease4Ptr &lease, CfgConsistency::ExtendedInfoSanity check=CfgConsistency::EXTENDED_INFO_CHECK_FIX)
The following queries are used to fulfill Bulk Lease Query queries.
Definition lease_mgr.cc:582
virtual isc::dhcp::Lease4Ptr parse(isc::dhcp::ConstSrvConfigPtr &cfg, const isc::data::ConstElementPtr &lease_info, bool &force_create)
Parses Element tree and tries to convert to Lease4.
virtual isc::dhcp::Lease6Ptr parse(isc::dhcp::ConstSrvConfigPtr &cfg, const isc::data::ConstElementPtr &lease_info, bool &force_create)
Parses Element tree and tries to convert to Lease4.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
An abstract API for lease database.
ElementPtr copy(ConstElementPtr from, unsigned level)
Copy the data up to a nesting level.
Definition data.cc:1522
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
boost::shared_ptr< const SrvConfig > ConstSrvConfigPtr
Const pointer to the SrvConfig.
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
Definition subnet.h:620
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
Definition subnet.h:455
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:528
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:154
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
Defines the logger used by the top-level component of kea-lfc.
Hardware type that represents information from DHCPv4 packet.
Definition hwaddr.h:20
static HWAddr fromText(const std::string &text, const uint16_t htype=HTYPE_ETHER)
Creates instance of the hardware address from textual format.
Definition hwaddr.cc:69
Structure that holds a lease for IPv4 address.
Definition lease.h:323
Structure that holds a lease for IPv6 address and/or prefix.
Definition lease.h:536
static constexpr uint32_t STATE_DECLINED
Declined lease.
Definition lease.h:72
static constexpr uint32_t STATE_REGISTERED
Registered self-generated lease.
Definition lease.h:81
Type
Type of lease or pool.
Definition lease.h:46
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition lease.h:49
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47