Kea 2.2.0
cb_ctl_dhcp6.cc
Go to the documentation of this file.
1// Copyright (C) 2019-2022 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>
9#include <dhcpsrv/cfgmgr.h>
10#include <dhcpsrv/dhcpsrv_log.h>
11#include <dhcpsrv/host_mgr.h>
14#include <hooks/hooks_manager.h>
15
16using namespace isc::db;
17using namespace isc::data;
18using namespace isc::process;
19using namespace isc::hooks;
20
21namespace {
22
24struct CbCtlHooks {
25 int hook_index_cb6_updated_;
26
28 CbCtlHooks() {
29 hook_index_cb6_updated_ = HooksManager::registerHook("cb6_updated");
30 }
31};
32
33// Declare a Hooks object. As this is outside any function or method, it
34// will be instantiated (and the constructor run) when the module is loaded.
35// As a result, the hook indexes will be defined before any method in this
36// module is called.
37CbCtlHooks hooks_;
38
39}; // anonymous namespace
40
41namespace isc {
42namespace dhcp {
43
44void
46 const db::ServerSelector& server_selector,
47 const boost::posix_time::ptime& lb_modification_time,
48 const db::AuditEntryCollection& audit_entries) {
49 bool globals_fetched = false;
50
51 // Let's first delete all the configuration elements for which DELETE audit
52 // entries are found. Although, this may break chronology of the audit in
53 // some cases it should not affect the end result of the data fetch. If the
54 // object was created and then subsequently deleted, we will first try to
55 // delete this object from the local configuration (which will fail because
56 // the object does not exist) and then we will try to fetch it from the
57 // database which will return no result.
58 if (!audit_entries.empty()) {
59
60 auto cfg = CfgMgr::instance().getCurrentCfg();
61 auto external_cfg = CfgMgr::instance().createExternalCfg();
62
63 // Get audit entries for deleted global parameters.
64 const auto& index = audit_entries.get<AuditEntryObjectTypeTag>();
65 auto range = index.equal_range(boost::make_tuple("dhcp6_global_parameter",
66 AuditEntry::ModificationType::DELETE));
67 if (range.first != range.second) {
68 // Some globals have been deleted. Since we currently don't track database
69 // identifiers of the global parameters we have to fetch all global
70 // parameters for this server. Next, we simply replace existing
71 // global parameters with the new parameters. This is slightly
72 // inefficient but only slightly. Note that this is a single
73 // database query and the number of global parameters is small.
75 globals = getMgr().getPool()->getAllGlobalParameters6(backend_selector, server_selector);
76 addGlobalsToConfig(external_cfg, globals);
77
78 // Add defaults.
79 external_cfg->applyDefaultsConfiguredGlobals(SimpleParser6::GLOBAL6_DEFAULTS);
80
81 // Sanity check it.
82 external_cfg->sanityChecksLifetime("preferred-lifetime");
83 external_cfg->sanityChecksLifetime("valid-lifetime");
84
85 // Now that we successfully fetched the new global parameters, let's
86 // remove existing ones and merge them into the current configuration.
87 cfg->clearConfiguredGlobals();
88 CfgMgr::instance().mergeIntoCurrentCfg(external_cfg->getSequence());
89 globals_fetched = true;
90 }
91
92 try {
93 // Get audit entries for deleted option definitions and delete each
94 // option definition from the current configuration for which the
95 // audit entry is found.
96 range = index.equal_range(boost::make_tuple("dhcp6_option_def",
97 AuditEntry::ModificationType::DELETE));
98 for (auto entry = range.first; entry != range.second; ++entry) {
99 cfg->getCfgOptionDef()->del((*entry)->getObjectId());
100 }
101
102 // Repeat the same for other configuration elements.
103
104 range = index.equal_range(boost::make_tuple("dhcp6_options",
105 AuditEntry::ModificationType::DELETE));
106 for (auto entry = range.first; entry != range.second; ++entry) {
107 cfg->getCfgOption()->del((*entry)->getObjectId());
108 }
109
110 range = index.equal_range(boost::make_tuple("dhcp6_client_class",
111 AuditEntry::ModificationType::DELETE));
112 for (auto entry = range.first; entry != range.second; ++entry) {
113 cfg->getClientClassDictionary()->removeClass((*entry)->getObjectId());
114 }
115
116 range = index.equal_range(boost::make_tuple("dhcp6_shared_network",
117 AuditEntry::ModificationType::DELETE));
118 for (auto entry = range.first; entry != range.second; ++entry) {
119 cfg->getCfgSharedNetworks6()->del((*entry)->getObjectId());
120 }
121
122 range = index.equal_range(boost::make_tuple("dhcp6_subnet",
123 AuditEntry::ModificationType::DELETE));
124 for (auto entry = range.first; entry != range.second; ++entry) {
125 // If the deleted subnet belongs to a shared network and the
126 // shared network is not being removed, we need to detach the
127 // subnet from the shared network.
128 auto subnet = cfg->getCfgSubnets6()->getBySubnetId((*entry)->getObjectId());
129 if (subnet) {
130 // Check if the subnet belongs to a shared network.
131 SharedNetwork6Ptr network;
132 subnet->getSharedNetwork(network);
133 if (network) {
134 // Detach the subnet from the shared network.
135 network->del(subnet->getID());
136 }
137 // Actually delete the subnet from the configuration.
138 cfg->getCfgSubnets6()->del((*entry)->getObjectId());
139 }
140 }
141
142 } catch (...) {
143 // Ignore errors thrown when attempting to delete a non-existing
144 // configuration entry. There is no guarantee that the deleted
145 // entry is actually there as we're not processing the audit
146 // chronologically.
147 }
148 }
149
150 // Create the external config into which we'll fetch backend config data.
152
153 // First let's fetch the globals and add them to external config.
154 AuditEntryCollection updated_entries;
155 if (!globals_fetched && !audit_entries.empty()) {
156 updated_entries = fetchConfigElement(audit_entries, "dhcp6_global_parameter");
157 }
158 if (!globals_fetched && (audit_entries.empty() || !updated_entries.empty())) {
160 globals = getMgr().getPool()->getModifiedGlobalParameters6(backend_selector, server_selector,
161 lb_modification_time);
162 addGlobalsToConfig(external_cfg, globals);
163 globals_fetched = true;
164 }
165
166 // Now we fetch the option definitions and add them.
167 if (!audit_entries.empty()) {
168 updated_entries = fetchConfigElement(audit_entries, "dhcp6_option_def");
169 }
170 if (audit_entries.empty() || !updated_entries.empty()) {
171 OptionDefContainer option_defs =
172 getMgr().getPool()->getModifiedOptionDefs6(backend_selector, server_selector,
173 lb_modification_time);
174 for (auto option_def = option_defs.begin(); option_def != option_defs.end(); ++option_def) {
175 if (!audit_entries.empty() && !hasObjectId(updated_entries, (*option_def)->getId())) {
176 continue;
177 }
178 external_cfg->getCfgOptionDef()->add(*option_def);
179 }
180 }
181
182 // Next fetch the options. They are returned as a container of OptionDescriptors.
183 if (!audit_entries.empty()) {
184 updated_entries = fetchConfigElement(audit_entries, "dhcp6_options");
185 }
186 if (audit_entries.empty() || !updated_entries.empty()) {
187 OptionContainer options = getMgr().getPool()->getModifiedOptions6(backend_selector,
188 server_selector,
189 lb_modification_time);
190 for (auto option = options.begin(); option != options.end(); ++option) {
191 if (!audit_entries.empty() && !hasObjectId(updated_entries, (*option).getId())) {
192 continue;
193 }
194 external_cfg->getCfgOption()->add((*option), (*option).space_name_);
195 }
196 }
197
198 // Fetch client classes. They are returned in a ClientClassDictionary.
199 if (!audit_entries.empty()) {
200 updated_entries = fetchConfigElement(audit_entries, "dhcp6_client_class");
201 }
202 if (audit_entries.empty() || !updated_entries.empty()) {
203 ClientClassDictionary client_classes = getMgr().getPool()->getAllClientClasses6(backend_selector,
204 server_selector);
205 // Match expressions are not initialized for classes returned from the config backend.
206 // We have to ensure to initialize them before they can be used by the server.
207 client_classes.initMatchExpr(AF_INET6);
208
209 // Class options also need to be created when returned from the config backend.
210 client_classes.createOptions(external_cfg->getCfgOptionDef());
211
212 external_cfg->setClientClassDictionary(boost::make_shared<ClientClassDictionary>(client_classes));
213 }
214
215 // Now fetch the shared networks.
216 if (!audit_entries.empty()) {
217 updated_entries = fetchConfigElement(audit_entries, "dhcp6_shared_network");
218 }
219 if (audit_entries.empty() || !updated_entries.empty()) {
220 SharedNetwork6Collection networks =
221 getMgr().getPool()->getModifiedSharedNetworks6(backend_selector, server_selector,
222 lb_modification_time);
223 for (auto network = networks.begin(); network != networks.end(); ++network) {
224 if (!audit_entries.empty() && !hasObjectId(updated_entries, (*network)->getId())) {
225 continue;
226 }
227 // In order to take advantage of the dynamic inheritance of global
228 // parameters to a shared network we need to set a callback function
229 // for each network to allow for fetching global parameters.
230 (*network)->setFetchGlobalsFn([] () -> ConstCfgGlobalsPtr {
231 return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
232 });
233 external_cfg->getCfgSharedNetworks6()->add((*network));
234 }
235 }
236
237 // Next we fetch subnets.
238 if (!audit_entries.empty()) {
239 updated_entries = fetchConfigElement(audit_entries, "dhcp6_subnet");
240 }
241 if (audit_entries.empty() || !updated_entries.empty()) {
242 Subnet6Collection subnets = getMgr().getPool()->getModifiedSubnets6(backend_selector,
243 server_selector,
244 lb_modification_time);
245 for (auto subnet = subnets.begin(); subnet != subnets.end(); ++subnet) {
246 if (!audit_entries.empty() && !hasObjectId(updated_entries, (*subnet)->getID())) {
247 continue;
248 }
249 // In order to take advantage of the dynamic inheritance of global
250 // parameters to a subnet we need to set a callback function for each
251 // subnet to allow for fetching global parameters.
252 (*subnet)->setFetchGlobalsFn([] () -> ConstCfgGlobalsPtr {
253 return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
254 });
255 external_cfg->getCfgSubnets6()->add((*subnet));
256 }
257 }
258
259 if (audit_entries.empty()) {
260 // If we're configuring the server after startup, we do not apply the
261 // ip-reservations-unique setting here. It will be applied when the
262 // configuration is committed.
263 auto const& cfg = CfgMgr::instance().getStagingCfg();
264 external_cfg->sanityChecksLifetime(*cfg, "preferred-lifetime");
265 external_cfg->sanityChecksLifetime(*cfg, "valid-lifetime");
266 CfgMgr::instance().mergeIntoStagingCfg(external_cfg->getSequence());
267 } else {
268 if (globals_fetched) {
269 // ip-reservations-unique parameter requires special handling because
270 // setting it to false may be unsupported by some host backends.
271 bool ip_unique = true;
272 auto ip_unique_param = external_cfg->getConfiguredGlobal("ip-reservations-unique");
273 if (ip_unique_param && (ip_unique_param->getType() == Element::boolean)) {
274 ip_unique = ip_unique_param->boolValue();
275 }
276 // First try to use the new setting to configure the HostMgr because it
277 // may fail if the backend does not support it.
278 if (!HostMgr::instance().setIPReservationsUnique(ip_unique)) {
279 // The new setting is unsupported by the backend, so do not apply this
280 // setting at all.
282 external_cfg->addConfiguredGlobal("ip-reservations-unique", Element::create(true));
283 }
284 }
285 auto const& cfg = CfgMgr::instance().getCurrentCfg();
286 external_cfg->sanityChecksLifetime(*cfg, "preferred-lifetime");
287 external_cfg->sanityChecksLifetime(*cfg, "valid-lifetime");
288 CfgMgr::instance().mergeIntoCurrentCfg(external_cfg->getSequence());
289 }
291
292 if (!audit_entries.empty() &&
293 HooksManager::calloutsPresent(hooks_.hook_index_cb6_updated_)) {
294 CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
295
296 // Use the RAII wrapper to make sure that the callout handle state is
297 // reset when this object goes out of scope. All hook points must do
298 // it to prevent possible circular dependency between the callout
299 // handle and its arguments.
300 ScopedCalloutHandleState callout_handle_state(callout_handle);
301
302 // Pass a shared pointer to audit entries.
303 AuditEntryCollectionPtr ptr(new AuditEntryCollection(audit_entries));
304 callout_handle->setArgument("audit_entries", ptr);
305
306 // Call the callouts
307 HooksManager::callCallouts(hooks_.hook_index_cb6_updated_, *callout_handle);
308
309 // Ignore the result.
310 }
311}
312
313} // end of namespace isc::dhcp
314} // end of namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:241
Config Backend selector.
Server selector for associating objects in a database with specific servers.
void addGlobalsToConfig(SrvConfigPtr external_cfg, data::StampedValueCollection &cb_globals) const
Adds globals fetched from config backend(s) to a SrvConfig instance.
Definition: cb_ctl_dhcp.h:45
virtual void databaseConfigApply(const db::BackendSelector &backend_selector, const db::ServerSelector &server_selector, const boost::posix_time::ptime &lb_modification_time, const db::AuditEntryCollection &audit_entries)
DHCPv6 server specific method to fetch and apply back end configuration into the local configuration.
Definition: cb_ctl_dhcp6.cc:45
SrvConfigPtr createExternalCfg()
Creates an external configuration and returns pointer to it.
Definition: cfgmgr.cc:177
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
void mergeIntoStagingCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the staging configuration.
Definition: cfgmgr.cc:190
void mergeIntoCurrentCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the current configuration.
Definition: cfgmgr.cc:195
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition: cfgmgr.cc:167
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition: cfgmgr.cc:161
Maintains a list of ClientClassDef's.
void createOptions(const CfgOptionDefPtr &cfg_option_def)
Iterates over the classes in the dictionary and recreates the options.
void initMatchExpr(uint16_t family)
Iterates over the classes in the dictionary and ensures that that match expressions are initialized.
static HostMgr & instance()
Returns a sole instance of the HostMgr.
Definition: host_mgr.cc:105
static const isc::data::SimpleDefaults GLOBAL6_DEFAULTS
This table defines default global values for DHCPv6.
Wrapper class around callout handle which automatically resets handle's state.
db::AuditEntryCollection fetchConfigElement(const db::AuditEntryCollection &audit_entries, const std::string &object_type) const
Returns audit entries for new or updated configuration elements of specific type to be fetched from t...
Definition: cb_ctl_base.h:262
ConfigBackendDHCPv6Mgr & getMgr() const
Returns the instance of the Config Backend Manager used by this object.
Definition: cb_ctl_base.h:312
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
boost::multi_index_container< StampedValuePtr, boost::multi_index::indexed_by< boost::multi_index::hashed_non_unique< boost::multi_index::tag< StampedValueNameIndexTag >, boost::multi_index::const_mem_fun< StampedValue, std::string, &StampedValue::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< StampedValueModificationTimeIndexTag >, boost::multi_index::const_mem_fun< BaseStampedElement, boost::posix_time::ptime, &BaseStampedElement::getModificationTime > > > > StampedValueCollection
Multi index container for StampedValue.
boost::multi_index_container< AuditEntryPtr, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::tag< AuditEntryObjectTypeTag >, boost::multi_index::composite_key< AuditEntry, boost::multi_index::const_mem_fun< AuditEntry, std::string, &AuditEntry::getObjectType >, boost::multi_index::const_mem_fun< AuditEntry, AuditEntry::ModificationType, &AuditEntry::getModificationType > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< AuditEntryModificationTimeIdTag >, boost::multi_index::composite_key< AuditEntry, boost::multi_index::const_mem_fun< AuditEntry, boost::posix_time::ptime, &AuditEntry::getModificationTime >, boost::multi_index::const_mem_fun< AuditEntry, uint64_t, &AuditEntry::getRevisionId > > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< AuditEntryObjectIdTag >, boost::multi_index::const_mem_fun< AuditEntry, uint64_t, &AuditEntry::getObjectId > > > > AuditEntryCollection
Multi index container holding AuditEntry instances.
Definition: audit_entry.h:291
boost::shared_ptr< AuditEntryCollection > AuditEntryCollectionPtr
Definition: audit_entry.h:294
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
boost::shared_ptr< const CfgGlobals > ConstCfgGlobalsPtr
Const shared pointer to a CfgGlobals instance.
Definition: cfg_globals.h:159
boost::multi_index_container< SharedNetwork6Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SharedNetworkRandomAccessIndexTag > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< SharedNetworkIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SharedNetworkNameIndexTag >, boost::multi_index::const_mem_fun< SharedNetwork6, std::string, &SharedNetwork6::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SharedNetworkModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > SharedNetwork6Collection
Multi index container holding shared networks.
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:964
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition: srv_config.h:1165
const isc::log::MessageID DHCPSRV_CFGMGR_CONFIG6_MERGED
boost::shared_ptr< SharedNetwork6 > SharedNetwork6Ptr
Pointer to SharedNetwork6 object.
boost::multi_index_container< OptionDescriptor, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_non_unique< KeyFromKeyExtractor< boost::multi_index::const_mem_fun< Option, uint16_t, &Option::getType >, boost::multi_index::member< OptionDescriptor, OptionPtr, &OptionDescriptor::option_ > > >, boost::multi_index::hashed_non_unique< boost::multi_index::member< OptionDescriptor, bool, &OptionDescriptor::persistent_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< OptionIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > > > > OptionContainer
Multi index container for DHCP option descriptors.
Definition: cfg_option.h:269
boost::multi_index_container< OptionDefinitionPtr, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_non_unique< boost::multi_index::const_mem_fun< OptionDefinition, uint16_t, &OptionDefinition::getCode > >, boost::multi_index::hashed_non_unique< boost::multi_index::const_mem_fun< OptionDefinition, std::string, &OptionDefinition::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::StampedElement::getModificationTime > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< OptionIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > > > > OptionDefContainer
Multi index container for DHCP option definitions.
const isc::log::MessageID DHCPSRV_CFGMGR_IPV6_RESERVATIONS_NON_UNIQUE_IGNORED
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
bool hasObjectId(const db::AuditEntryCollection &audit_entries, const uint64_t &object_id)
Checks if an object is in a collection od audit entries.
Definition: cb_ctl_base.h:364
Defines the logger used by the top-level component of kea-lfc.
Tag used to access index by object type.
Definition: audit_entry.h:229