1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (C) 2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <cc/data.h>
#include <dhcp/classify.h>
#include <dhcpsrv/lease.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/srv_config.h>
#include <dhcpsrv/subnet_id.h>
#include <limits/configuration.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits/limit_manager.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <util/dhcp_space.h>

#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::util;

using namespace std;

namespace isc {
namespace limits {

LimitManager&
LimitManager::instance() {
    static LimitManager instance;
    return (instance);
}

void
LimitManager::clear() {
    clocked_in_times_by_class_.clear();
    clocked_in_times_by_subnet_id_.clear();
}

void
LimitManager::initialize(SrvConfigPtr const& config) {
    // Reset any packet accounting that had transpired so far.
    clear();

    // Parse the configuration to get the limits of interest.
    parse(config);
}

void
LimitManager::addClientClassesToLeaseContext(ClientClasses const& classes, LeasePtr const& lease) {
    // The ConstElementPtr - ElementPtr division forces the copies below. Changes to the lease's
    // user context are not expected so shallow copy is fine as it would have been to reuse the
    // original user context. Feel free to get rid of the copying. Preferable to not resort to the
    // undefined behavior that is const-cast + writing data to the memory freshly made mutable.

    // Get a mutable user context.
    ConstElementPtr user_context_source(lease->getContext());
    ElementPtr user_context;
    if (user_context_source) {
        user_context = copy(user_context_source, 0);
    } else {
        user_context = Element::createMap();
    }

    // Get a mutable ISC element.
    ConstElementPtr ISC_source(user_context->get("ISC"));
    ElementPtr ISC;
    if (ISC_source) {
        ISC = copy(ISC_source, 0);
    } else {
        ISC = Element::createMap();
        user_context->set("ISC", ISC);
    }

    // Set the new client classes. Old ones are discarded.
    ISC->set("client-classes", classes.toElement());

    // Set the modified user context in the lease.
    lease->setContext(user_context);
}

void
LimitManager::parse(SrvConfigPtr const& config) {
    // Parse the configuration to get the limits of interest.
    address_limit_configuration_.parse(config);
    prefix_limit_configuration_.parse(config);
    rate_limit_configuration_.parse(config);
}

template <>
ElementPtr
LimitManager::clientClassLimitsToElement<DHCPv4>(SubClassRelationContainer const& classes,
                                                 Lease::Type const& /* lease_type */) {
    ElementPtr result(Element::createList());

    for (auto const& c : classes) {
        auto const& class_def = isc::dhcp::CfgMgr::instance().getCurrentCfg()->getClientClassDictionary()->findClass(c.class_def_);
        if (!class_def) {
            continue;
        }

        auto const& limit_cfg = address_limit_configuration_.parseUserContext(class_def->getContext());
        if (limit_cfg) {
            int64_t const limit_candidate(limit_cfg->intValue());
            checkForLimitBoundaries<LeaseLimit>(limit_candidate);
            // Get the limit.
            LeaseLimit const limit(limit_candidate);

            ElementPtr element(Element::createMap());
            element->set("name", Element::create(c.class_));
            element->set("address-limit", Element::create(limit));
            result->add(element);
        }
    }

    return (result);
}

template <>
ElementPtr
LimitManager::clientClassLimitsToElement<DHCPv6>(SubClassRelationContainer const& classes,
                                                 Lease::Type const& lease_type) {
    ElementPtr result(Element::createList());

    auto get_configuration = [&]() -> Configuration<LeaseLimit>& {
        if (lease_type == Lease::TYPE_PD) {
            return (prefix_limit_configuration_);
        } else {
            return (address_limit_configuration_);
        }
    };

    auto& config = get_configuration();

    for (auto const& c : classes) {
        auto const& class_def = isc::dhcp::CfgMgr::instance().getCurrentCfg()->getClientClassDictionary()->findClass(c.class_def_);
        if (!class_def) {
            continue;
        }

        auto& limit_cfg = config.parseUserContext(class_def->getContext());
        if (limit_cfg) {
            int64_t const limit_candidate(limit_cfg->intValue());
            checkForLimitBoundaries<LeaseLimit>(limit_candidate);
            // Get the limit.
            LeaseLimit const limit(limit_candidate);

            ElementPtr element(Element::createMap());
            element->set("name", Element::create(c.class_));
            element->set(config.key(), Element::create(limit));
            result->add(element);
        }
    }

    return (result);
}

template <>
ElementPtr
LimitManager::subnetLimitToElement<DHCPv4>(SubnetID const subnet_id,
                                           Lease::Type const& /* lease_type */) {
    ElementPtr result(Element::createMap());

    auto const& subnet_cfg = isc::dhcp::CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getBySubnetId(subnet_id);
    if (subnet_cfg) {
        auto const& limit_cfg = address_limit_configuration_.parseUserContext(subnet_cfg->getContext());
        if (limit_cfg) {
            int64_t const limit_candidate(limit_cfg->intValue());
            checkForLimitBoundaries<LeaseLimit>(limit_candidate);
            // Get the limit.
            LeaseLimit const limit(limit_candidate);

            result->set("id", Element::create(subnet_id));
            result->set("address-limit", Element::create(limit));
        }
    }

    return (result);
}

template <>
ElementPtr
LimitManager::subnetLimitToElement<DHCPv6>(SubnetID const subnet_id,
                                           Lease::Type const& lease_type) {
    ElementPtr result(Element::createMap());

    std::string limit_name;
    auto get_configuration = [&]() -> Configuration<LeaseLimit>& {
        if (lease_type == Lease::TYPE_PD) {
            limit_name = std::string("prefix-limit");
            return (prefix_limit_configuration_);
        } else {
            limit_name = std::string("address-limit");
            return (address_limit_configuration_);
        }
    };

    auto& config = get_configuration();

    auto const& subnet_cfg = isc::dhcp::CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getBySubnetId(subnet_id);
    if (subnet_cfg) {
        auto const& limit_cfg = config.parseUserContext(subnet_cfg->getContext());
        if (limit_cfg) {
            int64_t const limit_candidate(limit_cfg->intValue());
            checkForLimitBoundaries<LeaseLimit>(limit_candidate);
            // Get the limit.
            LeaseLimit const limit(limit_candidate);

            result->set("id", Element::create(subnet_id));
            result->set(limit_name, Element::create(limit));
        }
    }

    return (result);
}

template <>
ConstElementPtr
LimitManager::subnetRateLimit<DHCPv4>(SubnetID const subnet_id) {
    ConstElementPtr limit;

    auto const& subnet_cfg = isc::dhcp::CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getBySubnetId(subnet_id);
    if (subnet_cfg) {
        limit = rate_limit_configuration_.parseUserContext(subnet_cfg->getContext());
    }

    return (limit);
}

template <>
ConstElementPtr
LimitManager::subnetRateLimit<DHCPv6>(SubnetID const subnet_id) {
    ConstElementPtr limit;

    auto const& subnet_cfg = isc::dhcp::CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getBySubnetId(subnet_id);
    if (subnet_cfg) {
        limit = rate_limit_configuration_.parseUserContext(subnet_cfg->getContext());
    }

    return (limit);
}

template <>
string
LimitManager::checkLeaseLimits<DHCPv4>(ConstElementPtr const& context) const {
    return (LeaseMgrFactory::instance().checkLimits4(context));
}

template <>
string
LimitManager::checkLeaseLimits<DHCPv6>(ConstElementPtr const& context) const {
    return (LeaseMgrFactory::instance().checkLimits6(context));
}

template <>
void
LimitManager::recountClassLeases<DHCPv4>() const {
    if (isc::dhcp::LeaseMgrFactory::instance().getType() == "memfile") {
        isc::dhcp::LeaseMgrFactory::instance().recountClassLeases4();
    }
}

template <>
void
LimitManager::recountClassLeases<DHCPv6>() const {
    if (isc::dhcp::LeaseMgrFactory::instance().getType() == "memfile") {
        isc::dhcp::LeaseMgrFactory::instance().recountClassLeases6();
    }
}

}  // namespace limits
}  // namespace isc