Kea 2.7.7
pool.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2025 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 <dhcpsrv/pool.h>
12#include <boost/make_shared.hpp>
13#include <sstream>
14
15using namespace isc::asiolink;
16using namespace isc::data;
17
18namespace isc {
19namespace dhcp {
20
22 const isc::asiolink::IOAddress& last)
23 : id_(0), first_(first), last_(last), type_(type), capacity_(0),
24 cfg_option_(new CfgOption()) {
25}
26
27bool
29 return (first_ <= addr && addr <= last_);
30}
31
32bool
33Pool::clientSupported(const ClientClasses& classes) const {
34 return (client_classes_.empty() || client_classes_.intersects(classes));
35}
36
37void
39 if (!client_classes_.contains(class_name)) {
40 client_classes_.insert(class_name);
41 }
42}
43
44void
46 if (!additional_classes_.contains(class_name)) {
47 additional_classes_.insert(class_name);
48 }
49}
50
51std::string
52Pool::toText() const {
53 std::stringstream tmp;
54 tmp << "type=" << Lease::typeToText(type_) << ", " << first_
55 << "-" << last_;
56 return (tmp.str());
57}
58
59bool
76
78 const isc::asiolink::IOAddress& last)
79 : Pool(Lease::TYPE_V4, first, last) {
80 // check if specified address boundaries are sane
81 if (!first.isV4() || !last.isV4()) {
82 isc_throw(BadValue, "Invalid Pool4 address boundaries: not IPv4");
83 }
84
85 if (last < first) {
86 isc_throw(BadValue, "Upper boundary is smaller than lower boundary.");
87 }
88
89 // This is IPv4 pool, which only has one type. We can calculate
90 // the number of theoretically possible leases in it. As there's 2^32
91 // possible IPv4 addresses, we'll be able to accurately store that
92 // info.
93 capacity_ = addrsInRange(first, last);
94}
95
96Pool4::Pool4(const isc::asiolink::IOAddress& prefix, uint8_t prefix_len)
97 : Pool(Lease::TYPE_V4, prefix, IOAddress("0.0.0.0")) {
98
99 // check if the prefix is sane
100 if (!prefix.isV4()) {
101 isc_throw(BadValue, "Invalid Pool4 address boundaries: not IPv4");
102 }
103
104 // check if the prefix length is sane
105 if (prefix_len == 0 || prefix_len > 32) {
106 isc_throw(BadValue, "Invalid prefix length");
107 }
108
109 IOAddress first_address = firstAddrInPrefix(prefix, prefix_len);
110 if (first_address != prefix) {
111 isc_throw(BadValue, "Invalid Pool4 address boundaries: " << prefix
112 << " is not the first address in prefix: " << first_address
113 << "/" << static_cast<uint32_t>(prefix_len));
114 }
115
116 // Let's now calculate the last address in defined pool
117 last_ = lastAddrInPrefix(prefix, prefix_len);
118
119 // This is IPv4 pool, which only has one type. We can calculate
120 // the number of theoretically possible leases in it. As there's 2^32
121 // possible IPv4 addresses, we'll be able to accurately store that
122 // info.
123 capacity_ = addrsInRange(prefix, last_);
124}
125
127Pool4::create(const IOAddress& first, const IOAddress& last) {
128 return (boost::make_shared<Pool4>(first, last));
129}
130
132Pool4::create(const IOAddress& prefix, uint8_t prefix_len) {
133 return (boost::make_shared<Pool4>(prefix, prefix_len));
134}
135
138 // Prepare the map
140
141 // Set user-context
142 contextToElement(map);
143
144 // Set pool options
146 map->set("option-data", opts->toElement());
147
148 // Set client-classes
149 if (!client_classes_.empty()) {
150 map->set("client-classes", client_classes_.toElement());
151 }
152
153 // Set evaluate-additional-classes
154 if (!additional_classes_.empty()) {
155 map->set("evaluate-additional-classes",
157 }
158
159 if (id_) {
160 map->set("pool-id", Element::create(static_cast<long long>(id_)));
161 }
162
163 // Add in DDNS parameters for non-prefix pools.
164 if (type_ != Lease::TYPE_PD) {
166 map->set("ddns-send-updates", Element::create(ddns_send_updates_));
167 }
168
170 map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
171 }
172
174 map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
175 }
176
178 map->set("ddns-replace-client-name",
180 replaceClientNameModeToString(ddns_replace_client_name_mode_)));
181 }
182
184 map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
185 }
186
188 map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
189 }
190
192 map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
193 }
194
196 map->set("ddns-conflict-resolution-mode", Element::create(ddns_conflict_resolution_mode_));
197 }
198
200 map->set("ddns-ttl-percent", Element::create(ddns_ttl_percent_));
201 }
202
203 if (!ddns_ttl_.unspecified()) {
204 map->set("ddns-ttl", Element::create(ddns_ttl_));
205 }
206
207 if (!ddns_ttl_min_.unspecified()) {
208 map->set("ddns-ttl-min", Element::create(ddns_ttl_min_));
209 }
210
211 if (!ddns_ttl_max_.unspecified()) {
212 map->set("ddns-ttl-max", Element::create(ddns_ttl_max_));
213 }
214
216 map->set("hostname-char-set", Element::create(hostname_char_set_));
217 }
218
220 map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
221 }
222 }
223
224 return (map);
225}
226
229 // Prepare the map
231
232 // Set pool
233 const IOAddress& first = getFirstAddress();
234 const IOAddress& last = getLastAddress();
235 std::string range = first.toText() + "-" + last.toText();
236
237 // Try to output a prefix (vs a range)
238 int prefix_len = prefixLengthFromRange(first, last);
239 if (prefix_len >= 0) {
240 std::ostringstream oss;
241 oss << first.toText() << "/" << prefix_len;
242 range = oss.str();
243 }
244
245 map->set("pool", Element::create(range));
246 return (map);
247}
248
250 const isc::asiolink::IOAddress& last)
251 : Pool(type, first, last), prefix_len_(128), pd_exclude_option_() {
252
253 // check if specified address boundaries are sane
254 if (!first.isV6() || !last.isV6()) {
255 isc_throw(BadValue, "Invalid Pool6 address boundaries: not IPv6");
256 }
257
258 if ((type != Lease::TYPE_NA) && (type != Lease::TYPE_TA) &&
259 (type != Lease::TYPE_PD)) {
260 isc_throw(BadValue, "Invalid Pool6 type: " << static_cast<int>(type)
261 << ", must be TYPE_IA, TYPE_TA or TYPE_PD");
262 }
263
264 if (last < first) {
265 isc_throw(BadValue, "Upper boundary is smaller than lower boundary.");
266 // This check is a bit strict. If we decide that it is too strict,
267 // we need to comment it and uncomment lines below.
268 // On one hand, letting the user specify 2001::f - 2001::1 is nice, but
269 // on the other hand, 2001::1 may be a typo and the user really meant
270 // 2001::1:0 (or 1 followed by some hex digit), so a at least a warning
271 // would be useful.
272
273 // first_ = last;
274 // last_ = first;
275 }
276
277 // TYPE_PD is not supported by this constructor. first-last style
278 // parameters are for IA and TA only. There is another dedicated
279 // constructor for that (it uses prefix/length)
280 if ((type != Lease::TYPE_NA) && (type != Lease::TYPE_TA)) {
281 isc_throw(BadValue, "Invalid Pool6 type specified: "
282 << static_cast<int>(type));
283 }
284
285 // Let's calculate the theoretical number of leases in this pool.
286 // If the pool is extremely large (i.e. contains more than 2^64 addresses,
287 // we'll just cap it at max value of uint64_t).
288 capacity_ = addrsInRange(first, last);
289}
290
292 const uint8_t prefix_len, const uint8_t delegated_len /* = 128 */)
293 : Pool(type, prefix, IOAddress::IPV6_ZERO_ADDRESS()),
294 prefix_len_(delegated_len), pd_exclude_option_() {
295
296 init(type, prefix, prefix_len, delegated_len,
298}
299
300Pool6::Pool6(const asiolink::IOAddress& prefix, const uint8_t prefix_len,
301 const uint8_t delegated_len,
302 const asiolink::IOAddress& excluded_prefix,
303 const uint8_t excluded_prefix_len)
304 : Pool(Lease::TYPE_PD, prefix, IOAddress::IPV6_ZERO_ADDRESS()),
305 prefix_len_(delegated_len), pd_exclude_option_() {
306
307 init(Lease::TYPE_PD, prefix, prefix_len, delegated_len, excluded_prefix,
308 excluded_prefix_len);
309
310 // The excluded prefix can only be specified using this constructor.
311 // Therefore, the initialization of the excluded prefix is takes place
312 // here, rather than in the init(...) function.
313 if (!excluded_prefix.isV6()) {
314 isc_throw(BadValue, "excluded prefix must be an IPv6 prefix");
315 }
316
317 // An "unspecified" prefix should have both value and length equal to 0.
318 if ((excluded_prefix.isV6Zero() && (excluded_prefix_len != 0)) ||
319 (!excluded_prefix.isV6Zero() && (excluded_prefix_len == 0))) {
320 isc_throw(BadValue, "invalid excluded prefix "
321 << excluded_prefix << "/"
322 << static_cast<unsigned>(excluded_prefix_len));
323 }
324
325 // If excluded prefix has been specified.
326 if (!excluded_prefix.isV6Zero() && (excluded_prefix_len != 0)) {
327 // Excluded prefix length must not be greater than 128.
328 if (excluded_prefix_len > 128) {
329 isc_throw(BadValue, "excluded prefix length "
330 << static_cast<unsigned>(excluded_prefix_len)
331 << " must not be greater than 128");
332 }
333
334 // Excluded prefix must be a sub-prefix of a delegated prefix. First
335 // check the prefix length as it is less involved.
336 if (excluded_prefix_len <= prefix_len_) {
337 isc_throw(BadValue, "excluded prefix length "
338 << static_cast<unsigned>(excluded_prefix_len)
339 << " must be longer than the delegated prefix length "
340 << static_cast<unsigned>(prefix_len_));
341 }
342
348 }
349}
350
352Pool6::create(Lease::Type type, const IOAddress& first, const IOAddress& last) {
353 return (boost::make_shared<Pool6>(type, first, last));
354}
355
358 uint8_t prefix_len, uint8_t delegated_len) {
359 return (boost::make_shared<Pool6>(type, prefix, prefix_len, delegated_len));
360}
361
363Pool6::create(const IOAddress& prefix, const uint8_t prefix_len,
364 const uint8_t delegated_len, const IOAddress& excluded_prefix,
365 const uint8_t excluded_prefix_len) {
366 return (boost::make_shared<Pool6>(prefix, prefix_len,
367 delegated_len, excluded_prefix,
368 excluded_prefix_len));
369}
370
371void
372Pool6::init(const Lease::Type& type,
373 const asiolink::IOAddress& prefix,
374 const uint8_t prefix_len,
375 const uint8_t delegated_len,
376 const asiolink::IOAddress& excluded_prefix,
377 const uint8_t excluded_prefix_len) {
378 // Check if the prefix is sane
379 if (!prefix.isV6()) {
380 isc_throw(BadValue, "Invalid Pool6 address boundaries: not IPv6");
381 }
382
383 // Check if the prefix length is sane
384 if (prefix_len == 0 || prefix_len > 128) {
385 isc_throw(BadValue, "Invalid prefix length: "
386 << static_cast<unsigned>(prefix_len));
387 }
388
389 if (prefix_len > delegated_len) {
390 isc_throw(BadValue, "Delegated length ("
391 << static_cast<int>(delegated_len)
392 << ") must be longer than or equal to prefix length ("
393 << static_cast<int>(prefix_len) << ")");
394 }
395
396 if ((type != Lease::TYPE_PD) && (delegated_len != 128)) {
397 isc_throw(BadValue, "For NA or TA pools, delegated prefix length must"
398 << " be 128.");
399 }
400
401 // excluded_prefix_len == 0 means there's no excluded prefix at all.
402 if (excluded_prefix_len && (excluded_prefix_len <= delegated_len)) {
403 isc_throw(BadValue, "Excluded prefix ("
404 << static_cast<int>(excluded_prefix_len)
405 << ") must be longer than the delegated prefix length ("
406 << static_cast<int>(delegated_len) << ")");
407 }
408
409 if (prefix_len != 128) {
410 IOAddress first_address = firstAddrInPrefix(prefix, prefix_len);
411 if (first_address != prefix) {
412 isc_throw(BadValue, "Invalid Pool6 address boundaries: " << prefix
413 << " is not the first address in prefix: " << first_address
414 << "/" << static_cast<uint32_t>(prefix_len));
415 }
416 }
417
420
421 // Let's now calculate the last address in defined pool
422 last_ = lastAddrInPrefix(prefix, prefix_len);
423
424 // Let's calculate the theoretical number of leases in this pool.
425 // For addresses, we could use addrsInRange(prefix, last_), but it's
426 // much faster to do calculations on prefix lengths.
427 capacity_ = prefixesInRange(prefix_len, delegated_len);
428
429 // If user specified an excluded prefix, create an option that will
430 // be sent to clients obtaining prefixes from this pool.
431 if (excluded_prefix_len > 0) {
432 pd_exclude_option_.reset(new Option6PDExclude(prefix, delegated_len,
433 excluded_prefix,
434 excluded_prefix_len));
435 }
436}
437
440 // Prepare the map
442
443 switch (getType()) {
444 case Lease::TYPE_NA: {
445 const IOAddress& first = getFirstAddress();
446 const IOAddress& last = getLastAddress();
447 std::string range = first.toText() + "-" + last.toText();
448
449 // Try to output a prefix (vs a range)
450 int prefix_len = prefixLengthFromRange(first, last);
451 if (prefix_len >= 0) {
452 std::ostringstream oss;
453 oss << first.toText() << "/" << prefix_len;
454 range = oss.str();
455 }
456
457 map->set("pool", Element::create(range));
458 break;
459 }
460 case Lease::TYPE_PD: {
461 // Set prefix
462 const IOAddress& prefix = getFirstAddress();
463 map->set("prefix", Element::create(prefix.toText()));
464
465 // Set prefix-len (get it from min - max)
466 const IOAddress& last = getLastAddress();
467 int prefix_len = prefixLengthFromRange(prefix, last);
468 if (prefix_len < 0) {
469 // The pool is bad: give up
470 isc_throw(ToElementError, "invalid prefix range "
471 << prefix.toText() << "-" << last.toText());
472 }
473 map->set("prefix-len", Element::create(prefix_len));
474
475 // Set delegated-len
476 uint8_t len = getLength();
477 map->set("delegated-len", Element::create(static_cast<int>(len)));
478
479 // Set excluded prefix
481 if (xopt) {
482 const IOAddress& xprefix = xopt->getExcludedPrefix(prefix, len);
483 map->set("excluded-prefix", Element::create(xprefix.toText()));
484
485 uint8_t xlen = xopt->getExcludedPrefixLength();
486 map->set("excluded-prefix-len",
487 Element::create(static_cast<int>(xlen)));
488 }
489 // Let's not insert empty excluded-prefix values. If we ever
490 // decide to insert it after all, here's the code to do it:
491 // else {
492 // map->set("excluded-prefix",
493 // Element::create(std::string("::")));
494 // map->set("excluded-prefix-len", Element::create(0));
496
497 break;
498 }
499 default:
500 isc_throw(ToElementError, "Lease type: " << getType()
501 << ", unsupported for Pool6");
502 break;
503 }
504
505 return (map);
506}
507
508std::string
510 std::ostringstream s;
511 s << "type=" << Lease::typeToText(type_) << ", " << first_
512 << "-" << last_ << ", delegated_len="
513 << static_cast<unsigned>(prefix_len_);
514
515 if (pd_exclude_option_) {
516 s << ", excluded_prefix="
517 << pd_exclude_option_->getExcludedPrefix(first_, prefix_len_).toText()
518 << "/"
519 << static_cast<unsigned>(pd_exclude_option_->getExcludedPrefixLength());
520 }
521 return (s.str());
522}
523
524} // namespace dhcp
525} // namespace isc
if(!(yy_init))
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Cannot unparse error.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Represents option data configuration for the DHCP server.
Definition cfg_option.h:372
Container for storing client class names.
Definition classify.h:109
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition classify.cc:55
void insert(const ClientClass &class_name)
Insert an element.
Definition classify.h:159
bool empty() const
Check if classes is empty.
Definition classify.h:169
virtual isc::data::ElementPtr toElement() const
Returns all class names as an ElementPtr of type ListElement.
Definition classify.cc:95
bool intersects(const ClientClasses &cclasses) const
returns whether this container has at least one class in common with a given container.
Definition classify.cc:61
Acts as a storage vault for D2 client configuration.
virtual data::ElementPtr toElement() const
Unparse a Pool4 object.
Definition pool.cc:228
Pool4(const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
the constructor for Pool4 "min-max" style definition
Definition pool.cc:77
static Pool4Ptr create(const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
Factory function for creating an instance of the Pool4.
Definition pool.cc:127
uint8_t getLength() const
returns delegated prefix length
Definition pool.h:668
virtual data::ElementPtr toElement() const
Unparse a Pool6 object.
Definition pool.cc:439
Option6PDExcludePtr getPrefixExcludeOption() const
Returns instance of the pool specific Prefix Exclude option.
Definition pool.h:676
static Pool6Ptr create(Lease::Type type, const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
Factory function for creating an instance of the Pool6.
Definition pool.cc:352
virtual std::string toText() const
returns textual representation of the pool
Definition pool.cc:509
Pool6(Lease::Type type, const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
the constructor for Pool6 "min-max" style definition
Definition pool.cc:249
Lease::Type getType() const
returns pool type
Definition pool.h:659
base class for Pool4 and Pool6
Definition pool.h:34
virtual data::ElementPtr toElement() const
Unparse a pool object.
Definition pool.cc:137
util::Optional< uint32_t > ddns_ttl_
Explicit value to use for DNS TTL.
Definition pool.h:457
util::Optional< std::string > hostname_char_replacement_
A string to replace invalid characters when scrubbing hostnames.
Definition pool.h:471
util::Optional< uint32_t > ddns_ttl_max_
Maximum value to use for DNS TTL.
Definition pool.h:463
util::Optional< bool > ddns_update_on_renew_
Should Kea perform updates when leases are extended.
Definition pool.h:448
Pool(Lease::Type type, const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
protected constructor
Definition pool.cc:21
util::Optional< std::string > ddns_qualifying_suffix_
Suffix Kea should use when to qualify partial domain-names.
Definition pool.h:445
util::Optional< std::string > hostname_char_set_
Regular expression describing invalid characters for client hostnames.
Definition pool.h:467
const isc::asiolink::IOAddress & getFirstAddress() const
Returns the first address in a pool.
Definition pool.h:63
util::Optional< D2ClientConfig::ReplaceClientNameMode > ddns_replace_client_name_mode_
How Kea should handle the domain-name supplied by the client.
Definition pool.h:439
ClientClasses additional_classes_
Additional classes.
Definition pool.h:419
bool hasDdnsParameters() const
Checks if any of the DDNS parameters has a value.
Definition pool.cc:60
util::Optional< uint32_t > ddns_ttl_min_
Minimum value to use for DNS TTL.
Definition pool.h:460
uint64_t id_
pool-id
Definition pool.h:386
const isc::asiolink::IOAddress & getLastAddress() const
Returns the last address in a pool.
Definition pool.h:69
util::Optional< std::string > ddns_generated_prefix_
Prefix Kea should use when generating domain-names.
Definition pool.h:442
util::Optional< bool > ddns_override_client_update_
Should Kea perform updates, even if client requested delegation.
Definition pool.h:436
util::Optional< double > ddns_ttl_percent_
Percentage of the lease lifetime to use for DNS TTL.
Definition pool.h:454
isc::util::uint128_t capacity_
Stores number of possible leases.
Definition pool.h:403
ClientClasses client_classes_
List of client classes allowed to use this pool.
Definition pool.h:414
void allowClientClass(const isc::dhcp::ClientClass &class_name)
Adds class clas_name to the allowed client classes list.
Definition pool.cc:38
util::Optional< bool > ddns_override_no_update_
Should Kea perform updates, even if client requested no updates.
Definition pool.h:433
isc::asiolink::IOAddress last_
The last address in a pool.
Definition pool.h:392
isc::asiolink::IOAddress first_
The first address in a pool.
Definition pool.h:389
util::Optional< std::string > ddns_conflict_resolution_mode_
DDNS conflict resolution mode.
Definition pool.h:451
util::Optional< bool > ddns_send_updates_
Should Kea perform DNS updates.
Definition pool.h:429
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this pool.
Definition pool.h:106
virtual std::string toText() const
returns textual representation of the pool
Definition pool.cc:52
bool inRange(const isc::asiolink::IOAddress &addr) const
Checks if a given address is in the range.
Definition pool.cc:28
void addAdditionalClass(const ClientClass &class_name)
Adds class class_name to the additional classes list.
Definition pool.cc:45
bool clientSupported(const ClientClasses &client_classes) const
Checks whether this pool supports client that belongs to specified classes.
Definition pool.cc:33
Lease::Type type_
defines a lease type that will be served from this pool
Definition pool.h:395
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition optional.h:136
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< Pool4 > Pool4Ptr
a pointer an IPv4 Pool
Definition pool.h:477
std::string ClientClass
Defines a single class name.
Definition classify.h:43
boost::shared_ptr< Option6PDExclude > Option6PDExcludePtr
Pointer to the Option6PDExclude object.
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:895
boost::shared_ptr< Pool6 > Pool6Ptr
a pointer an IPv6 Pool
Definition pool.h:536
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
a common structure for IPv4 and IPv6 leases
Definition lease.h:31
Type
Type of lease or pool.
Definition lease.h:46
@ TYPE_TA
the lease contains temporary IPv6 address
Definition lease.h:48
@ 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
static std::string typeToText(Type type)
returns text representation of a lease type
Definition lease.cc:56