Kea 2.5.9
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;
25
26namespace isc {
27namespace dhcp {
28
30 : check_iface_(check_iface) {
31}
32
35 bool encapsulate_options) {
36 SharedNetwork4Ptr shared_network;
37 try {
38
39 // Check parameters.
41 shared_network_data);
42
43 // Make sure that the network name has been specified. The name is required
44 // to create a SharedNetwork4 object.
45 std::string name = getString(shared_network_data, "name");
46 shared_network.reset(new SharedNetwork4(name));
47
48 // Move from reservation mode to new reservations flags.
49 ElementPtr mutable_params;
50 mutable_params = boost::const_pointer_cast<Element>(shared_network_data);
52
53 // Parse parameters common to all Network derivations.
54 NetworkPtr network = boost::dynamic_pointer_cast<Network>(shared_network);
55 parseCommon(mutable_params, network);
56
57 // interface is an optional parameter
58 if (shared_network_data->contains("interface")) {
59 std::string iface = getString(shared_network_data, "interface");
60 if (!iface.empty()) {
61 if (check_iface_ && !IfaceMgr::instance().getIface(iface)) {
63 shared_network_data->get("interface");
65 "Specified network interface name " << iface
66 << " for shared network " << name
67 << " is not present in the system ("
68 << error->getPosition() << ")");
69 }
70 shared_network->setIface(iface);
71 }
72 }
73
74 if (shared_network_data->contains("option-data")) {
75 auto json = shared_network_data->get("option-data");
76 // Create parser instance for option-data.
77 CfgOptionPtr cfg_option = shared_network->getCfgOption();
78 auto parser = createOptionDataListParser();
79 parser->parse(cfg_option, json, encapsulate_options);
80 }
81
82 if (shared_network_data->contains("subnet4")) {
83 auto json = shared_network_data->get("subnet4");
84
85 // Create parser instance of subnet4.
86 auto parser = createSubnetsListParser();
87 Subnet4Collection subnets;
88 parser->parse(subnets, json);
89
90 // Add all returned subnets into shared network.
91 for (auto const& subnet : subnets) {
92 shared_network->add(subnet);
93 }
94 }
95
96 if (shared_network_data->contains("match-client-id")) {
97 shared_network->setMatchClientId(getBoolean(shared_network_data,
98 "match-client-id"));
99 }
100
101 if (shared_network_data->contains("authoritative")) {
102 shared_network->setAuthoritative(getBoolean(shared_network_data,
103 "authoritative"));
104 }
105
106 // Set next-server
107 if (shared_network_data->contains("next-server")) {
108 std::string next_server;
109 try {
110 next_server = getString(shared_network_data, "next-server");
111 if (!next_server.empty()) {
112 shared_network->setSiaddr(IOAddress(next_server));
113 }
114 } catch (...) {
115 ConstElementPtr next = shared_network_data->get("next-server");
116 std::string pos;
117 if (next) {
118 pos = next->getPosition().str();
119 } else {
120 pos = shared_network_data->getPosition().str();
121 }
122 isc_throw(DhcpConfigError, "invalid parameter next-server : "
123 << next_server << "(" << pos << ")");
124 }
125 }
126
127 // Set server-hostname.
128 if (shared_network_data->contains("server-hostname")) {
129 std::string sname = getString(shared_network_data, "server-hostname");
130 if (!sname.empty()) {
131 if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
132 ConstElementPtr error = shared_network_data->get("server-hostname");
133 isc_throw(DhcpConfigError, "server-hostname must be at most "
134 << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
135 << sname.length() << " ("
136 << error->getPosition() << ")");
137 }
138 shared_network->setSname(sname);
139 }
140 }
141
142 // Set boot-file-name.
143 if (shared_network_data->contains("boot-file-name")) {
144 std::string filename = getString(shared_network_data, "boot-file-name");
145 if (!filename.empty()) {
146 if (filename.length() > Pkt4::MAX_FILE_LEN) {
147 ConstElementPtr error = shared_network_data->get("boot-file-name");
148 isc_throw(DhcpConfigError, "boot-file-name must be at most "
149 << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
150 << filename.length() << " ("
151 << error->getPosition() << ")");
152 }
153 shared_network->setFilename(filename);
154 }
155 }
156
157 if (shared_network_data->contains("client-class")) {
158 std::string client_class = getString(shared_network_data, "client-class");
159 if (!client_class.empty()) {
160 shared_network->allowClientClass(client_class);
161 }
162 }
163
164 ConstElementPtr user_context = shared_network_data->get("user-context");
165 if (user_context) {
166 shared_network->setContext(user_context);
167 }
168
169 if (shared_network_data->contains("require-client-classes")) {
170 const std::vector<data::ElementPtr>& class_list =
171 shared_network_data->get("require-client-classes")->listValue();
172 for (auto const& cclass : class_list) {
173 if ((cclass->getType() != Element::string) ||
174 cclass->stringValue().empty()) {
175 isc_throw(DhcpConfigError, "invalid class name ("
176 << cclass->getPosition() << ")");
177 }
178 shared_network->requireClientClass(cclass->stringValue());
179 }
180 }
181
182 if (shared_network_data->contains("relay")) {
183 auto relay_parms = shared_network_data->get("relay");
184 if (relay_parms) {
187 parser.parse(relay_info, relay_parms);
188 shared_network->setRelayInfo(*relay_info);
189 }
190 }
191
192 parseTeePercents(shared_network_data, network);
193
194 // Parse DDNS parameters
195 parseDdnsParams(shared_network_data, network);
196
197 // Parse lease cache parameters
198 parseCacheParams(shared_network_data, network);
199
200 // Parse allocator params.
201 parseAllocatorParams(shared_network_data, network);
202
203 // Parse offer-lifetime parameter.
204 Network4Ptr network4 = boost::dynamic_pointer_cast<Network4>(shared_network);
205 parseOfferLft(shared_network_data, network4);
206
207 } catch (const DhcpConfigError&) {
208 // Position was already added
209 throw;
210 } catch (const std::exception& ex) {
211 isc_throw(DhcpConfigError, ex.what() << " ("
212 << shared_network_data->getPosition() << ")");
213 }
214
215 // In order to take advantage of the dynamic inheritance of global
216 // parameters to a shared network we need to set a callback function
217 // for each shared network to allow for fetching global parameters.
218 shared_network->setFetchGlobalsFn([]() -> ConstCfgGlobalsPtr {
219 return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
220 });
221
222 return (shared_network);
223}
224
225boost::shared_ptr<OptionDataListParser>
227 auto parser = boost::make_shared<OptionDataListParser>(AF_INET);
228 return (parser);
229}
230
231boost::shared_ptr<Subnets4ListConfigParser>
233 auto parser = boost::make_shared<Subnets4ListConfigParser>(check_iface_);
234 return (parser);
235}
236
238 : check_iface_(check_iface) {
239}
240
243 bool encapsulate_options) {
244 SharedNetwork6Ptr shared_network;
245 std::string name;
246 try {
247 // Check parameters.
249 shared_network_data);
250
251 // Make sure that the network name has been specified. The name is required
252 // to create a SharedNetwork6 object.
253 std::string name = getString(shared_network_data, "name");
254 shared_network.reset(new SharedNetwork6(name));
255
256 // Move from reservation mode to new reservations flags.
257 ElementPtr mutable_params;
258 mutable_params = boost::const_pointer_cast<Element>(shared_network_data);
260
261 // Parse parameters common to all Network derivations.
262 NetworkPtr network = boost::dynamic_pointer_cast<Network>(shared_network);
263 parseCommon(mutable_params, network);
264
265 // preferred-lifetime
266 shared_network->setPreferred(parseIntTriplet(shared_network_data,
267 "preferred-lifetime"));
268
269 // Get interface-id option content. For now we support string
270 // representation only
271 Optional<std::string> ifaceid;
272 if (shared_network_data->contains("interface-id")) {
273 ifaceid = getString(shared_network_data, "interface-id");
274 }
275
276 // Interface is an optional parameter
278 if (shared_network_data->contains("interface")) {
279 iface = getString(shared_network_data, "interface");
280 }
281
282 // Specifying both interface for locally reachable subnets and
283 // interface id for relays is mutually exclusive. Need to test for
284 // this condition.
285 if (!ifaceid.unspecified() && !iface.unspecified() && !ifaceid.empty() &&
286 !iface.empty()) {
288 "parser error: interface (defined for locally reachable "
289 "subnets) and interface-id (defined for subnets reachable"
290 " via relays) cannot be defined at the same time for "
291 "shared network " << name << "("
292 << shared_network_data->getPosition() << ")");
293 }
294
295 // Configure interface-id for remote interfaces, if defined
296 if (!ifaceid.unspecified() && !ifaceid.empty()) {
297 std::string ifaceid_value = ifaceid.get();
298 OptionBuffer tmp(ifaceid_value.begin(), ifaceid_value.end());
300 shared_network->setInterfaceId(opt);
301 }
302
303 // Set interface name. If it is defined, then subnets are available
304 // directly over specified network interface.
305 if (!iface.unspecified() && !iface.empty()) {
306 if (check_iface_ && !IfaceMgr::instance().getIface(iface)) {
307 ConstElementPtr error = shared_network_data->get("interface");
309 "Specified network interface name " << iface
310 << " for shared network " << name
311 << " is not present in the system ("
312 << error->getPosition() << ")");
313 }
314 shared_network->setIface(iface);
315 }
316
317 if (shared_network_data->contains("rapid-commit")) {
318 shared_network->setRapidCommit(getBoolean(shared_network_data,
319 "rapid-commit"));
320 }
321
322 if (shared_network_data->contains("option-data")) {
323 auto json = shared_network_data->get("option-data");
324 // Create parser instance for option-data.
325 CfgOptionPtr cfg_option = shared_network->getCfgOption();
326 auto parser = createOptionDataListParser();
327 parser->parse(cfg_option, json, encapsulate_options);
328 }
329
330 if (shared_network_data->contains("client-class")) {
331 std::string client_class = getString(shared_network_data, "client-class");
332 if (!client_class.empty()) {
333 shared_network->allowClientClass(client_class);
334 }
335 }
336
337 ConstElementPtr user_context = shared_network_data->get("user-context");
338 if (user_context) {
339 shared_network->setContext(user_context);
340 }
341
342 if (shared_network_data->contains("require-client-classes")) {
343 const std::vector<data::ElementPtr>& class_list =
344 shared_network_data->get("require-client-classes")->listValue();
345 for (auto const& cclass : class_list) {
346 if ((cclass->getType() != Element::string) ||
347 cclass->stringValue().empty()) {
348 isc_throw(DhcpConfigError, "invalid class name ("
349 << cclass->getPosition() << ")");
350 }
351 shared_network->requireClientClass(cclass->stringValue());
352 }
353 }
354
355 if (shared_network_data->contains("subnet6")) {
356 auto json = shared_network_data->get("subnet6");
357
358 // Create parser instance of subnet6.
359 auto parser = createSubnetsListParser();
360 Subnet6Collection subnets;
361 parser->parse(subnets, json);
362
363 // Add all returned subnets into shared network.
364 for (auto const& subnet : subnets) {
365 shared_network->add(subnet);
366 }
367 }
368
369 if (shared_network_data->contains("relay")) {
370 auto relay_parms = shared_network_data->get("relay");
371 if (relay_parms) {
374 parser.parse(relay_info, relay_parms);
375 shared_network->setRelayInfo(*relay_info);
376 }
377 }
378
379 parseTeePercents(shared_network_data, network);
380
381 // Parse DDNS parameters
382 parseDdnsParams(shared_network_data, network);
383
384 // Parse lease cache parameters
385 parseCacheParams(shared_network_data, network);
386
387 // Parse allocator params.
388 parseAllocatorParams(shared_network_data, network);
389 if (network->getAllocatorType() == "flq") {
390 isc_throw(BadValue, "Free Lease Queue allocator is not supported for IPv6 address pools");
391 }
392
393 // Parse prefix delegation allocator params.
394 auto network6 = boost::dynamic_pointer_cast<Network6>(shared_network);
395 parsePdAllocatorParams(shared_network_data, network6);
396
397 } catch (const std::exception& ex) {
398 isc_throw(DhcpConfigError, ex.what() << " ("
399 << shared_network_data->getPosition() << ")");
400 }
401
402 // In order to take advantage of the dynamic inheritance of global
403 // parameters to a shared network we need to set a callback function
404 // for each shared network which can be used to fetch global parameters.
405 shared_network->setFetchGlobalsFn([]() -> ConstCfgGlobalsPtr {
406 return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
407 });
408
409 return (shared_network);
410}
411
412boost::shared_ptr<OptionDataListParser>
414 auto parser = boost::make_shared<OptionDataListParser>(AF_INET6);
415 return (parser);
416}
417
418boost::shared_ptr<Subnets6ListConfigParser>
420 auto parser = boost::make_shared<Subnets6ListConfigParser>(check_iface_);
421 return (parser);
422}
423
424} // end of namespace isc::dhcp
425} // 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 moveReservationMode(isc::data::ElementPtr config)
Moves deprecated reservation-mode parameter to new reservations flags.
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
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
Definition: dhcp_parsers.h:449
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.
A template representing an optional value.
Definition: optional.h:36
T get() const
Retrieves the encapsulated value.
Definition: optional.h:114
bool empty() const
Checks if the encapsulated value is empty.
Definition: optional.h:153
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition: optional.h:136
@ 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.
Definition: cfg_globals.h:172
boost::shared_ptr< Network4 > Network4Ptr
Pointer to the Network4 object.
Definition: network.h:1413
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:803
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:934
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:863
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.