Kea 2.7.1
cfg_subnets6.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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#include <dhcp/dhcp6.h>
12#include <dhcpsrv/dhcpsrv_log.h>
14#include <dhcpsrv/subnet_id.h>
15#include <stats/stats_mgr.h>
16#include <boost/range/adaptor/reversed.hpp>
17#include <string.h>
18#include <sstream>
19
20using namespace isc::asiolink;
21using namespace isc::data;
22
23using namespace std;
24
25namespace isc {
26namespace dhcp {
27
28void
30 if (getBySubnetId(subnet->getID())) {
31 isc_throw(isc::dhcp::DuplicateSubnetID, "ID of the new IPv6 subnet '"
32 << subnet->getID() << "' is already in use");
33
34 } else if (getByPrefix(subnet->toText())) {
37 isc_throw(isc::dhcp::DuplicateSubnetID, "subnet with the prefix of '"
38 << subnet->toText() << "' already exists");
39 }
40
42 .arg(subnet->toText());
43 static_cast<void>(subnets_.insert(subnet));
44}
45
48 // Get the subnet with the same ID.
49 const SubnetID& subnet_id = subnet->getID();
50 auto& index = subnets_.template get<SubnetSubnetIdIndexTag>();
51 auto subnet_it = index.find(subnet_id);
52 if (subnet_it == index.end()) {
53 isc_throw(BadValue, "There is no IPv6 subnet with ID " << subnet_id);
54 }
55 Subnet6Ptr old = *subnet_it;
56 bool ret = index.replace(subnet_it, subnet);
57
59 .arg(subnet_id).arg(ret);
60 if (ret) {
61 return (old);
62 } else {
63 return (Subnet6Ptr());
64 }
65}
66
67void
69 del(subnet->getID());
70}
71
72void
73CfgSubnets6::del(const SubnetID& subnet_id) {
74 auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
75 auto subnet_it = index.find(subnet_id);
76 if (subnet_it == index.end()) {
77 isc_throw(BadValue, "no subnet with ID of '" << subnet_id
78 << "' found");
79 }
80
81 Subnet6Ptr subnet = *subnet_it;
82
83 index.erase(subnet_it);
84
86 .arg(subnet->toText());
87}
88
89void
91 CfgSubnets6& other) {
92 auto& index_id = subnets_.get<SubnetSubnetIdIndexTag>();
93 auto& index_prefix = subnets_.get<SubnetPrefixIndexTag>();
94
95 // Iterate over the subnets to be merged. They will replace the existing
96 // subnets with the same id. All new subnets will be inserted into the
97 // configuration into which we're merging.
98 auto const& other_subnets = other.getAll();
99 for (auto const& other_subnet : *other_subnets) {
100
101 // Check if there is a subnet with the same ID.
102 auto subnet_it = index_id.find(other_subnet->getID());
103 if (subnet_it != index_id.end()) {
104
105 // Subnet found.
106 auto existing_subnet = *subnet_it;
107
108 // If the existing subnet and other subnet
109 // are the same instance skip it.
110 if (existing_subnet == other_subnet) {
111 continue;
112 }
113
114 // We're going to replace the existing subnet with the other
115 // version. If it belongs to a shared network, we need
116 // remove it from that network.
117 SharedNetwork6Ptr network;
118 existing_subnet->getSharedNetwork(network);
119 if (network) {
120 network->del(existing_subnet->getID());
121 }
122
123 // Now we remove the existing subnet.
124 index_id.erase(subnet_it);
125 }
126
127 // Check if there is a subnet with the same prefix.
128 auto subnet_prefix_it = index_prefix.find(other_subnet->toText());
129 if (subnet_prefix_it != index_prefix.end()) {
130
131 // Subnet found.
132 auto existing_subnet = *subnet_prefix_it;
133
134 // Updating the id can lead to problems... e.g. reservation
135 // for the previous subnet ID.
136 // @todo: check reservations
137
138 // We're going to replace the existing subnet with the other
139 // version. If it belongs to a shared network, we need
140 // remove it from that network.
141 SharedNetwork6Ptr network;
142 existing_subnet->getSharedNetwork(network);
143 if (network) {
144 network->del(existing_subnet->getID());
145 }
146
147 // Now we remove the existing subnet.
148 index_prefix.erase(subnet_prefix_it);
149 }
150
151 // Create the subnet's options based on the given definitions.
152 other_subnet->getCfgOption()->createOptions(cfg_def);
153
154 // Encapsulate options, so that the DHCP server can effectively return
155 // them to the clients without having to encapsulate them for each request.
156 other_subnet->getCfgOption()->encapsulate();
157
158 // Create the options for pool based on the given definitions.
159 for (auto const& pool : other_subnet->getPoolsWritable(Lease::TYPE_NA)) {
160 pool->getCfgOption()->createOptions(cfg_def);
161 pool->getCfgOption()->encapsulate();
162 }
163
164 // Create the options for pd pool based on the given definitions.
165 for (auto const& pool : other_subnet->getPoolsWritable(Lease::TYPE_PD)) {
166 pool->getCfgOption()->createOptions(cfg_def);
167 pool->getCfgOption()->encapsulate();
168 }
169
170 // Add the "other" subnet to the our collection of subnets.
171 static_cast<void>(subnets_.insert(other_subnet));
172
173 // If it belongs to a shared network, find the network and
174 // add the subnet to it
175 std::string network_name = other_subnet->getSharedNetworkName();
176 if (!network_name.empty()) {
177 SharedNetwork6Ptr network = networks->getByName(network_name);
178 if (network) {
179 network->add(other_subnet);
180 } else {
181 // This implies the shared-network collection we were given
182 // is out of sync with the subnets we were given.
183 isc_throw(InvalidOperation, "Cannot assign subnet ID of "
184 << other_subnet->getID()
185 << " to shared network: " << network_name
186 << ", network does not exist");
187 }
188 }
189 // Instantiate the configured allocators and their states.
190 other_subnet->createAllocators();
191 }
192}
193
195CfgSubnets6::getByPrefix(const std::string& subnet_text) const {
196 auto const& index = subnets_.get<SubnetPrefixIndexTag>();
197 auto subnet_it = index.find(subnet_text);
198 return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet6Ptr());
199}
200
203 // Initialize subnet selector with the values used to select the subnet.
204 SubnetSelector selector;
205 selector.iface_name_ = query->getIface();
206 selector.remote_address_ = query->getRemoteAddr();
207 selector.first_relay_linkaddr_ = IOAddress("::");
208 selector.client_classes_ = query->classes_;
209
210 // Initialize fields specific to relayed messages.
211 if (!query->relay_info_.empty()) {
212 for (auto const& relay : boost::adaptors::reverse(query->relay_info_)) {
213 if (!relay.linkaddr_.isV6Zero() &&
214 !relay.linkaddr_.isV6LinkLocal()) {
215 selector.first_relay_linkaddr_ = relay.linkaddr_;
216 break;
217 }
218 }
219 selector.interface_id_ =
220 query->getAnyRelayOption(D6O_INTERFACE_ID,
222 }
223
224 return (selector);
225}
226
229 Subnet6Ptr subnet;
230
231 // If relay agent link address is set to zero it means that we're dealing
232 // with a directly connected client.
233 if (selector.first_relay_linkaddr_ == IOAddress("::")) {
234 // If interface name is known try to match it with interface names
235 // specified for configured subnets.
236 if (!selector.iface_name_.empty()) {
237 subnet = selectSubnet(selector.iface_name_,
238 selector.client_classes_);
239 }
240
241 // If interface name didn't match, try the client's address.
242 if (!subnet && selector.remote_address_ != IOAddress("::")) {
243 subnet = selectSubnet(selector.remote_address_,
244 selector.client_classes_);
245 }
246
247 // If relay agent link address is set, we're dealing with a relayed message.
248 } else {
249 // Find the subnet using the Interface Id option, if present.
250 subnet = selectSubnet(selector.interface_id_, selector.client_classes_);
251
252 // If Interface ID option could not be matched for any subnet, try
253 // the relay agent link address.
254 if (!subnet) {
255 subnet = selectSubnet(selector.first_relay_linkaddr_,
256 selector.client_classes_,
257 true);
258 }
259 }
260
261 // Return subnet found, or NULL if not found.
262 return (subnet);
263}
264
267 const ClientClasses& client_classes,
268 const bool is_relay_address) const {
269 // If the specified address is a relay address we first need to match
270 // it with the relay addresses specified for all subnets.
271 if (is_relay_address) {
272 for (auto const& subnet : subnets_) {
273
274 // If the specified address matches a relay address, return this
275 // subnet.
276 if (subnet->hasRelays()) {
277 if (!subnet->hasRelayAddress(address)) {
278 continue;
279 }
280
281 } else {
282 SharedNetwork6Ptr network;
283 subnet->getSharedNetwork(network);
284 if (!network || !network->hasRelayAddress(address)) {
285 continue;
286 }
287 }
288
289 if (subnet->clientSupported(client_classes)) {
290 // The relay address is matching the one specified for a subnet
291 // or its shared network.
294 .arg(subnet->toText()).arg(address.toText());
295 return (subnet);
296 }
297 }
298 }
299
300 // No success so far. Check if the specified address is in range
301 // with any subnet.
302 for (auto const& subnet : subnets_) {
303 if (subnet->inRange(address) && subnet->clientSupported(client_classes)) {
305 .arg(subnet->toText()).arg(address.toText());
306 return (subnet);
307 }
308 }
309
312 .arg(address.toText());
313
314 // Nothing found.
315 return (Subnet6Ptr());
316}
317
319CfgSubnets6::selectSubnet(const std::string& iface_name,
320 const ClientClasses& client_classes) const {
321 // If empty interface specified, we can't select subnet by interface.
322 if (!iface_name.empty()) {
323 for (auto const& subnet : subnets_) {
324
325 // If interface name matches with the one specified for the subnet
326 // and the client is not rejected based on the classification,
327 // return the subnet.
328 if ((subnet->getIface() == iface_name) &&
329 subnet->clientSupported(client_classes)) {
332 .arg(subnet->toText()).arg(iface_name);
333 return (subnet);
334 }
335 }
336 }
337
340 .arg(iface_name);
341
342 // No subnet found for this interface name.
343 return (Subnet6Ptr());
344}
345
347CfgSubnets6::selectSubnet(const OptionPtr& interface_id,
348 const ClientClasses& client_classes) const {
349 // We can only select subnet using an interface id, if the interface
350 // id is known.
351 if (interface_id) {
352 for (auto const& subnet : subnets_) {
353
354 // If interface id matches for the subnet and the subnet is not
355 // rejected based on the classification.
356 if (subnet->getInterfaceId() &&
357 subnet->getInterfaceId()->equals(interface_id) &&
358 subnet->clientSupported(client_classes)) {
359
362 .arg(subnet->toText());
363 return (subnet);
364 }
365 }
366
369 .arg(interface_id->toText());
370 }
371
372 // No subnet found.
373 return (Subnet6Ptr());
374}
375
377CfgSubnets6::getSubnet(const SubnetID subnet_id) const {
378 auto const& index = subnets_.get<SubnetSubnetIdIndexTag>();
379 auto subnet_it = index.find(subnet_id);
380 return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet6Ptr());
381}
382
384CfgSubnets6::getLinks(const IOAddress& link_addr) const {
385 SubnetIDSet links;
386 for (auto const& subnet : subnets_) {
387 if (!subnet->inRange(link_addr)) {
388 continue;
389 }
390 links.insert(subnet->getID());
391 }
392 return (links);
393}
394
395void
397 using namespace isc::stats;
398
399 StatsMgr& stats_mgr = StatsMgr::instance();
400 // For each v6 subnet currently configured, remove the statistics.
401 for (auto const& subnet6 : subnets_) {
402 SubnetID subnet_id = subnet6->getID();
403 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
404 "total-nas"));
405
406 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
407 "assigned-nas"));
408
409 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
410 "cumulative-assigned-nas"));
411
412 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
413 "total-pds"));
414
415 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
416 "assigned-pds"));
417
418 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
419 "cumulative-assigned-pds"));
420
421 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
422 "declined-addresses"));
423
424 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
425 "reclaimed-declined-addresses"));
426
427 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
428 "reclaimed-leases"));
429
430 for (auto const& pool : subnet6->getPools(Lease::TYPE_NA)) {
431 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
432 StatsMgr::generateName("pool", pool->getID(),
433 "total-nas")));
434
435 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
436 StatsMgr::generateName("pool", pool->getID(),
437 "assigned-nas")));
438
439 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
440 StatsMgr::generateName("pool", pool->getID(),
441 "cumulative-assigned-nas")));
442
443 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
444 StatsMgr::generateName("pool", pool->getID(),
445 "declined-addresses")));
446
447 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
448 StatsMgr::generateName("pool", pool->getID(),
449 "reclaimed-declined-addresses")));
450
451 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
452 StatsMgr::generateName("pool", pool->getID(),
453 "reclaimed-leases")));
454 }
455
456 for (auto const& pool : subnet6->getPools(Lease::TYPE_PD)) {
457 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
458 StatsMgr::generateName("pd-pool", pool->getID(),
459 "total-pds")));
460
461 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
462 StatsMgr::generateName("pd-pool", pool->getID(),
463 "assigned-pds")));
464
465 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
466 StatsMgr::generateName("pd-pool", pool->getID(),
467 "cumulative-assigned-pds")));
468
469 stats_mgr.del(StatsMgr::generateName("subnet", subnet_id,
470 StatsMgr::generateName("pd-pool", pool->getID(),
471 "reclaimed-leases")));
472 }
473 }
474}
475
476void
478 using namespace isc::stats;
479
480 StatsMgr& stats_mgr = StatsMgr::instance();
481 // For each v6 subnet currently configured, calculate totals
482 for (auto const& subnet6 : subnets_) {
483 SubnetID subnet_id = subnet6->getID();
484
485 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
486 "total-nas"),
487 subnet6->getPoolCapacity(Lease::TYPE_NA));
488
489 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
490 "total-pds"),
491 subnet6->getPoolCapacity(Lease::TYPE_PD));
492
493 const std::string& name_nas(StatsMgr::generateName("subnet", subnet_id,
494 "cumulative-assigned-nas"));
495 if (!stats_mgr.getObservation(name_nas)) {
496 stats_mgr.setValue(name_nas, static_cast<int64_t>(0));
497 }
498
499 const std::string& name_pds(StatsMgr::generateName("subnet", subnet_id,
500 "cumulative-assigned-pds"));
501 if (!stats_mgr.getObservation(name_pds)) {
502 stats_mgr.setValue(name_pds, static_cast<int64_t>(0));
503 }
504
505 string const& name_ia_na_reuses(StatsMgr::generateName("subnet", subnet_id,
506 "v6-ia-na-lease-reuses"));
507 if (!stats_mgr.getObservation(name_ia_na_reuses)) {
508 stats_mgr.setValue(name_ia_na_reuses, int64_t(0));
509 }
510
511 string const& name_ia_pd_reuses(StatsMgr::generateName("subnet", subnet_id,
512 "v6-ia-pd-lease-reuses"));
513 if (!stats_mgr.getObservation(name_ia_pd_reuses)) {
514 stats_mgr.setValue(name_ia_pd_reuses, int64_t(0));
515 }
516
517 for (auto const& pool : subnet6->getPools(Lease::TYPE_NA)) {
518 const std::string& name_total_nas(StatsMgr::generateName("subnet", subnet_id,
519 StatsMgr::generateName("pool", pool->getID(),
520 "total-nas")));
521 if (!stats_mgr.getObservation(name_total_nas)) {
522 stats_mgr.setValue(name_total_nas, pool->getCapacity());
523 } else {
524 stats_mgr.addValue(name_total_nas, pool->getCapacity());
525 }
526
527 const std::string& name_ca_nas(StatsMgr::generateName("subnet", subnet_id,
528 StatsMgr::generateName("pool", pool->getID(),
529 "cumulative-assigned-nas")));
530 if (!stats_mgr.getObservation(name_ca_nas)) {
531 stats_mgr.setValue(name_ca_nas, static_cast<int64_t>(0));
532 }
533 }
534
535 for (auto const& pool : subnet6->getPools(Lease::TYPE_PD)) {
536 const std::string& name_total_pds(StatsMgr::generateName("subnet", subnet_id,
537 StatsMgr::generateName("pd-pool", pool->getID(),
538 "total-pds")));
539 if (!stats_mgr.getObservation(name_total_pds)) {
540 stats_mgr.setValue(name_total_pds, pool->getCapacity());
541 } else {
542 stats_mgr.addValue(name_total_pds, pool->getCapacity());
543 }
544
545 const std::string& name_ca_pds(StatsMgr::generateName("subnet", subnet_id,
546 StatsMgr::generateName("pd-pool", pool->getID(),
547 "cumulative-assigned-pds")));
548 if (!stats_mgr.getObservation(name_ca_pds)) {
549 stats_mgr.setValue(name_ca_pds, static_cast<int64_t>(0));
550 }
551 }
552 }
553
554 // Only recount the stats if we have subnets.
555 if (subnets_.begin() != subnets_.end()) {
556 LeaseMgrFactory::instance().recountLeaseStats6();
557 }
558}
559
560void
562 for (auto const& subnet : subnets_) {
563 subnet->initAllocatorsAfterConfigure();
564 }
565}
566
567void
569 subnets_.clear();
570}
571
575 // Iterate subnets
576 for (auto const& subnet : subnets_) {
577 result->add(subnet->toElement());
578 }
579 return (result);
580}
581
582} // end of namespace isc::dhcp
583} // end of namespace isc
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.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
Holds subnets configured for the DHCPv6 server.
SubnetIDSet getLinks(const asiolink::IOAddress &link_addr) const
Convert a link address into a link set.
void updateStatistics()
Updates statistics.
Subnet6Ptr replace(const Subnet6Ptr &subnet)
Replaces subnet in the configuration.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
void merge(CfgOptionDefPtr cfg_def, CfgSharedNetworks6Ptr networks, CfgSubnets6 &other)
Merges specified subnet configuration into this configuration.
Subnet6Ptr selectSubnet(const SubnetSelector &selector) const
Selects a subnet using parameters specified in the selector.
Subnet6Ptr getSubnet(const SubnetID id) const
Returns subnet with specified subnet-id value.
void removeStatistics()
Removes statistics.
void add(const Subnet6Ptr &subnet)
Adds new subnet to the configuration.
void clear()
Clears all subnets from the configuration.
static SubnetSelector initSelector(const Pkt6Ptr &query)
Build selector from a client's message.
void del(const ConstSubnet6Ptr &subnet)
Removes subnet from the configuration.
ConstSubnet6Ptr getByPrefix(const std::string &subnet_prefix) const
Returns const pointer to a subnet which matches the specified prefix in the canonical form.
void initAllocatorsAfterConfigure()
Calls initAllocatorsAfterConfigure for each subnet.
ConstSubnet6Ptr getBySubnetId(const SubnetID &subnet_id) const
Returns const pointer to a subnet identified by the specified subnet identifier.
Container for storing client class names.
Definition classify.h:108
Exception thrown upon attempt to add subnet with an ID that belongs to the subnet that already exists...
Definition subnet_id.h:36
static TrackingLeaseMgr & instance()
Return current lease manager.
@ RELAY_GET_FIRST
Definition pkt6.h:77
Statistics Manager class.
@ D6O_INTERFACE_ID
Definition dhcp6.h:38
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_CFGMGR_SUBNET6_IFACE
const isc::log::MessageID DHCPSRV_CFGMGR_ADD_SUBNET6
std::set< dhcp::SubnetID > SubnetIDSet
Ordered list aka set of subnetIDs.
Definition subnet_id.h:43
const isc::log::MessageID DHCPSRV_CFGMGR_SUBNET6_RELAY
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
Definition subnet.h:620
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
const isc::log::MessageID DHCPSRV_CFGMGR_UPDATE_SUBNET6
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition subnet.h:623
boost::shared_ptr< SharedNetwork6 > SharedNetwork6Ptr
Pointer to SharedNetwork6 object.
boost::shared_ptr< CfgSharedNetworks6 > CfgSharedNetworks6Ptr
Pointer to the configuration of IPv6 shared networks.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
const isc::log::MessageID DHCPSRV_CFGMGR_SUBNET6
const isc::log::MessageID DHCPSRV_CFGMGR_SUBNET6_IFACE_ID
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition pkt6.h:31
const isc::log::MessageID DHCPSRV_CFGMGR_DEL_SUBNET6
const isc::log::MessageID DHCPSRV_SUBNET6_SELECT_BY_INTERFACE_ID_NO_MATCH
const isc::log::MessageID DHCPSRV_SUBNET6_SELECT_BY_ADDRESS_NO_MATCH
const isc::log::MessageID DHCPSRV_SUBNET6_SELECT_BY_INTERFACE_NO_MATCH
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Definition dhcpsrv_log.h:26
Defines the logger used by the top-level component of kea-lfc.
@ 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
Tag for the index for searching by subnet prefix.
Definition subnet.h:777
Subnet selector used to specify parameters used to select a subnet.
std::string iface_name_
Name of the interface on which the message was received.
Tag for the index for searching by subnet identifier.
Definition subnet.h:774