| Kea 3.0.2-git

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
8#include <dhcpsrv/sanity_checker.h>
9#include <dhcpsrv/cfg_consistency.h>
10#include <dhcpsrv/cfgmgr.h>
11#include <dhcpsrv/subnet_id.h>
12#include <dhcpsrv/dhcpsrv_log.h>
13#include <sstream>
14
15 namespace isc {
16 namespace dhcp {
17
18 bool SanityChecker::leaseCheckingEnabled(bool current) {
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
34 void 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
52 void 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
74 template<typename LeasePtrType, typename SubnetsType>
75 void 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()) {
99 case CfgConsistency::LEASE_CHECK_WARN:
100 if (lease->subnet_id_ != id) {
101 // Print a warning, but return the lease as is.
102 LOG_WARN(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL)
103 .arg(lease->addr_.toText())
104 .arg(lease->subnet_id_)
105 .arg(msg.str());
106 }
107 break;
108
109 case CfgConsistency::LEASE_CHECK_FIX:
110 if (lease->subnet_id_ != id) {
111
112 // If there is a better subnet, use it.
113 if (id != 0) {
114 LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FIXED)
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.
121 LOG_WARN(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL)
122 .arg(lease->addr_.toText())
123 .arg(lease->subnet_id_)
124 .arg(msg.str());
125 }
126 }
127 break;
128
129 case CfgConsistency::LEASE_CHECK_FIX_DEL:
130 if (lease->subnet_id_ != id) {
131
132 // If there is a better subnet, use it.
133 if (id != 0) {
134 LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FIXED)
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.
142 LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL_DISCARD)
143 .arg(lease->addr_.toText())
144 .arg(lease->subnet_id_)
145 .arg(msg.str());
146 lease.reset();
147 }
148
149 }
150 break;
151
152 case CfgConsistency::LEASE_CHECK_DEL:
153 if (lease->subnet_id_ != id) {
154 LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL_DISCARD)
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
176 template<typename LeaseType, typename SubnetsType>
177 SubnetID 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}
cfg_consistency.h
cfgmgr.h
isc::dhcp::CfgConsistency::LEASE_CHECK_FIX
@ LEASE_CHECK_FIX
Definition cfg_consistency.h:27
isc::dhcp::CfgConsistency::LEASE_CHECK_DEL
@ LEASE_CHECK_DEL
Definition cfg_consistency.h:32
isc::dhcp::CfgConsistency::LEASE_CHECK_FIX_DEL
@ LEASE_CHECK_FIX_DEL
Definition cfg_consistency.h:30
isc::dhcp::CfgConsistency::LEASE_CHECK_WARN
@ LEASE_CHECK_WARN
Definition cfg_consistency.h:26
isc::dhcp::CfgConsistency::LEASE_CHECK_NONE
@ LEASE_CHECK_NONE
Definition cfg_consistency.h:25
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
isc::dhcp::CfgMgr::getStagingCfg
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition cfgmgr.cc:121
isc::dhcp::CfgMgr::getCurrentCfg
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition cfgmgr.cc:116
isc::dhcp::SanityChecker::checkLease
void checkLease(Lease4Ptr &lease, bool current=true)
Sanity checks and possibly corrects an IPv4 lease.
Definition sanity_checker.cc:34
isc::dhcp::SanityChecker::leaseCheckingEnabled
static bool leaseCheckingEnabled(bool current=true)
Indicates the specified configuration enables lease sanity checking.
Definition sanity_checker.cc:18
dhcpsrv_log.h
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
LOG_WARN
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::dhcpsrv_logger
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
isc::dhcp::DHCPSRV_LEASE_SANITY_FAIL_DISCARD
const isc::log::MessageID DHCPSRV_LEASE_SANITY_FAIL_DISCARD
Definition dhcpsrv_messages.h:86
isc::dhcp::DHCPSRV_LEASE_SANITY_FAIL
const isc::log::MessageID DHCPSRV_LEASE_SANITY_FAIL
Definition dhcpsrv_messages.h:85
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:528
isc::dhcp::SrvConfigPtr
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition srv_config.h:1136
isc::dhcp::CfgSubnets6Ptr
boost::shared_ptr< CfgSubnets6 > CfgSubnets6Ptr
Non-const pointer.
Definition cfg_subnets6.h:348
isc::dhcp::SubnetID
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
isc::dhcp::CfgSubnets4Ptr
boost::shared_ptr< CfgSubnets4 > CfgSubnets4Ptr
Non-const pointer.
Definition cfg_subnets4.h:350
isc::dhcp::CfgConsistencyPtr
boost::shared_ptr< CfgConsistency > CfgConsistencyPtr
Type used to for pointing to CfgConsistency structure.
Definition cfg_consistency.h:103
isc::dhcp::DHCPSRV_LEASE_SANITY_FIXED
const isc::log::MessageID DHCPSRV_LEASE_SANITY_FIXED
Definition dhcpsrv_messages.h:87
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
sanity_checker.h
isc::dhcp::Lease::TYPE_NA
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47
subnet_id.h