Kea 2.7.5
sanity_checker.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2022 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#include <config.h>
7
10#include <dhcpsrv/cfgmgr.h>
11#include <dhcpsrv/subnet_id.h>
12#include <dhcpsrv/dhcpsrv_log.h>
13#include <sstream>
14
15namespace isc {
16namespace dhcp {
17
19 SrvConfigPtr cfg;
20 if (current) {
21 cfg = CfgMgr::instance().getCurrentCfg();
22 } else {
23 cfg = CfgMgr::instance().getStagingCfg();
24 }
25
26 if (cfg) {
27 CfgConsistencyPtr sanity = cfg->getConsistency();
28 return (sanity && (sanity->getLeaseSanityCheck() != CfgConsistency::LEASE_CHECK_NONE));
29 }
30
31 return (false);
32}
33
34void SanityChecker::checkLease(Lease4Ptr& lease, bool current) {
35 SrvConfigPtr cfg;
36 if (current) {
37 cfg = CfgMgr::instance().getCurrentCfg();
38 } else {
39 cfg = CfgMgr::instance().getStagingCfg();
40 }
41
42 CfgConsistencyPtr sanity = cfg->getConsistency();
43 if (sanity->getLeaseSanityCheck() == CfgConsistency::LEASE_CHECK_NONE) {
44 // No sense going farther.
45 return;
46 }
47
48 CfgSubnets4Ptr subnets = cfg->getCfgSubnets4();
49 checkLeaseInternal(lease, sanity, subnets);
50}
51
52void SanityChecker::checkLease(Lease6Ptr& lease, bool current) {
53 // We only check IA_NAs currently.
54 if (lease->type_ != Lease::TYPE_NA) {
55 return;
56 }
57
58 SrvConfigPtr cfg;
59 if (current) {
60 cfg = CfgMgr::instance().getCurrentCfg();
61 } else {
62 cfg = CfgMgr::instance().getStagingCfg();
63 }
64 CfgConsistencyPtr sanity = cfg->getConsistency();
65 if (sanity->getLeaseSanityCheck() == CfgConsistency::LEASE_CHECK_NONE) {
66 // No sense going farther.
67 return;
68 }
69
70 CfgSubnets6Ptr subnets = cfg->getCfgSubnets6();
71 checkLeaseInternal(lease, sanity, subnets);
72}
73
74template<typename LeasePtrType, typename SubnetsType>
75void SanityChecker::checkLeaseInternal(LeasePtrType& lease, const CfgConsistencyPtr& checks,
76 const SubnetsType& subnets) {
77
78 auto subnet = subnets->getBySubnetId(lease->subnet_id_);
79 if (subnet && subnet->inRange(lease->addr_)) {
80
81 // If the subnet is defined and the address is in range, we're good.
82
83 return;
84 }
85
86 // Ok, if we got here, that means that either we did not find a subnet
87 // of found it, but it wasn't the right subnet.
88 SubnetID id = findSubnetId(lease, subnets);
89
90 // Prepare a message in the case the check fails.
91 std::ostringstream msg;
92 if (id != 0) {
93 msg << "the lease should have subnet-id " << id;
94 } else {
95 msg << "the lease IP address did not belong to a configured subnet";
96 }
97
98 switch (checks->getLeaseSanityCheck()) {
100 if (lease->subnet_id_ != id) {
101 // Print a warning, but return the lease as is.
103 .arg(lease->addr_.toText())
104 .arg(lease->subnet_id_)
105 .arg(msg.str());
106 }
107 break;
108
110 if (lease->subnet_id_ != id) {
111
112 // If there is a better subnet, use it.
113 if (id != 0) {
115 .arg(lease->addr_.toText())
116 .arg(lease->subnet_id_)
117 .arg(id);
118 lease->subnet_id_ = id;
119 } else {
120 // If not, return the lease as is.
122 .arg(lease->addr_.toText())
123 .arg(lease->subnet_id_)
124 .arg(msg.str());
125 }
126 }
127 break;
128
130 if (lease->subnet_id_ != id) {
131
132 // If there is a better subnet, use it.
133 if (id != 0) {
135 .arg(lease->addr_.toText())
136 .arg(lease->subnet_id_)
137 .arg(id);
138 lease->subnet_id_ = id;
139 break;
140 } else {
141 // If not, delete the lease.
143 .arg(lease->addr_.toText())
144 .arg(lease->subnet_id_)
145 .arg(msg.str());
146 lease.reset();
147 }
148
149 }
150 break;
151
153 if (lease->subnet_id_ != id) {
155 .arg(lease->addr_.toText())
156 .arg(lease->subnet_id_)
157 .arg(msg.str());
158 lease.reset();
159 }
160 break;
161
162 default:
163 // Shouldn't get here but some compilers and analyzers
164 // complain. We'll we treat it as NONE and return the
165 // lease as-is.
166 break;
167
168 }
169
170 // Additional checks may be implemented in the future here.
171
174}
175
176template<typename LeaseType, typename SubnetsType>
177SubnetID SanityChecker::findSubnetId(const LeaseType& lease, const SubnetsType& subnets) {
178 auto subnet = subnets->selectSubnet(lease->addr_);
179 if (!subnet) {
180 return (0);
181 }
182
183 return (subnet->getID());
184}
185
186}
187}
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:28
void checkLease(Lease4Ptr &lease, bool current=true)
Sanity checks and possibly corrects an IPv4 lease.
static bool leaseCheckingEnabled(bool current=true)
Indicates the specified configuration enables lease sanity checking.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_LEASE_SANITY_FAIL_DISCARD
const isc::log::MessageID DHCPSRV_LEASE_SANITY_FAIL
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:508
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
boost::shared_ptr< CfgSubnets6 > CfgSubnets6Ptr
Non-const pointer.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< CfgSubnets4 > CfgSubnets4Ptr
Non-const pointer.
boost::shared_ptr< CfgConsistency > CfgConsistencyPtr
Type used to for pointing to CfgConsistency structure.
const isc::log::MessageID DHCPSRV_LEASE_SANITY_FIXED
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:295
Defines the logger used by the top-level component of kea-lfc.
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47