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
// Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
//
// 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/.
#include <config.h>

#include <dhcpsrv/sanity_checker.h>
#include <dhcpsrv/cfg_consistency.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace dhcp {

bool SanityChecker::leaseCheckingEnabled(bool current) {
    SrvConfigPtr cfg;
    if (current) {
        cfg = CfgMgr::instance().getCurrentCfg();
    } else {
        cfg = CfgMgr::instance().getStagingCfg();
    }

    if (cfg) {
        CfgConsistencyPtr sanity = cfg->getConsistency();
        return (sanity && (sanity->getLeaseSanityCheck() != CfgConsistency::LEASE_CHECK_NONE));
    }

    return (false);
}

void SanityChecker::checkLease(Lease4Ptr& lease, bool current) {
    SrvConfigPtr cfg;
    if (current) {
        cfg = CfgMgr::instance().getCurrentCfg();
    } else {
        cfg = CfgMgr::instance().getStagingCfg();
    }

    CfgConsistencyPtr sanity = cfg->getConsistency();
    if (sanity->getLeaseSanityCheck() == CfgConsistency::LEASE_CHECK_NONE) {
        // No sense going farther.
        return;
    }

    CfgSubnets4Ptr subnets = cfg->getCfgSubnets4();
    checkLeaseInternal(lease, sanity, subnets);
}

void SanityChecker::checkLease(Lease6Ptr& lease, bool current) {
    // We only check IA_NAs currently.
    if (lease->type_ != Lease::TYPE_NA) {
        return;
    }

    SrvConfigPtr cfg;
    if (current) {
        cfg = CfgMgr::instance().getCurrentCfg();
    } else {
        cfg = CfgMgr::instance().getStagingCfg();
    }
    CfgConsistencyPtr sanity = cfg->getConsistency();
    if (sanity->getLeaseSanityCheck() == CfgConsistency::LEASE_CHECK_NONE) {
        // No sense going farther.
        return;
    }

    CfgSubnets6Ptr subnets = cfg->getCfgSubnets6();
    checkLeaseInternal(lease, sanity, subnets);
}

template<typename LeasePtrType, typename SubnetsType>
void SanityChecker::checkLeaseInternal(LeasePtrType& lease, const CfgConsistencyPtr& checks,
                               const SubnetsType& subnets) {

    auto subnet = subnets->getBySubnetId(lease->subnet_id_);
    if (subnet && subnet->inRange(lease->addr_)) {

        // If the subnet is defined and the address is in range, we're good.

        return;
    }

    // Ok, if we got here, that means that either we did not find a subnet
    // of found it, but it wasn't the right subnet.
    SubnetID id = findSubnetId(lease, subnets);

    // Prepare a message in the case the check fails.
    std::ostringstream msg;
    if (id != 0) {
        msg << "the lease should have subnet-id " << id;
    } else {
        msg << "the lease IP address did not belong to a configured subnet";
    }

    switch (checks->getLeaseSanityCheck()) {
    case CfgConsistency::LEASE_CHECK_WARN:
        if (lease->subnet_id_ != id) {
            // Print a warning, but return the lease as is.
            LOG_WARN(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL)
                .arg(lease->addr_.toText())
                .arg(lease->subnet_id_)
                .arg(msg.str());
        }
        break;

    case CfgConsistency::LEASE_CHECK_FIX:
        if (lease->subnet_id_ != id) {

            // If there is a better subnet, use it.
            if (id != 0) {
                LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FIXED)
                    .arg(lease->addr_.toText())
                    .arg(lease->subnet_id_)
                    .arg(id);
                lease->subnet_id_ = id;
            } else {
                // If not, return the lease as is.
                LOG_WARN(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL)
                    .arg(lease->addr_.toText())
                    .arg(lease->subnet_id_)
                    .arg(msg.str());
            }
        }
        break;

    case CfgConsistency::LEASE_CHECK_FIX_DEL:
        if (lease->subnet_id_ != id) {

            // If there is a better subnet, use it.
            if (id != 0) {
                LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FIXED)
                    .arg(lease->addr_.toText())
                    .arg(lease->subnet_id_)
                    .arg(id);
                lease->subnet_id_ = id;
                break;
            } else {
                // If not, delete the lease.
                LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL_DISCARD)
                    .arg(lease->addr_.toText())
                    .arg(lease->subnet_id_)
                    .arg(msg.str());
                lease.reset();
            }

        }
        break;

    case CfgConsistency::LEASE_CHECK_DEL:
        if (lease->subnet_id_ != id) {
            LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL_DISCARD)
                .arg(lease->addr_.toText())
                .arg(lease->subnet_id_)
                .arg(msg.str());
            lease.reset();
        }
        break;

    default:
        // Shouldn't get here but some compilers and analyzers
        // complain.  We'll we treat it as NONE and return the
        // lease as-is.
        break;

    }

    // Additional checks may be implemented in the future here.

    /// @todo: add a check if the address is within specified dynamic pool
    /// if not, check if the address is reserved.
}

template<typename LeaseType, typename SubnetsType>
SubnetID SanityChecker::findSubnetId(const LeaseType& lease, const SubnetsType& subnets) {
    auto subnet = subnets->selectSubnet(lease->addr_);
    if (!subnet) {
        return (0);
    }

    return (subnet->getID());
}

}
}