Kea 2.7.5
shared_network_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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
10#include <cc/data.h>
11#include <dhcpsrv/cfgmgr.h>
12#include <dhcpsrv/cfg_option.h>
18#include <boost/make_shared.hpp>
19#include <boost/pointer_cast.hpp>
20#include <string>
21
22using namespace isc::asiolink;
23using namespace isc::data;
24using namespace isc::util;
25namespace ph = std::placeholders;
26
27namespace isc {
28namespace dhcp {
29
31 : check_iface_(check_iface) {
32}
33
36 bool encapsulate_options) {
37 SharedNetwork4Ptr shared_network;
38 try {
39
40 // Check parameters.
42 shared_network_data);
43
44 // Make sure that the network name has been specified. The name is required
45 // to create a SharedNetwork4 object.
46 std::string name = getString(shared_network_data, "name");
47 shared_network.reset(new SharedNetwork4(name));
48
49 ElementPtr mutable_params;
50 mutable_params = boost::const_pointer_cast<Element>(shared_network_data);
51
52 // Parse parameters common to all Network derivations.
53 NetworkPtr network = boost::dynamic_pointer_cast<Network>(shared_network);
54 parseCommon(mutable_params, network);
55
56 // interface is an optional parameter
57 if (shared_network_data->contains("interface")) {
58 std::string iface = getString(shared_network_data, "interface");
59 if (!iface.empty()) {
60 if (check_iface_ && !IfaceMgr::instance().getIface(iface)) {
62 shared_network_data->get("interface");
64 "Specified network interface name " << iface
65 << " for shared network " << name
66 << " is not present in the system ("
67 << error->getPosition() << ")");
68 }
69 shared_network->setIface(iface);
70 }
71 }
72
73 if (shared_network_data->contains("option-data")) {
74 auto json = shared_network_data->get("option-data");
75 // Create parser instance for option-data.
76 CfgOptionPtr cfg_option = shared_network->getCfgOption();
77 auto parser = createOptionDataListParser();
78 parser->parse(cfg_option, json, encapsulate_options);
79 }
80
81 if (shared_network_data->contains("subnet4")) {
82 auto json = shared_network_data->get("subnet4");
83
84 // Create parser instance of subnet4.
85 auto parser = createSubnetsListParser();
86 Subnet4Collection subnets;
87 parser->parse(subnets, json);
88
89 // Add all returned subnets into shared network.
90 for (auto const& subnet : subnets) {
91 shared_network->add(subnet);
92 }
93 }
94
95 if (shared_network_data->contains("match-client-id")) {
96 shared_network->setMatchClientId(getBoolean(shared_network_data,
97 "match-client-id"));
98 }
99
100 if (shared_network_data->contains("authoritative")) {
101 shared_network->setAuthoritative(getBoolean(shared_network_data,
102 "authoritative"));
103 }
104
105 // Set next-server
106 if (shared_network_data->contains("next-server")) {
107 std::string next_server;
108 try {
109 next_server = getString(shared_network_data, "next-server");
110 if (!next_server.empty()) {
111 shared_network->setSiaddr(IOAddress(next_server));
112 }
113 } catch (...) {
114 ConstElementPtr next = shared_network_data->get("next-server");
115 std::string pos;
116 if (next) {
117 pos = next->getPosition().str();
118 } else {
119 pos = shared_network_data->getPosition().str();
120 }
121 isc_throw(DhcpConfigError, "invalid parameter next-server : "
122 << next_server << "(" << pos << ")");
123 }
124 }
125
126 // Set server-hostname.
127 if (shared_network_data->contains("server-hostname")) {
128 std::string sname = getString(shared_network_data, "server-hostname");
129 if (!sname.empty()) {
130 if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
131 ConstElementPtr error = shared_network_data->get("server-hostname");
132 isc_throw(DhcpConfigError, "server-hostname must be at most "
133 << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
134 << sname.length() << " ("
135 << error->getPosition() << ")");
136 }
137 shared_network->setSname(sname);
138 }
139 }
140
141 // Set boot-file-name.
142 if (shared_network_data->contains("boot-file-name")) {
143 std::string filename = getString(shared_network_data, "boot-file-name");
144 if (!filename.empty()) {
145 if (filename.length() > Pkt4::MAX_FILE_LEN) {
146 ConstElementPtr error = shared_network_data->get("boot-file-name");
147 isc_throw(DhcpConfigError, "boot-file-name must be at most "
148 << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
149 << filename.length() << " ("
150 << error->getPosition() << ")");
151 }
152 shared_network->setFilename(filename);
153 }
154 }
155
156 if (shared_network_data->contains("client-class")) {
157 std::string client_class = getString(shared_network_data, "client-class");
158 if (!client_class.empty()) {
159 shared_network->allowClientClass(client_class);
160 }
161 }
162
163 ConstElementPtr user_context = shared_network_data->get("user-context");
164 if (user_context) {
165 shared_network->setContext(user_context);
166 }
167
168 // Setup additional class list.
169 getAdditionalClassesElem(shared_network_data,
171 shared_network, ph::_1));
172
173 if (shared_network_data->contains("relay")) {
174 auto relay_parms = shared_network_data->get("relay");
175 if (relay_parms) {
178 parser.parse(relay_info, relay_parms);
179 shared_network->setRelayInfo(*relay_info);
180 }
181 }
182
183 parseTeePercents(shared_network_data, network);
184
185 // Parse DDNS parameters
186 parseDdnsParams(shared_network_data, network);
187
188 // Parse lease cache parameters
189 parseCacheParams(shared_network_data, network);
190
191 // Parse allocator params.
192 parseAllocatorParams(shared_network_data, network);
193
194 // Parse offer-lifetime parameter.
195 Network4Ptr network4 = boost::dynamic_pointer_cast<Network4>(shared_network);
196 parseOfferLft(shared_network_data, network4);
197
198 } catch (const std::exception& ex) {
199 isc_throw(DhcpConfigError, ex.what() << " ("
200 << shared_network_data->getPosition() << ")");
201 }
202
203 // In order to take advantage of the dynamic inheritance of global
204 // parameters to a shared network we need to set a callback function
205 // for each shared network to allow for fetching global parameters.
206 shared_network->setFetchGlobalsFn([]() -> ConstCfgGlobalsPtr {
207 return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
208 });
209
210 return (shared_network);
211}
212
213boost::shared_ptr<OptionDataListParser>
215 auto parser = boost::make_shared<OptionDataListParser>(AF_INET);
216 return (parser);
217}
218
219boost::shared_ptr<Subnets4ListConfigParser>
221 auto parser = boost::make_shared<Subnets4ListConfigParser>(check_iface_);
222 return (parser);
223}
224
226 : check_iface_(check_iface) {
227}
228
231 bool encapsulate_options) {
232 SharedNetwork6Ptr shared_network;
233 std::string name;
234 try {
235 // Check parameters.
237 shared_network_data);
238
239 // Make sure that the network name has been specified. The name is required
240 // to create a SharedNetwork6 object.
241 name = getString(shared_network_data, "name");
242 shared_network.reset(new SharedNetwork6(name));
243
244 ElementPtr mutable_params;
245 mutable_params = boost::const_pointer_cast<Element>(shared_network_data);
246
247 // Parse parameters common to all Network derivations.
248 NetworkPtr network = boost::dynamic_pointer_cast<Network>(shared_network);
249 parseCommon(mutable_params, network);
250
251 // preferred-lifetime
252 shared_network->setPreferred(parseIntTriplet(shared_network_data,
253 "preferred-lifetime"));
254
255 // Get interface-id option content. For now we support string
256 // representation only
257 Optional<std::string> ifaceid;
258 if (shared_network_data->contains("interface-id")) {
259 ifaceid = getString(shared_network_data, "interface-id");
260 }
261
262 // Interface is an optional parameter
263 Optional<std::string> iface;
264 if (shared_network_data->contains("interface")) {
265 iface = getString(shared_network_data, "interface");
266 }
267
268 // Specifying both interface for locally reachable subnets and
269 // interface id for relays is mutually exclusive. Need to test for
270 // this condition.
271 if (!ifaceid.unspecified() && !iface.unspecified() && !ifaceid.empty() &&
272 !iface.empty()) {
274 "parser error: interface (defined for locally reachable "
275 "subnets) and interface-id (defined for subnets reachable"
276 " via relays) cannot be defined at the same time for "
277 "shared network " << name << "("
278 << shared_network_data->getPosition() << ")");
279 }
280
281 // Configure interface-id for remote interfaces, if defined
282 if (!ifaceid.unspecified() && !ifaceid.empty()) {
283 std::string ifaceid_value = ifaceid.get();
284 OptionBuffer tmp(ifaceid_value.begin(), ifaceid_value.end());
286 shared_network->setInterfaceId(opt);
287 }
288
289 // Set interface name. If it is defined, then subnets are available
290 // directly over specified network interface.
291 if (!iface.unspecified() && !iface.empty()) {
292 if (check_iface_ && !IfaceMgr::instance().getIface(iface)) {
293 ConstElementPtr error = shared_network_data->get("interface");
295 "Specified network interface name " << iface
296 << " for shared network " << name
297 << " is not present in the system ("
298 << error->getPosition() << ")");
299 }
300 shared_network->setIface(iface);
301 }
302
303 if (shared_network_data->contains("rapid-commit")) {
304 shared_network->setRapidCommit(getBoolean(shared_network_data,
305 "rapid-commit"));
306 }
307
308 if (shared_network_data->contains("option-data")) {
309 auto json = shared_network_data->get("option-data");
310 // Create parser instance for option-data.
311 CfgOptionPtr cfg_option = shared_network->getCfgOption();
312 auto parser = createOptionDataListParser();
313 parser->parse(cfg_option, json, encapsulate_options);
314 }
315
316 if (shared_network_data->contains("client-class")) {
317 std::string client_class = getString(shared_network_data, "client-class");
318 if (!client_class.empty()) {
319 shared_network->allowClientClass(client_class);
320 }
321 }
322
323 ConstElementPtr user_context = shared_network_data->get("user-context");
324 if (user_context) {
325 shared_network->setContext(user_context);
326 }
327
328 // Setup additional class list.
329 getAdditionalClassesElem(shared_network_data,
331 shared_network, ph::_1));
332
333 if (shared_network_data->contains("subnet6")) {
334 auto json = shared_network_data->get("subnet6");
335
336 // Create parser instance of subnet6.
337 auto parser = createSubnetsListParser();
338 Subnet6Collection subnets;
339 parser->parse(subnets, json);
340
341 // Add all returned subnets into shared network.
342 for (auto const& subnet : subnets) {
343 shared_network->add(subnet);
344 }
345 }
346
347 if (shared_network_data->contains("relay")) {
348 auto relay_parms = shared_network_data->get("relay");
349 if (relay_parms) {
352 parser.parse(relay_info, relay_parms);
353 shared_network->setRelayInfo(*relay_info);
354 }
355 }
356
357 parseTeePercents(shared_network_data, network);
358
359 // Parse DDNS parameters
360 parseDdnsParams(shared_network_data, network);
361
362 // Parse lease cache parameters
363 parseCacheParams(shared_network_data, network);
364
365 // Parse allocator params.
366 parseAllocatorParams(shared_network_data, network);
367 if (network->getAllocatorType() == "flq") {
368 isc_throw(BadValue, "Free Lease Queue allocator is not supported for IPv6 address pools");
369 }
370
371 // Parse prefix delegation allocator params.
372 auto network6 = boost::dynamic_pointer_cast<Network6>(shared_network);
373 parsePdAllocatorParams(shared_network_data, network6);
374
375 } catch (const std::exception& ex) {
376 isc_throw(DhcpConfigError, ex.what() << " ("
377 << shared_network_data->getPosition() << ")");
378 }
379
380 // In order to take advantage of the dynamic inheritance of global
381 // parameters to a shared network we need to set a callback function
382 // for each shared network which can be used to fetch global parameters.
383 shared_network->setFetchGlobalsFn([]() -> ConstCfgGlobalsPtr {
384 return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
385 });
386
387 return (shared_network);
388}
389
390boost::shared_ptr<OptionDataListParser>
392 auto parser = boost::make_shared<OptionDataListParser>(AF_INET6);
393 return (parser);
394}
395
396boost::shared_ptr<Subnets6ListConfigParser>
398 auto parser = boost::make_shared<Subnets6ListConfigParser>(check_iface_);
399 return (parser);
400}
401
402} // end of namespace isc::dhcp
403} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static void checkKeywords(const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
Checks acceptable keywords with their expected type.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
const isc::util::Triplet< uint32_t > parseIntTriplet(const data::ConstElementPtr &scope, const std::string &name)
Parses an integer triplet.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
void parseCacheParams(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters related to lease cache settings.
void parseAllocatorParams(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters pertaining to allocator selection.
void parseDdnsParams(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters pertaining to DDNS behavior.
void parseCommon(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses common parameters.
static void getAdditionalClassesElem(data::ConstElementPtr params, ClassAdderFunc adder_func)
Fetches the element for either 'evaluate-additional-classes' or deprecated 'require-client-classes'.
void parseTeePercents(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters related to "percent" timers settings.
void parsePdAllocatorParams(const data::ConstElementPtr &network_data, Network6Ptr &network)
Parses parameters pertaining to prefix delegation allocator selection.
void parseOfferLft(const data::ConstElementPtr &network_data, Network4Ptr &network)
Parses offer-lifetime parameter (v4 only)
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:25
To be removed. Please use ConfigError instead.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:54
Holds optional information about relay.
Definition network.h:162
boost::shared_ptr< Network::RelayInfo > RelayInfoPtr
Pointer to the RelayInfo structure.
Definition network.h:210
void addAdditionalClass(const isc::dhcp::ClientClass &class_name)
Adds class class_name to the additional classes list.
Definition network.cc:85
static const size_t MAX_SNAME_LEN
length of the SNAME field in DHCPv4 message
Definition pkt4.h:44
static const size_t MAX_FILE_LEN
length of the FILE field in DHCPv4 message
Definition pkt4.h:47
parser for additional relay information
void parse(const isc::dhcp::Network::RelayInfoPtr &relay_info, isc::data::ConstElementPtr relay_elem)
parses the actual relay parameters
virtual boost::shared_ptr< Subnets4ListConfigParser > createSubnetsListParser() const
Returns an instance of the Subnets4ListConfigParser to be used for parsing the subnets within the sha...
SharedNetwork4Parser(bool check_iface=true)
Constructor.
virtual boost::shared_ptr< OptionDataListParser > createOptionDataListParser() const
Returns an instance of the OptionDataListParser to be used in parsing the option-data structure.
bool check_iface_
Check if the specified interface exists in the system.
SharedNetwork4Ptr parse(const data::ConstElementPtr &shared_network_data, bool encapsulate_options=true)
Parses shared configuration information for IPv4 shared network.
Shared network holding IPv4 subnets.
SharedNetwork6Parser(bool check_iface=true)
Constructor.
virtual boost::shared_ptr< Subnets6ListConfigParser > createSubnetsListParser() const
Returns an instance of the Subnets6ListConfigParser to be used for parsing the subnets within the sha...
bool check_iface_
Check if the specified interface exists in the system.
virtual boost::shared_ptr< OptionDataListParser > createOptionDataListParser() const
Returns an instance of the OptionDataListParser to be used in parsing the option-data structure.
SharedNetwork6Ptr parse(const data::ConstElementPtr &shared_network_data, bool encapsulate_options=true)
Parses shared configuration information for IPv6 shared network.
Shared network holding IPv6 subnets.
static const isc::data::SimpleKeywords SHARED_NETWORK4_PARAMETERS
This table defines all shared network parameters for DHCPv4.
static const isc::data::SimpleKeywords SHARED_NETWORK6_PARAMETERS
This table defines all shared network parameters for DHCPv6.
@ D6O_INTERFACE_ID
Definition dhcp6.h:38
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
@ error
Definition db_log.h:118
boost::shared_ptr< const CfgGlobals > ConstCfgGlobalsPtr
Const shared pointer to a CfgGlobals instance.
boost::shared_ptr< Network4 > Network4Ptr
Pointer to the Network4 object.
Definition network.h:1419
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition cfg_option.h:832
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:937
boost::shared_ptr< SharedNetwork6 > SharedNetwork6Ptr
Pointer to SharedNetwork6 object.
boost::multi_index_container< Subnet4Ptr, 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< SubnetServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > >, 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 > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition subnet.h:866
boost::shared_ptr< Network > NetworkPtr
Pointer to the Network object.
Definition network.h:73
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
boost::shared_ptr< SharedNetwork4 > SharedNetwork4Ptr
Pointer to SharedNetwork4 object.
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
Defines the logger used by the top-level component of kea-lfc.