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 | // Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option_custom.h>
#include <asiolink/addr_utilities.h>
#include <dhcpsrv/cfg_subnets6.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/subnet_id.h>
#include <stats/stats_mgr.h>
#include <boost/range/adaptor/reversed.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::asiolink;
using namespace isc::data;
using namespace std;
namespace isc {
namespace dhcp {
void
CfgSubnets6::add(const Subnet6Ptr& subnet) {
if (getBySubnetId(subnet->getID())) {
isc_throw(isc::dhcp::DuplicateSubnetID, "ID of the new IPv6 subnet '"
<< subnet->getID() << "' is already in use");
} else if (getByPrefix(subnet->toText())) {
/// @todo: Check that this new subnet does not cross boundaries of any
/// other already defined subnet.
isc_throw(isc::dhcp::DuplicateSubnetID, "subnet with the prefix of '"
<< subnet->toText() << "' already exists");
}
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_SUBNET6)
.arg(subnet->toText());
static_cast<void>(subnets_.insert(subnet));
}
Subnet6Ptr
CfgSubnets6::replace(const Subnet6Ptr& subnet) {
// Get the subnet with the same ID.
const SubnetID& subnet_id = subnet->getID();
auto& index = subnets_.template get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet_id);
if (subnet_it == index.end()) {
isc_throw(BadValue, "There is no IPv6 subnet with ID " << subnet_id);
}
Subnet6Ptr old = *subnet_it;
bool ret = index.replace(subnet_it, subnet);
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_UPDATE_SUBNET6)
.arg(subnet_id).arg(ret);
if (ret) {
return (old);
} else {
return (Subnet6Ptr());
}
}
void
CfgSubnets6::del(const ConstSubnet6Ptr& subnet) {
del(subnet->getID());
}
void
CfgSubnets6::del(const SubnetID& subnet_id) {
auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet_id);
if (subnet_it == index.end()) {
isc_throw(BadValue, "no subnet with ID of '" << subnet_id
<< "' found");
}
Subnet6Ptr subnet = *subnet_it;
index.erase(subnet_it);
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DEL_SUBNET6)
.arg(subnet->toText());
}
void
CfgSubnets6::merge(CfgOptionDefPtr cfg_def, CfgSharedNetworks6Ptr networks,
CfgSubnets6& other) {
auto& index_id = subnets_.get<SubnetSubnetIdIndexTag>();
auto& index_prefix = subnets_.get<SubnetPrefixIndexTag>();
// Iterate over the subnets to be merged. They will replace the existing
// subnets with the same id. All new subnets will be inserted into the
// configuration into which we're merging.
auto const& other_subnets = other.getAll();
for (auto const& other_subnet : *other_subnets) {
// Check if there is a subnet with the same ID.
auto subnet_it = index_id.find(other_subnet->getID());
if (subnet_it != index_id.end()) {
// Subnet found.
auto existing_subnet = *subnet_it;
// If the existing subnet and other subnet
// are the same instance skip it.
if (existing_subnet == other_subnet) {
continue;
}
// We're going to replace the existing subnet with the other
// version. If it belongs to a shared network, we need
// remove it from that network.
SharedNetwork6Ptr network;
existing_subnet->getSharedNetwork(network);
if (network) {
network->del(existing_subnet->getID());
}
// Now we remove the existing subnet.
index_id.erase(subnet_it);
}
// Check if there is a subnet with the same prefix.
auto subnet_prefix_it = index_prefix.find(other_subnet->toText());
if (subnet_prefix_it != index_prefix.end()) {
// Subnet found.
auto existing_subnet = *subnet_prefix_it;
// Updating the id can lead to problems... e.g. reservation
// for the previous subnet ID.
// @todo: check reservations
// We're going to replace the existing subnet with the other
// version. If it belongs to a shared network, we need
// remove it from that network.
SharedNetwork6Ptr network;
existing_subnet->getSharedNetwork(network);
if (network) {
network->del(existing_subnet->getID());
}
// Now we remove the existing subnet.
index_prefix.erase(subnet_prefix_it);
}
// Create the subnet's options based on the given definitions.
other_subnet->getCfgOption()->createOptions(cfg_def);
// Encapsulate options, so that the DHCP server can effectively return
// them to the clients without having to encapsulate them for each request.
other_subnet->getCfgOption()->encapsulate();
// Create the options for pool based on the given definitions.
for (auto const& pool : other_subnet->getPoolsWritable(Lease::TYPE_NA)) {
pool->getCfgOption()->createOptions(cfg_def);
pool->getCfgOption()->encapsulate();
}
// Create the options for pd pool based on the given definitions.
for (auto const& pool : other_subnet->getPoolsWritable(Lease::TYPE_PD)) {
pool->getCfgOption()->createOptions(cfg_def);
pool->getCfgOption()->encapsulate();
}
// Add the "other" subnet to the our collection of subnets.
static_cast<void>(subnets_.insert(other_subnet));
// If it belongs to a shared network, find the network and
// add the subnet to it
std::string network_name = other_subnet->getSharedNetworkName();
if (!network_name.empty()) {
SharedNetwork6Ptr network = networks->getByName(network_name);
if (network) {
network->add(other_subnet);
} else {
// This implies the shared-network collection we were given
// is out of sync with the subnets we were given.
isc_throw(InvalidOperation, "Cannot assign subnet ID of "
<< other_subnet->getID()
<< " to shared network: " << network_name
<< ", network does not exist");
}
}
// Instantiate the configured allocators and their states.
other_subnet->createAllocators();
}
}
ConstSubnet6Ptr
CfgSubnets6::getByPrefix(const std::string& subnet_text) const {
auto const& index = subnets_.get<SubnetPrefixIndexTag>();
auto subnet_it = index.find(subnet_text);
return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet6Ptr());
}
SubnetSelector
CfgSubnets6::initSelector(const Pkt6Ptr& query) {
// Initialize subnet selector with the values used to select the subnet.
SubnetSelector selector;
selector.iface_name_ = query->getIface();
selector.remote_address_ = query->getRemoteAddr();
selector.first_relay_linkaddr_ = IOAddress("::");
selector.client_classes_ = query->classes_;
// Initialize fields specific to relayed messages.
if (!query->relay_info_.empty()) {
for (auto const& relay : boost::adaptors::reverse(query->relay_info_)) {
if (!relay.linkaddr_.isV6Zero() &&
!relay.linkaddr_.isV6LinkLocal()) {<--- Consider using std::find_if algorithm instead of a raw loop.
selector.first_relay_linkaddr_ = relay.linkaddr_;
break;
}
}
selector.interface_id_ =
query->getAnyRelayOption(D6O_INTERFACE_ID,
Pkt6::RELAY_GET_FIRST);
}
return (selector);
}
ConstSubnet6Ptr
CfgSubnets6::selectSubnet(const SubnetSelector& selector) const {
ConstSubnet6Ptr subnet;
// If relay agent link address is set to zero it means that we're dealing
// with a directly connected client.
if (selector.first_relay_linkaddr_ == IOAddress("::")) {
// If interface name is known try to match it with interface names
// specified for configured subnets.
if (!selector.iface_name_.empty()) {
subnet = selectSubnet(selector.iface_name_,
selector.client_classes_);
}
// If interface name didn't match, try the client's address.
if (!subnet && selector.remote_address_ != IOAddress("::")) {
subnet = selectSubnet(selector.remote_address_,
selector.client_classes_);
}
// If relay agent link address is set, we're dealing with a relayed message.
} else {
// Find the subnet using the Interface Id option, if present.
subnet = selectSubnet(selector.interface_id_, selector.client_classes_);
// If Interface ID option could not be matched for any subnet, try
// the relay agent link address.
if (!subnet) {
subnet = selectSubnet(selector.first_relay_linkaddr_,
selector.client_classes_,
true);
}
}
// Return subnet found, or NULL if not found.
return (subnet);
}
ConstSubnet6Ptr
CfgSubnets6::selectSubnet(const asiolink::IOAddress& address,
const ClientClasses& client_classes,
const bool is_relay_address) const {
// If the specified address is a relay address we first need to match
// it with the relay addresses specified for all subnets.
if (is_relay_address) {
for (auto const& subnet : subnets_) {
// If the specified address matches a relay address, return this
// subnet.
if (subnet->hasRelays()) {
if (!subnet->hasRelayAddress(address)) {
continue;
}
} else {
SharedNetwork6Ptr network;
subnet->getSharedNetwork(network);
if (!network || !network->hasRelayAddress(address)) {
continue;
}
}
if (subnet->clientSupported(client_classes)) {
// The relay address is matching the one specified for a subnet
// or its shared network.
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_CFGMGR_SUBNET6_RELAY)
.arg(subnet->toText()).arg(address.toText());
return (subnet);
}
}
}
// No success so far. Check if the specified address is in range
// with any subnet.
for (auto const& subnet : subnets_) {
if (subnet->inRange(address) && subnet->clientSupported(client_classes)) {<--- Consider using std::find_if algorithm instead of a raw loop.
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_SUBNET6)
.arg(subnet->toText()).arg(address.toText());
return (subnet);
}
}
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_SUBNET6_SELECT_BY_ADDRESS_NO_MATCH)
.arg(address.toText());
// Nothing found.
return (ConstSubnet6Ptr());
}
ConstSubnet6Ptr
CfgSubnets6::selectSubnet(const std::string& iface_name,
const ClientClasses& client_classes) const {
// If empty interface specified, we can't select subnet by interface.
if (!iface_name.empty()) {
for (auto const& subnet : subnets_) {
// If interface name matches with the one specified for the subnet
// and the client is not rejected based on the classification,
// return the subnet.
if ((subnet->getIface() == iface_name) &&
subnet->clientSupported(client_classes)) {<--- Consider using std::find_if algorithm instead of a raw loop.
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_CFGMGR_SUBNET6_IFACE)
.arg(subnet->toText()).arg(iface_name);
return (subnet);
}
}
}
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_SUBNET6_SELECT_BY_INTERFACE_NO_MATCH)
.arg(iface_name);
// No subnet found for this interface name.
return (ConstSubnet6Ptr());
}
ConstSubnet6Ptr
CfgSubnets6::selectSubnet(const OptionPtr& interface_id,
const ClientClasses& client_classes) const {
// We can only select subnet using an interface id, if the interface
// id is known.
if (interface_id) {
for (auto const& subnet : subnets_) {
// If interface id matches for the subnet and the subnet is not
// rejected based on the classification.
if (subnet->getInterfaceId() &&
subnet->getInterfaceId()->equals(interface_id) &&
subnet->clientSupported(client_classes)) {<--- Consider using std::find_if algorithm instead of a raw loop.
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_CFGMGR_SUBNET6_IFACE_ID)
.arg(subnet->toText());
return (subnet);
}
}
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_SUBNET6_SELECT_BY_INTERFACE_ID_NO_MATCH)
.arg(interface_id->toText());
}
// No subnet found.
return (ConstSubnet6Ptr());
}
Subnet6Ptr
CfgSubnets6::getSubnet(const SubnetID subnet_id) const {
auto const& index = subnets_.get<SubnetSubnetIdIndexTag>();
auto subnet_it = index.find(subnet_id);
return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet6Ptr());
}
SubnetIDSet
CfgSubnets6::getLinks(const IOAddress& link_addr) const {
SubnetIDSet links;
for (auto const& subnet : subnets_) {
if (!subnet->inRange(link_addr)) {
continue;
}
links.insert(subnet->getID());
}
return (links);
}
void
CfgSubnets6::removeStatistics() {
using namespace isc::stats;
StatsMgr& stats_mgr = StatsMgr::instance();
// For each v6 subnet currently configured, remove the statistics.
for (auto const& subnet6 : subnets_) {
SubnetID subnet_id = subnet6->getID();
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"total-nas"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"assigned-nas"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-nas"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"total-pds"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"assigned-pds"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-pds"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"));
for (auto const& pool : subnet6->getPools(Lease::TYPE_NA)) {
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"total-nas")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"assigned-nas")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"cumulative-assigned-nas")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"declined-addresses")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"reclaimed-declined-addresses")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"reclaimed-leases")));
}
for (auto const& pool : subnet6->getPools(Lease::TYPE_PD)) {
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pd-pool", pool->getID(),
"total-pds")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pd-pool", pool->getID(),
"assigned-pds")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pd-pool", pool->getID(),
"cumulative-assigned-pds")));
stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pd-pool", pool->getID(),
"reclaimed-leases")));
}
}
}
void
CfgSubnets6::updateStatistics() {
using namespace isc::stats;
StatsMgr& stats_mgr = StatsMgr::instance();
// For each v6 subnet currently configured, calculate totals
for (auto const& subnet6 : subnets_) {
SubnetID subnet_id = subnet6->getID();
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"total-nas"),
subnet6->getPoolCapacity(Lease::TYPE_NA));
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"total-pds"),
subnet6->getPoolCapacity(Lease::TYPE_PD));
const std::string& name_nas(StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-nas"));
if (!stats_mgr.getObservation(name_nas)) {
stats_mgr.setValue(name_nas, static_cast<int64_t>(0));
}
const std::string& name_pds(StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-pds"));
if (!stats_mgr.getObservation(name_pds)) {
stats_mgr.setValue(name_pds, static_cast<int64_t>(0));
}
string const& name_ia_na_reuses(StatsMgr::generateName("subnet", subnet_id,
"v6-ia-na-lease-reuses"));
if (!stats_mgr.getObservation(name_ia_na_reuses)) {
stats_mgr.setValue(name_ia_na_reuses, int64_t(0));
}
string const& name_ia_pd_reuses(StatsMgr::generateName("subnet", subnet_id,
"v6-ia-pd-lease-reuses"));
if (!stats_mgr.getObservation(name_ia_pd_reuses)) {
stats_mgr.setValue(name_ia_pd_reuses, int64_t(0));
}
for (auto const& pool : subnet6->getPools(Lease::TYPE_NA)) {
const std::string& name_total_nas(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"total-nas")));
if (!stats_mgr.getObservation(name_total_nas)) {
stats_mgr.setValue(name_total_nas, pool->getCapacity());
} else {
stats_mgr.addValue(name_total_nas, pool->getCapacity());
}
const std::string& name_ca_nas(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", pool->getID(),
"cumulative-assigned-nas")));
if (!stats_mgr.getObservation(name_ca_nas)) {
stats_mgr.setValue(name_ca_nas, static_cast<int64_t>(0));
}
}
for (auto const& pool : subnet6->getPools(Lease::TYPE_PD)) {
const std::string& name_total_pds(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pd-pool", pool->getID(),
"total-pds")));
if (!stats_mgr.getObservation(name_total_pds)) {
stats_mgr.setValue(name_total_pds, pool->getCapacity());
} else {
stats_mgr.addValue(name_total_pds, pool->getCapacity());
}
const std::string& name_ca_pds(StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pd-pool", pool->getID(),
"cumulative-assigned-pds")));
if (!stats_mgr.getObservation(name_ca_pds)) {
stats_mgr.setValue(name_ca_pds, static_cast<int64_t>(0));
}
}
}
// Only recount the stats if we have subnets.
if (subnets_.begin() != subnets_.end()) {
LeaseMgrFactory::instance().recountLeaseStats6();
}
}
void
CfgSubnets6::initAllocatorsAfterConfigure() {
for (auto const& subnet : subnets_) {
subnet->initAllocatorsAfterConfigure();
}
}
void
CfgSubnets6::clear() {
subnets_.clear();
}
ElementPtr
CfgSubnets6::toElement() const {
ElementPtr result = Element::createList();
// Iterate subnets
for (auto const& subnet : subnets_) {
result->add(subnet->toElement());
}
return (result);
}
} // end of namespace isc::dhcp
} // end of namespace isc
|