simple_parser6.cc 20.5 KB
Newer Older
1
// Copyright (C) 2016-2019 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6
//
// 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/.

7 8
#include <config.h>

9
#include <cc/data.h>
10
#include <dhcpsrv/parsers/simple_parser6.h>
11 12 13 14 15 16
#include <boost/foreach.hpp>

using namespace isc::data;

namespace isc {
namespace dhcp {
17 18 19 20 21 22 23 24 25
/// @brief This sets of arrays define the default values and
///        values inherited (derived) between various scopes.
///
/// Each of those is documented in @file simple_parser6.cc. This
/// is different than most other comments in Kea code. The reason
/// for placing those in .cc rather than .h file is that it
/// is expected to be one centralized place to look at for
/// the default values. This is expected to be looked at also by
/// people who are not skilled in C or C++, so they may be
26
/// confused with the differences between declaration and definition.
27 28 29 30
/// As such, there's one file to look at that hopefully is readable
/// without any C or C++ skills.
///
/// @{
31

32 33 34 35 36 37
/// @brief This table defines all global parameters in DHCPv6.
///
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows global_param rule in bison grammar.
const SimpleKeywords SimpleParser6::GLOBAL6_PARAMETERS = {
38
    { "data-directory",               Element::string },
39
    { "preferred-lifetime",           Element::integer },
40 41
    { "min-preferred-lifetime",       Element::integer },
    { "max-preferred-lifetime",       Element::integer },
42
    { "valid-lifetime",               Element::integer },
43 44
    { "min-valid-lifetime",           Element::integer },
    { "max-valid-lifetime",           Element::integer },
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
    { "renew-timer",                  Element::integer },
    { "rebind-timer",                 Element::integer },
    { "decline-probation-period",     Element::integer },
    { "subnet6",                      Element::list },
    { "shared-networks",              Element::list },
    { "interfaces-config",            Element::map },
    { "lease-database",               Element::map },
    { "hosts-database",               Element::map },
    { "hosts-databases",              Element::list },
    { "mac-sources",                  Element::list },
    { "relay-supplied-options",       Element::list },
    { "host-reservation-identifiers", Element::list },
    { "client-classes",               Element::list },
    { "option-def",                   Element::list },
    { "option-data",                  Element::list },
    { "hooks-libraries",              Element::list },
    { "expired-leases-processing",    Element::map },
    { "server-id",                    Element::map },
    { "dhcp4o6-port",                 Element::integer },
    { "control-socket",               Element::map },
    { "dhcp-queue-control",           Element::map },
    { "dhcp-ddns",                    Element::map },
    { "user-context",                 Element::map },
    { "comment",                      Element::string },
    { "sanity-checks",                Element::map },
    { "reservations",                 Element::list },
    { "config-control",               Element::map },
    { "server-tag",                   Element::string },
73 74 75
    { "reservation-mode",             Element::string },
    { "calculate-tee-times",          Element::boolean },
    { "t1-percent",                   Element::real },
76
    { "t2-percent",                   Element::real },
77 78
    { "loggers",                      Element::list },
    { "hostname-char-set",            Element::string },
79 80 81 82 83 84 85
    { "hostname-char-replacement",    Element::string },
    { "ddns-send-updates",            Element::boolean },
    { "ddns-override-no-update",      Element::boolean },
    { "ddns-override-client-update",  Element::boolean },
    { "ddns-replace-client-name",     Element::string },
    { "ddns-generated-prefix",        Element::string },
    { "ddns-qualifying-suffix",       Element::string }
86 87
};

88 89 90 91 92 93
/// @brief This table defines default global values for DHCPv6
///
/// Some of the global parameters defined in the global scope (i.e. directly
/// in Dhcp6) are optional. If not defined, the following values will be
/// used.
const SimpleDefaults SimpleParser6::GLOBAL6_DEFAULTS = {
94 95 96 97 98 99 100 101 102
    { "preferred-lifetime",             Element::integer, "3600" },
    { "valid-lifetime",                 Element::integer, "7200" },
    { "decline-probation-period",       Element::integer, "86400" }, // 24h
    { "dhcp4o6-port",                   Element::integer, "0" },
    { "server-tag",                     Element::string,  "" },
    { "reservation-mode",               Element::string,  "all" },
    { "calculate-tee-times",            Element::boolean, "true" },
    { "t1-percent",                     Element::real,    ".50" },
    { "t2-percent",                     Element::real,    ".80" },
103
    { "ddns-send-updates",              Element::boolean, "true" },
104 105 106 107 108
    { "ddns-override-no-update",        Element::boolean, "false" },
    { "ddns-override-client-update",    Element::boolean, "false" },
    { "ddns-replace-client-name",       Element::string, "never" },
    { "ddns-generated-prefix",          Element::string, "myhost" },
    // TKM should this still be true? qualifying-suffix has no default ??
109
    { "ddns-qualifying-suffix",         Element::string, "" }
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
};

/// @brief This table defines all option definition parameters.
///
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows option_def_param rules in bison grammar.
const SimpleKeywords SimpleParser6::OPTION6_DEF_PARAMETERS = {
    { "name",         Element::string },
    { "code",         Element::integer },
    { "type",         Element::string },
    { "record-types", Element::string },
    { "space",        Element::string },
    { "encapsulate",  Element::string },
    { "array",        Element::boolean, },
    { "user-context", Element::map },
    { "comment",      Element::string },
    { "metadata",     Element::map }
};

130 131 132 133
/// @brief This table defines default values for option definitions in DHCPv6.
///
/// Dhcp6 may contain an array called option-def that enumerates new option
/// definitions. This array lists default values for those option definitions.
134 135 136 137 138 139 140
const SimpleDefaults SimpleParser6::OPTION6_DEF_DEFAULTS = {
    { "record-types", Element::string,  ""},
    { "space",        Element::string,  "dhcp6"},
    { "array",        Element::boolean, "false"},
    { "encapsulate",  Element::string,  "" }
};

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/// @brief This table defines all option parameters.
///
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows option_param rules in bison grammar.
const SimpleKeywords SimpleParser6::OPTION6_PARAMETERS = {
    { "name",         Element::string },
    { "data",         Element::string },
    { "code",         Element::integer },
    { "space",        Element::string },
    { "csv-format",   Element::boolean },
    { "always-send",  Element::boolean },
    { "user-context", Element::map },
    { "comment",      Element::string },
    { "metadata",     Element::map }
};

158 159 160 161 162
/// @brief This table defines default values for options in DHCPv6.
///
/// Dhcp6 usually contains option values (option-data) defined in global,
/// subnet, class or host reservations scopes. This array lists default values
/// for those option-data declarations.
163 164
const SimpleDefaults SimpleParser6::OPTION6_DEFAULTS = {
    { "space",        Element::string,  "dhcp6"},
165
    { "csv-format",   Element::boolean, "true"},
166
    { "always-send",  Element::boolean, "false"}
167 168
};

169
/// @brief This table defines all subnet parameters for DHCPv6.
170
///
171 172 173 174
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows subnet6_param rule in bison grammar.
const SimpleKeywords SimpleParser6::SUBNET6_PARAMETERS = {
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
    { "preferred-lifetime",             Element::integer },
    { "min-preferred-lifetime",         Element::integer },
    { "max-preferred-lifetime",         Element::integer },
    { "valid-lifetime",                 Element::integer },
    { "min-valid-lifetime",             Element::integer },
    { "max-valid-lifetime",             Element::integer },
    { "renew-timer",                    Element::integer },
    { "rebind-timer",                   Element::integer },
    { "option-data",                    Element::list },
    { "pools",                          Element::list },
    { "pd-pools",                       Element::list },
    { "subnet",                         Element::string },
    { "interface",                      Element::string },
    { "interface-id",                   Element::string },
    { "id",                             Element::integer },
    { "rapid-commit",                   Element::boolean },
    { "client-class",                   Element::string },
    { "require-client-classes",         Element::list },
    { "reservations",                   Element::list },
    { "reservation-mode",               Element::string },
    { "relay",                          Element::map },
    { "user-context",                   Element::map },
    { "comment",                        Element::string },
    { "calculate-tee-times",            Element::boolean },
    { "t1-percent",                     Element::real },
    { "t2-percent",                     Element::real },
    { "ddns-send-updates",              Element::boolean },
    { "ddns-override-no-update",        Element::boolean },
    { "ddns-override-client-update",    Element::boolean },
    { "ddns-replace-client-name",       Element::string },
    { "ddns-generated-prefix",          Element::string },
    { "ddns-qualifying-suffix",         Element::string },
207 208
    { "hostname-char-set",              Element::string },
    { "hostname-char-replacement",      Element::string },
209
    { "metadata",                       Element::map }
210 211
};

212 213
/// @brief This table defines default values for each IPv6 subnet.
const SimpleDefaults SimpleParser6::SUBNET6_DEFAULTS = {
214 215 216 217
    { "id",               Element::integer, "0" }, // 0 means autogenerate
    { "interface",        Element::string,  "" },
    { "client-class",     Element::string,  "" },
    { "rapid-commit",     Element::boolean, "false" }, // rapid-commit disabled by default
218
    { "interface-id",     Element::string,  "" }
219 220
};

221 222
/// @brief This table defines default values for each IPv6 shared network.
const SimpleDefaults SimpleParser6::SHARED_NETWORK6_DEFAULTS = {
223
    { "client-class",     Element::string,  "" },
224
    { "interface",        Element::string,  "" },
225 226 227 228
    { "interface-id",     Element::string,  "" },
    { "rapid-commit",     Element::boolean, "false" } // rapid-commit disabled by default
};

229 230 231 232 233 234
/// @brief List of parameters that can be inherited from the global to subnet6 scope.
///
/// Some parameters may be defined on both global (directly in Dhcp6) and
/// subnet (Dhcp6/subnet6/...) scope. If not defined in the subnet scope,
/// the value is being inherited (derived) from the global scope. This
/// array lists all of such parameters.
235 236 237 238
///
/// This list is also used for inheriting from global to shared networks
/// and from shared networks to subnets within it.
const ParamsList SimpleParser6::INHERIT_TO_SUBNET6 = {
239
    "client-class",
240 241
    "interface",
    "interface-id",
242
    "preferred-lifetime",
243 244
    "min-preferred-lifetime",
    "max-preferred-lifetime",
245 246 247 248 249
    "rapid-commit",
    "rebind-timer",
    "relay",
    "renew-timer",
    "reservation-mode",
250
    "valid-lifetime",
251 252
    "min-valid-lifetime",
    "max-valid-lifetime",
253 254
    "calculate-tee-times",
    "t1-percent",
255 256 257 258 259 260
    "t2-percent",
    "ddns-send-updates",
    "ddns-override-no-update",
    "ddns-override-client-update",
    "ddns-replace-client-name",
    "ddns-generated-prefix",
261 262 263
    "ddns-qualifying-suffix",
    "hostname-char-set",
    "hostname-char-replacement"
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
/// @brief This table defines all pool parameters.
///
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows pool_param rules in bison grammar.
const SimpleKeywords SimpleParser6::POOL6_PARAMETERS = {
    { "pool",                   Element::string },
    { "option-data",            Element::list },
    { "client-class",           Element::string },
    { "require-client-classes", Element::list },
    { "user-context",           Element::map },
    { "comment",                Element::string },
    { "metadata",               Element::map }
};

/// @brief This table defines all prefix delegation pool parameters.
///
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows pd_pool_param rules in bison grammar.
const SimpleKeywords SimpleParser6::PD_POOL6_PARAMETERS = {
    { "prefix",                 Element::string },
    { "prefix-len",             Element::integer },
    { "delegated-len",          Element::integer },
    { "option-data",            Element::list },
    { "client-class",           Element::string },
    { "require-client-classes", Element::list },
    { "excluded-prefix",        Element::string },
    { "excluded-prefix-len",    Element::integer },
    { "user-context",           Element::map },
    { "comment",                Element::string },
    { "metadata",               Element::map }
};

/// @brief This table defines all shared network parameters for DHCPv6.
///
/// Boolean, integer, real and string types are for scalar parameters,
/// list and map types for entries.
/// Order follows shared_network_param rule in bison grammar.
const SimpleKeywords SimpleParser6::SHARED_NETWORK6_PARAMETERS = {
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
    { "name",                           Element::string },
    { "subnet6",                        Element::list },
    { "interface",                      Element::string },
    { "interface-id",                   Element::string },
    { "renew-timer",                    Element::integer },
    { "rebind-timer",                   Element::integer },
    { "option-data",                    Element::list },
    { "relay",                          Element::map },
    { "reservation-mode",               Element::string },
    { "client-class",                   Element::string },
    { "require-client-classes",         Element::list },
    { "preferred-lifetime",             Element::integer },
    { "min-preferred-lifetime",         Element::integer },
    { "max-preferred-lifetime",         Element::integer },
    { "rapid-commit",                   Element::boolean },
    { "valid-lifetime",                 Element::integer },
    { "min-valid-lifetime",             Element::integer },
    { "max-valid-lifetime",             Element::integer },
    { "user-context",                   Element::map },
    { "comment",                        Element::string },
    { "calculate-tee-times",            Element::boolean },
    { "t1-percent",                     Element::real },
    { "t2-percent",                     Element::real },
    { "ddns-send-updates",              Element::boolean },
    { "ddns-override-no-update",        Element::boolean },
    { "ddns-override-client-update",    Element::boolean },
    { "ddns-replace-client-name",       Element::string },
    { "ddns-generated-prefix",          Element::string },
    { "ddns-qualifying-suffix",         Element::string },
335 336
    { "hostname-char-set",              Element::string },
    { "hostname-char-replacement",      Element::string },
337
    { "metadata",                       Element::map }
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
};

/// @brief This table defines default values for each IPv6 subnet.
const SimpleDefaults SimpleParser6::SHARED_SUBNET6_DEFAULTS = {
    { "id",               Element::integer, "0" } // 0 means autogenerate
};

/// @brief This table defines default values for interfaces for DHCPv6.
const SimpleDefaults SimpleParser6::IFACE6_DEFAULTS = {
    { "re-detect", Element::boolean, "true" }
};

/// @brief This table defines default values for dhcp-queue-control in DHCPv4.
const SimpleDefaults SimpleParser6::DHCP_QUEUE_CONTROL6_DEFAULTS = {
    { "enable-queue",   Element::boolean, "false"},
    { "queue-type",     Element::string,  "kea-ring6"},
    { "capacity",       Element::integer, "500"}
};

/// @brief This defines default values for sanity checking for DHCPv6.
const SimpleDefaults SimpleParser6::SANITY_CHECKS6_DEFAULTS = {
    { "lease-checks", Element::string, "warn" }
};

362
/// @}
363

364 365 366
/// ---------------------------------------------------------------------------
/// --- end of default values -------------------------------------------------
/// ---------------------------------------------------------------------------
367

368
size_t SimpleParser6::setAllDefaults(ElementPtr global) {
369 370 371 372 373 374 375 376 377 378 379 380 381
    size_t cnt = 0;

    // Set global defaults first.
    cnt = setDefaults(global, GLOBAL6_DEFAULTS);

    // Now set the defaults for each specified option definition
    ConstElementPtr option_defs = global->get("option-def");
    if (option_defs) {
        BOOST_FOREACH(ElementPtr option_def, option_defs->listValue()) {
            cnt += SimpleParser::setDefaults(option_def, OPTION6_DEF_DEFAULTS);
        }
    }

382
    // Set the defaults for option data
383 384 385 386 387 388 389
    ConstElementPtr options = global->get("option-data");
    if (options) {
        BOOST_FOREACH(ElementPtr single_option, options->listValue()) {
            cnt += SimpleParser::setDefaults(single_option, OPTION6_DEFAULTS);
        }
    }

390 391 392 393 394 395
    // Now set the defaults for defined subnets
    ConstElementPtr subnets = global->get("subnet6");
    if (subnets) {
        cnt += setListDefaults(subnets, SUBNET6_DEFAULTS);
    }

396 397 398 399 400 401 402
    // Set the defaults for interfaces config
    ConstElementPtr ifaces_cfg = global->get("interfaces-config");
    if (ifaces_cfg) {
        ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(ifaces_cfg);
        cnt += setDefaults(mutable_cfg, IFACE6_DEFAULTS);
    }

403 404 405 406
    // Set defaults for shared networks
    ConstElementPtr shared = global->get("shared-networks");
    if (shared) {
        BOOST_FOREACH(ElementPtr net, shared->listValue()) {
407 408 409

            cnt += setDefaults(net, SHARED_NETWORK6_DEFAULTS);

410 411
            ConstElementPtr subs = net->get("subnet6");
            if (subs) {
412
                cnt += setListDefaults(subs, SHARED_SUBNET6_DEFAULTS);
413 414 415 416
            }
        }
    }

417 418 419 420 421 422 423 424 425 426 427 428 429
    // Set the defaults for dhcp-queue-control.  If the element isn't there
    // we'll add it.
    ConstElementPtr queue_control = global->get("dhcp-queue-control");
    ElementPtr mutable_cfg;
    if (queue_control) {
        mutable_cfg = boost::const_pointer_cast<Element>(queue_control);
    } else {
        mutable_cfg = Element::createMap();
        global->set("dhcp-queue-control", mutable_cfg);
    }

    cnt += setDefaults(mutable_cfg, DHCP_QUEUE_CONTROL6_DEFAULTS);

430 431 432 433 434 435 436 437 438 439 440 441
    // Set the defaults for sanity-checks.  If the element isn't
    // there we'll add it.
    ConstElementPtr sanity_checks = global->get("sanity-checks");
    if (sanity_checks) {
        mutable_cfg = boost::const_pointer_cast<Element>(sanity_checks);
    } else {
        mutable_cfg = Element::createMap();
        global->set("sanity-checks", mutable_cfg);
    }

    cnt += setDefaults(mutable_cfg, SANITY_CHECKS6_DEFAULTS);

442 443 444
    return (cnt);
}

445
size_t SimpleParser6::deriveParameters(ElementPtr global) {
446 447 448 449 450 451
    size_t cnt = 0;
    // Now derive global parameters into subnets.
    ConstElementPtr subnets = global->get("subnet6");
    if (subnets) {
        BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
            cnt += SimpleParser::deriveParams(global, single_subnet,
452
                                              INHERIT_TO_SUBNET6);
453 454 455
        }
    }

456 457 458 459 460 461 462 463 464 465
    // Deriving parameters for shared networks is a bit more involved.
    // First, the shared-network level derives from global, and then
    // subnets within derive from it.
    ConstElementPtr shared = global->get("shared-networks");
    if (shared) {
        BOOST_FOREACH(ElementPtr net, shared->listValue()) {
            // First try to inherit the parameters from shared network,
            // if defined there.
            // Then try to inherit them from global.
            cnt += SimpleParser::deriveParams(global, net,
466
                                              INHERIT_TO_SUBNET6);
467 468 469 470 471 472

            // Now we need to go thrugh all the subnets in this net.
            subnets = net->get("subnet6");
            if (subnets) {
                BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
                    cnt += SimpleParser::deriveParams(net, single_subnet,
473
                                                      INHERIT_TO_SUBNET6);
474 475 476 477 478
                }
            }
        }
    }

479 480 481
    return (cnt);
}

482 483
};
};