Kea  1.5.0
sanity_checker.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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/.
8 #include <dhcpsrv/cfg_subnets4.h>
9 #include <dhcpsrv/cfgmgr.h>
10 #include <dhcpsrv/subnet_id.h>
11 #include <dhcpsrv/dhcpsrv_log.h>
12 
13 namespace isc {
14 namespace dhcp {
15 
16 void SanityChecker::checkLease(Lease4Ptr& lease, bool current) {
17  SrvConfigPtr cfg;
18  if (current) {
20  } else {
22  }
23  CfgConsistencyPtr sanity = cfg->getConsistency();
24  CfgSubnets4Ptr subnets = cfg->getCfgSubnets4();
25  checkLeaseInternal(lease, sanity, subnets);
26 }
27 
28 void SanityChecker::checkLease(Lease6Ptr& lease, bool current) {
29  SrvConfigPtr cfg;
30  if (current) {
32  } else {
34  }
35  CfgConsistencyPtr sanity = cfg->getConsistency();
36  CfgSubnets6Ptr subnets = cfg->getCfgSubnets6();
37  checkLeaseInternal(lease, sanity, subnets);
38 }
39 
40 template<typename LeasePtrType, typename SubnetsType>
41 void SanityChecker::checkLeaseInternal(LeasePtrType& lease, const CfgConsistencyPtr& checks,
42  const SubnetsType& subnets) {
43 
44  if (checks->getLeaseSanityCheck() == CfgConsistency::LEASE_CHECK_NONE) {
45  return;
46  }
47 
48  auto subnet = subnets->getBySubnetId(lease->subnet_id_);
49 
50  if (subnet && subnet->inRange(lease->addr_)) {
51 
52  // If the subnet is defined and the address is in range, we're good.
53 
54  return;
55  }
56 
57  // Ok, if we got here, that means that either we did not find a subnet
58  // of found it, but it wasn't the right subnet.
59  SubnetID id = findSubnetId(lease, subnets);
60 
61  switch (checks->getLeaseSanityCheck()) {
63  // No checks whatsoever, just return the lease as-is.
64  break;
66  if (lease->subnet_id_ != id) {
67  // Print a warning, but return the lease as is.
68  LOG_WARN(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL)
69  .arg(lease->addr_.toText()).arg(lease->subnet_id_);
70  }
71  break;
72 
74  if (lease->subnet_id_ != id) {
75 
76  // If there is a better subnet, use it.
77  if (id != 0) {
78  LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FIXED)
79  .arg(lease->addr_.toText()).arg(lease->subnet_id_).arg(id);
80  lease->subnet_id_ = id;
81  } else {
82  // If not, return the lease as is.
83  LOG_WARN(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL)
84  .arg(lease->addr_.toText()).arg(lease->subnet_id_);
85  }
86  }
87  break;
88 
90  if (lease->subnet_id_ != id) {
91 
92  // If there is a better subnet, use it.
93  if (id != 0) {
94  LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FIXED)
95  .arg(lease->addr_.toText()).arg(lease->subnet_id_).arg(id);
96  lease->subnet_id_ = id;
97  break;
98  } else {
99  // If not, delete the lease.
100  LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL_DISCARD)
101  .arg(lease->addr_.toText()).arg(lease->subnet_id_);
102  lease.reset();
103  }
104 
105  }
106  break;
108  if (lease->subnet_id_ != id) {
109  LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_SANITY_FAIL_DISCARD)
110  .arg(lease->addr_.toText()).arg(lease->subnet_id_);
111  lease.reset();
112  }
113  break;
114  }
115 
116  // Additional checks may be implemented in the future here.
117 
120 }
121 
122 template<typename LeaseType, typename SubnetsType>
123 SubnetID SanityChecker::findSubnetId(const LeaseType& lease, const SubnetsType& subnets) {
124  //CfgSubnets4Ptr subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4();
125 
126  auto subnet = subnets->selectSubnet(lease->addr_);
127  if (!subnet) {
128  return (0);
129  }
130 
131  return (subnet->getID());
132 }
133 
134 };
135 };
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition: srv_config.h:707
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition: cfgmgr.cc:154
boost::shared_ptr< CfgConsistency > CfgConsistencyPtr
Type used to for pointing to CfgConsistency structure.
boost::shared_ptr< CfgSubnets4 > CfgSubnets4Ptr
Non-const pointer.
Definition: cfg_subnets4.h:270
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:245
boost::shared_ptr< CfgSubnets6 > CfgSubnets6Ptr
Non-const pointer.
Definition: cfg_subnets6.h:268
Defines the logger used by the top-level component of kea-dhcp-ddns.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:460
void checkLease(Lease4Ptr &lease, bool current=true)
Sanity checks and possibly corrects an IPv4 lease.
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition: cfgmgr.cc:160