2019-01-09 11:14:01 -05:00
|
|
|
// Copyright (C) 2011-2019 Internet Systems Consortium, Inc. ("ISC")
|
2011-11-27 19:35:47 +08:00
|
|
|
//
|
2015-12-15 21:37:34 +01:00
|
|
|
// 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/.
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2015-04-18 01:39:43 +02:00
|
|
|
#include <config.h>
|
2018-09-21 16:57:43 +02:00
|
|
|
#include <kea_version.h>
|
|
|
|
|
2011-11-27 19:35:47 +08:00
|
|
|
#include <dhcp/dhcp4.h>
|
2013-07-24 13:41:27 +01:00
|
|
|
#include <dhcp/duid.h>
|
|
|
|
#include <dhcp/hwaddr.h>
|
2011-11-30 15:32:40 +01:00
|
|
|
#include <dhcp/iface_mgr.h>
|
2017-09-20 21:33:52 +02:00
|
|
|
#include <dhcp/libdhcp++.h>
|
2011-12-20 19:59:14 +01:00
|
|
|
#include <dhcp/option4_addrlst.h>
|
2017-10-17 11:23:37 +02:00
|
|
|
#include <dhcp/option_custom.h>
|
2012-12-31 16:49:37 +01:00
|
|
|
#include <dhcp/option_int.h>
|
2013-01-15 19:53:58 +01:00
|
|
|
#include <dhcp/option_int_array.h>
|
2013-10-22 12:27:07 +02:00
|
|
|
#include <dhcp/option_vendor.h>
|
2013-10-18 11:10:54 +02:00
|
|
|
#include <dhcp/option_string.h>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <dhcp/pkt4.h>
|
2016-06-17 02:24:29 +02:00
|
|
|
#include <dhcp/pkt4o6.h>
|
|
|
|
#include <dhcp/pkt6.h>
|
2013-10-22 12:27:07 +02:00
|
|
|
#include <dhcp/docsis3_option_defs.h>
|
2016-06-17 02:24:29 +02:00
|
|
|
#include <dhcp4/dhcp4to6_ipc.h>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <dhcp4/dhcp4_log.h>
|
|
|
|
#include <dhcp4/dhcp4_srv.h>
|
2018-08-16 16:50:29 +02:00
|
|
|
#include <asiolink/addr_utilities.h>
|
2012-12-31 16:49:37 +01:00
|
|
|
#include <dhcpsrv/cfgmgr.h>
|
2016-04-27 09:22:29 +02:00
|
|
|
#include <dhcpsrv/cfg_host_operations.h>
|
2017-10-06 12:39:08 +02:00
|
|
|
#include <dhcpsrv/cfg_iface.h>
|
2017-10-17 11:23:37 +02:00
|
|
|
#include <dhcpsrv/cfg_shared_networks.h>
|
2014-10-21 11:23:53 +02:00
|
|
|
#include <dhcpsrv/cfg_subnets4.h>
|
2012-12-31 16:49:37 +01:00
|
|
|
#include <dhcpsrv/lease_mgr.h>
|
|
|
|
#include <dhcpsrv/lease_mgr_factory.h>
|
2015-10-19 15:30:01 +02:00
|
|
|
#include <dhcpsrv/ncr_generator.h>
|
2017-09-18 10:46:31 +02:00
|
|
|
#include <dhcpsrv/shared_network.h>
|
2012-12-31 16:49:37 +01:00
|
|
|
#include <dhcpsrv/subnet.h>
|
2014-10-30 14:53:42 +01:00
|
|
|
#include <dhcpsrv/subnet_selector.h>
|
2012-12-31 16:49:37 +01:00
|
|
|
#include <dhcpsrv/utils.h>
|
2013-07-24 13:41:27 +01:00
|
|
|
#include <dhcpsrv/utils.h>
|
2015-11-20 14:41:18 +01:00
|
|
|
#include <eval/evaluate.h>
|
|
|
|
#include <eval/eval_messages.h>
|
2013-07-15 18:41:19 +02:00
|
|
|
#include <hooks/callout_handle.h>
|
2015-05-15 14:27:46 +02:00
|
|
|
#include <hooks/hooks_log.h>
|
2013-07-24 13:41:27 +01:00
|
|
|
#include <hooks/hooks_manager.h>
|
2015-05-27 14:29:06 +02:00
|
|
|
#include <stats/stats_mgr.h>
|
2013-11-20 14:04:40 +01:00
|
|
|
#include <util/strutil.h>
|
2015-05-15 15:54:08 +02:00
|
|
|
#include <stats/stats_mgr.h>
|
2015-05-17 13:38:10 +02:00
|
|
|
#include <log/logger.h>
|
|
|
|
#include <cryptolink/cryptolink.h>
|
2015-05-17 16:32:28 +02:00
|
|
|
#include <cfgrpt/config_report.h>
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2015-06-19 14:33:11 +02:00
|
|
|
#ifdef HAVE_MYSQL
|
|
|
|
#include <dhcpsrv/mysql_lease_mgr.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PGSQL
|
|
|
|
#include <dhcpsrv/pgsql_lease_mgr.h>
|
|
|
|
#endif
|
2016-06-23 14:24:39 +02:00
|
|
|
#ifdef HAVE_CQL
|
|
|
|
#include <dhcpsrv/cql_lease_mgr.h>
|
2016-05-19 17:09:32 +03:00
|
|
|
#endif
|
2015-06-19 14:33:11 +02:00
|
|
|
#include <dhcpsrv/memfile_lease_mgr.h>
|
|
|
|
|
2018-12-04 10:10:30 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2013-10-04 21:17:07 +02:00
|
|
|
#include <boost/bind.hpp>
|
2013-10-22 12:27:07 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2017-10-17 11:23:37 +02:00
|
|
|
#include <boost/pointer_cast.hpp>
|
2015-03-10 16:52:21 +01:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2013-01-14 17:57:26 +01:00
|
|
|
|
|
|
|
#include <iomanip>
|
2019-09-05 17:24:45 +02:00
|
|
|
#include <set>
|
2013-01-14 17:57:26 +01:00
|
|
|
|
2011-11-27 19:35:47 +08:00
|
|
|
using namespace isc;
|
|
|
|
using namespace isc::asiolink;
|
2015-05-17 13:38:10 +02:00
|
|
|
using namespace isc::cryptolink;
|
2012-09-06 11:18:51 +01:00
|
|
|
using namespace isc::dhcp;
|
2013-09-02 17:21:11 +02:00
|
|
|
using namespace isc::dhcp_ddns;
|
2013-07-15 18:41:19 +02:00
|
|
|
using namespace isc::hooks;
|
2012-09-06 11:18:51 +01:00
|
|
|
using namespace isc::log;
|
2015-06-15 12:25:12 +02:00
|
|
|
using namespace isc::stats;
|
2012-09-06 11:18:51 +01:00
|
|
|
using namespace std;
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2018-01-30 13:26:14 +01:00
|
|
|
namespace {
|
|
|
|
|
2013-07-15 18:41:19 +02:00
|
|
|
/// Structure that holds registered hook indexes
|
2013-08-19 15:08:08 +02:00
|
|
|
struct Dhcp4Hooks {
|
2018-01-18 20:02:07 +01:00
|
|
|
int hook_index_buffer4_receive_; ///< index for "buffer4_receive" hook point
|
|
|
|
int hook_index_pkt4_receive_; ///< index for "pkt4_receive" hook point
|
|
|
|
int hook_index_subnet4_select_; ///< index for "subnet4_select" hook point
|
|
|
|
int hook_index_leases4_committed_; ///< index for "leases4_committed" hook point
|
|
|
|
int hook_index_lease4_release_; ///< index for "lease4_release" hook point
|
|
|
|
int hook_index_pkt4_send_; ///< index for "pkt4_send" hook point
|
|
|
|
int hook_index_buffer4_send_; ///< index for "buffer4_send" hook point
|
|
|
|
int hook_index_lease4_decline_; ///< index for "lease4_decline" hook point
|
|
|
|
int hook_index_host4_identifier_; ///< index for "host4_identifier" hook point
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
/// Constructor that registers hook points for DHCPv4 engine
|
|
|
|
Dhcp4Hooks() {
|
2018-01-18 20:02:07 +01:00
|
|
|
hook_index_buffer4_receive_ = HooksManager::registerHook("buffer4_receive");
|
|
|
|
hook_index_pkt4_receive_ = HooksManager::registerHook("pkt4_receive");
|
|
|
|
hook_index_subnet4_select_ = HooksManager::registerHook("subnet4_select");
|
|
|
|
hook_index_leases4_committed_ = HooksManager::registerHook("leases4_committed");
|
|
|
|
hook_index_pkt4_send_ = HooksManager::registerHook("pkt4_send");
|
|
|
|
hook_index_lease4_release_ = HooksManager::registerHook("lease4_release");
|
|
|
|
hook_index_buffer4_send_ = HooksManager::registerHook("buffer4_send");
|
|
|
|
hook_index_lease4_decline_ = HooksManager::registerHook("lease4_decline");
|
|
|
|
hook_index_host4_identifier_ = HooksManager::registerHook("host4_identifier");
|
2013-07-15 18:41:19 +02:00
|
|
|
}
|
|
|
|
};
|
2019-09-05 17:24:45 +02:00
|
|
|
|
|
|
|
/// List of statistics which is initialized to 0 during the DHCPv4
|
|
|
|
/// server startup.
|
|
|
|
std::set<std::string> dhcp4_statistics = {
|
|
|
|
"pkt4-received",
|
|
|
|
"pkt4-discover-received",
|
|
|
|
"pkt4-offer-received",
|
|
|
|
"pkt4-request-received",
|
|
|
|
"pkt4-ack-received",
|
|
|
|
"pkt4-nak-received",
|
|
|
|
"pkt4-release-received",
|
|
|
|
"pkt4-decline-received",
|
|
|
|
"pkt4-inform-received",
|
|
|
|
"pkt4-unknown-received",
|
|
|
|
"pkt4-sent",
|
|
|
|
"pkt4-offer-sent",
|
|
|
|
"pkt4-ack-sent",
|
|
|
|
"pkt4-nak-sent",
|
|
|
|
"pkt4-parse-failed",
|
|
|
|
"pkt4-receive-drop"
|
|
|
|
};
|
|
|
|
|
2018-01-30 13:26:14 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2013-07-15 18:41:19 +02:00
|
|
|
// Declare a Hooks object. As this is outside any function or method, it
|
|
|
|
// will be instantiated (and the constructor run) when the module is loaded.
|
|
|
|
// As a result, the hook indexes will be defined before any method in this
|
|
|
|
// module is called.
|
2013-08-19 15:08:08 +02:00
|
|
|
Dhcp4Hooks Hooks;
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2018-01-30 13:26:14 +01:00
|
|
|
|
2013-01-31 12:16:22 +00:00
|
|
|
namespace isc {
|
|
|
|
namespace dhcp {
|
|
|
|
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Exchange::Dhcpv4Exchange(const AllocEnginePtr& alloc_engine,
|
2015-03-12 20:14:41 +01:00
|
|
|
const Pkt4Ptr& query,
|
|
|
|
const Subnet4Ptr& subnet)
|
2015-03-10 21:36:08 +01:00
|
|
|
: alloc_engine_(alloc_engine), query_(query), resp_(),
|
|
|
|
context_(new AllocEngine::ClientContext4()) {
|
2015-03-12 20:14:41 +01:00
|
|
|
|
2015-03-13 13:16:21 +01:00
|
|
|
if (!alloc_engine_) {
|
|
|
|
isc_throw(BadValue, "alloc_engine value must not be NULL"
|
|
|
|
" when creating an instance of the Dhcpv4Exchange");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!query_) {
|
|
|
|
isc_throw(BadValue, "query value must not be NULL when"
|
|
|
|
" creating an instance of the Dhcpv4Exchange");
|
2015-03-12 20:14:41 +01:00
|
|
|
}
|
2015-03-10 21:36:08 +01:00
|
|
|
// Create response message.
|
|
|
|
initResponse();
|
|
|
|
// Select subnet for the query message.
|
2015-03-12 20:14:41 +01:00
|
|
|
context_->subnet_ = subnet;
|
2015-03-10 16:52:21 +01:00
|
|
|
// Hardware address.
|
2015-03-10 21:36:08 +01:00
|
|
|
context_->hwaddr_ = query->getHWAddr();
|
2015-06-12 14:55:08 +02:00
|
|
|
// Pointer to client's query.
|
|
|
|
context_->query_ = query;
|
2015-05-12 19:01:27 +02:00
|
|
|
|
2017-09-18 12:11:51 +02:00
|
|
|
// If subnet found, retrieve client identifier which will be needed
|
|
|
|
// for allocations and search for reservations associated with a
|
|
|
|
// subnet/shared network.
|
2015-05-12 19:01:27 +02:00
|
|
|
if (subnet) {
|
2017-09-18 12:11:51 +02:00
|
|
|
OptionPtr opt_clientid = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
|
|
|
if (opt_clientid) {
|
|
|
|
context_->clientid_.reset(new ClientId(opt_clientid->getData()));
|
2015-05-12 19:01:27 +02:00
|
|
|
}
|
2016-04-13 08:56:28 +02:00
|
|
|
|
2016-04-27 09:22:29 +02:00
|
|
|
// Find static reservations if not disabled for our subnet.
|
2017-09-05 13:02:42 +02:00
|
|
|
if (subnet->getHostReservationMode() != Network::HR_DISABLED) {
|
2016-04-27 09:22:29 +02:00
|
|
|
// Before we can check for static reservations, we need to prepare a set
|
|
|
|
// of identifiers to be used for this.
|
|
|
|
setHostIdentifiers();
|
2016-04-13 08:56:28 +02:00
|
|
|
|
2016-04-27 09:22:29 +02:00
|
|
|
// Check for static reservations.
|
|
|
|
alloc_engine->findReservation(*context_);
|
2016-04-13 08:56:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-02 15:38:50 +02:00
|
|
|
|
2018-06-07 13:59:55 +02:00
|
|
|
// Set KNOWN builtin class if something was found, UNKNOWN if not.
|
|
|
|
if (!context_->hosts_.empty()) {
|
|
|
|
query->addClass("KNOWN");
|
2018-06-13 14:56:39 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg("KNOWN");
|
2018-06-07 13:59:55 +02:00
|
|
|
} else {
|
|
|
|
query->addClass("UNKNOWN");
|
2018-06-13 14:56:39 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg("UNKNOWN");
|
2016-04-13 08:56:28 +02:00
|
|
|
}
|
2016-09-02 15:38:50 +02:00
|
|
|
|
2018-06-07 13:59:55 +02:00
|
|
|
// Perform second pass of classification.
|
|
|
|
Dhcpv4Srv::evaluateClasses(query, true);
|
|
|
|
|
2016-09-02 15:38:50 +02:00
|
|
|
const ClientClasses& classes = query_->getClasses();
|
|
|
|
if (!classes.empty()) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
|
|
|
|
.arg(query_->getLabel())
|
2017-11-24 01:07:17 +01:00
|
|
|
.arg(classes.toText());
|
2016-09-02 15:38:50 +02:00
|
|
|
}
|
2015-03-10 16:52:21 +01:00
|
|
|
};
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Exchange::initResponse() {
|
2015-03-10 21:36:08 +01:00
|
|
|
uint8_t resp_type = 0;
|
|
|
|
switch (getQuery()->getType()) {
|
|
|
|
case DHCPDISCOVER:
|
|
|
|
resp_type = DHCPOFFER;
|
|
|
|
break;
|
|
|
|
case DHCPREQUEST:
|
|
|
|
case DHCPINFORM:
|
|
|
|
resp_type = DHCPACK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
2015-03-13 13:16:21 +01:00
|
|
|
// Only create a response if one is required.
|
2015-03-10 21:36:08 +01:00
|
|
|
if (resp_type > 0) {
|
|
|
|
resp_.reset(new Pkt4(resp_type, getQuery()->getTransid()));
|
2015-03-12 20:35:14 +01:00
|
|
|
copyDefaultFields();
|
2015-10-13 17:23:33 +02:00
|
|
|
copyDefaultOptions();
|
2016-06-17 02:24:29 +02:00
|
|
|
|
|
|
|
if (getQuery()->isDhcp4o6()) {
|
|
|
|
initResponse4o6();
|
|
|
|
}
|
2015-03-12 20:35:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 02:24:29 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Exchange::initResponse4o6() {
|
|
|
|
Pkt4o6Ptr query = boost::dynamic_pointer_cast<Pkt4o6>(getQuery());
|
|
|
|
if (!query) {
|
|
|
|
return;
|
2015-03-12 20:35:14 +01:00
|
|
|
}
|
2016-06-17 02:24:29 +02:00
|
|
|
const Pkt6Ptr& query6 = query->getPkt6();
|
|
|
|
Pkt6Ptr resp6(new Pkt6(DHCPV6_DHCPV4_RESPONSE, query6->getTransid()));
|
|
|
|
// Don't add client-id or server-id
|
|
|
|
// But copy relay info
|
|
|
|
if (!query6->relay_info_.empty()) {
|
|
|
|
resp6->copyRelayInfo(query6);
|
|
|
|
}
|
2017-12-23 22:59:15 +01:00
|
|
|
// Copy interface, and remote address and port
|
2016-06-17 02:24:29 +02:00
|
|
|
resp6->setIface(query6->getIface());
|
|
|
|
resp6->setIndex(query6->getIndex());
|
|
|
|
resp6->setRemoteAddr(query6->getRemoteAddr());
|
2017-12-23 22:59:15 +01:00
|
|
|
resp6->setRemotePort(query6->getRemotePort());
|
2016-06-17 02:24:29 +02:00
|
|
|
resp_.reset(new Pkt4o6(resp_, resp6));
|
2015-03-12 20:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Dhcpv4Exchange::copyDefaultFields() {
|
|
|
|
resp_->setIface(query_->getIface());
|
|
|
|
resp_->setIndex(query_->getIndex());
|
|
|
|
|
2015-03-13 13:16:21 +01:00
|
|
|
// explicitly set this to 0
|
|
|
|
resp_->setSiaddr(IOAddress::IPV4_ZERO_ADDRESS());
|
2015-03-12 20:35:14 +01:00
|
|
|
// ciaddr is always 0, except for the Renew/Rebind state when it may
|
|
|
|
// be set to the ciaddr sent by the client.
|
|
|
|
resp_->setCiaddr(IOAddress::IPV4_ZERO_ADDRESS());
|
|
|
|
resp_->setHops(query_->getHops());
|
|
|
|
|
|
|
|
// copy MAC address
|
|
|
|
resp_->setHWAddr(query_->getHWAddr());
|
|
|
|
|
|
|
|
// relay address
|
|
|
|
resp_->setGiaddr(query_->getGiaddr());
|
|
|
|
|
|
|
|
// If src/dest HW addresses are used by the packet filtering class
|
|
|
|
// we need to copy them as well. There is a need to check that the
|
|
|
|
// address being set is not-NULL because an attempt to set the NULL
|
|
|
|
// HW would result in exception. If these values are not set, the
|
|
|
|
// the default HW addresses (zeroed) should be generated by the
|
|
|
|
// packet filtering class when creating Ethernet header for
|
|
|
|
// outgoing packet.
|
|
|
|
HWAddrPtr src_hw_addr = query_->getLocalHWAddr();
|
|
|
|
if (src_hw_addr) {
|
|
|
|
resp_->setLocalHWAddr(src_hw_addr);
|
|
|
|
}
|
|
|
|
HWAddrPtr dst_hw_addr = query_->getRemoteHWAddr();
|
|
|
|
if (dst_hw_addr) {
|
|
|
|
resp_->setRemoteHWAddr(dst_hw_addr);
|
|
|
|
}
|
2016-02-24 20:51:53 -08:00
|
|
|
|
2016-03-10 19:41:14 -08:00
|
|
|
// Copy flags from the request to the response per RFC 2131
|
2016-02-24 20:51:53 -08:00
|
|
|
resp_->setFlags(query_->getFlags());
|
2015-10-13 17:14:34 +02:00
|
|
|
}
|
2015-03-12 20:35:14 +01:00
|
|
|
|
2015-10-13 17:14:34 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Exchange::copyDefaultOptions() {
|
2015-10-15 14:44:51 +02:00
|
|
|
// Let's copy client-id to response. See RFC6842.
|
|
|
|
// It is possible to disable RFC6842 to keep backward compatibility
|
2017-02-11 10:29:40 +01:00
|
|
|
bool echo = CfgMgr::instance().getCurrentCfg()->getEchoClientId();
|
2015-10-15 14:44:51 +02:00
|
|
|
OptionPtr client_id = query_->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
|
|
|
if (client_id && echo) {
|
|
|
|
resp_->addOption(client_id);
|
|
|
|
}
|
2015-03-12 20:35:14 +01:00
|
|
|
|
|
|
|
// If this packet is relayed, we want to copy Relay Agent Info option
|
2019-03-09 19:24:07 +01:00
|
|
|
// when it is not empty.
|
2015-03-12 20:35:14 +01:00
|
|
|
OptionPtr rai = query_->getOption(DHO_DHCP_AGENT_OPTIONS);
|
2019-03-09 19:24:07 +01:00
|
|
|
if (rai && (rai->len() > Option::OPTION4_HDR_LEN)) {
|
2015-03-12 20:35:14 +01:00
|
|
|
resp_->addOption(rai);
|
2015-03-10 21:36:08 +01:00
|
|
|
}
|
2015-10-13 17:14:34 +02:00
|
|
|
|
|
|
|
// RFC 3011 states about the Subnet Selection Option
|
|
|
|
|
|
|
|
// "Servers configured to support this option MUST return an
|
|
|
|
// identical copy of the option to any client that sends it,
|
|
|
|
// regardless of whether or not the client requests the option in
|
|
|
|
// a parameter request list. Clients using this option MUST
|
|
|
|
// discard DHCPOFFER or DHCPACK packets that do not contain this
|
|
|
|
// option."
|
|
|
|
OptionPtr subnet_sel = query_->getOption(DHO_SUBNET_SELECTION);
|
|
|
|
if (subnet_sel) {
|
|
|
|
resp_->addOption(subnet_sel);
|
|
|
|
}
|
2015-03-10 21:36:08 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 09:22:29 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Exchange::setHostIdentifiers() {
|
|
|
|
const ConstCfgHostOperationsPtr cfg =
|
|
|
|
CfgMgr::instance().getCurrentCfg()->getCfgHostOperations4();
|
2017-03-29 10:55:53 -05:00
|
|
|
|
2016-04-27 15:34:33 +02:00
|
|
|
// Collect host identifiers. The identifiers are stored in order of preference.
|
|
|
|
// The server will use them in that order to search for host reservations.
|
2016-04-27 09:22:29 +02:00
|
|
|
BOOST_FOREACH(const Host::IdentifierType& id_type,
|
|
|
|
cfg->getIdentifierTypes()) {
|
|
|
|
switch (id_type) {
|
|
|
|
case Host::IDENT_HWADDR:
|
|
|
|
if (context_->hwaddr_ && !context_->hwaddr_->hwaddr_.empty()) {
|
|
|
|
context_->addHostIdentifier(id_type, context_->hwaddr_->hwaddr_);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Host::IDENT_DUID:
|
|
|
|
if (context_->clientid_) {
|
|
|
|
const std::vector<uint8_t>& vec = context_->clientid_->getDuid();
|
|
|
|
if (!vec.empty()) {
|
|
|
|
// Client identifier type = DUID? Client identifier holding a DUID
|
|
|
|
// comprises Type (1 byte), IAID (4 bytes), followed by the actual
|
|
|
|
// DUID. Thus, the minimal length is 6.
|
|
|
|
if ((vec[0] == CLIENT_ID_OPTION_TYPE_DUID) && (vec.size() > 5)) {
|
|
|
|
// Extract DUID, skip IAID.
|
|
|
|
context_->addHostIdentifier(id_type,
|
|
|
|
std::vector<uint8_t>(vec.begin() + 5,
|
|
|
|
vec.end()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Host::IDENT_CIRCUIT_ID:
|
|
|
|
{
|
|
|
|
OptionPtr rai = query_->getOption(DHO_DHCP_AGENT_OPTIONS);
|
|
|
|
if (rai) {
|
|
|
|
OptionPtr circuit_id_opt = rai->getOption(RAI_OPTION_AGENT_CIRCUIT_ID);
|
|
|
|
if (circuit_id_opt) {
|
|
|
|
const OptionBuffer& circuit_id_vec = circuit_id_opt->getData();
|
|
|
|
if (!circuit_id_vec.empty()) {
|
|
|
|
context_->addHostIdentifier(id_type, circuit_id_vec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-06-03 13:43:08 +02:00
|
|
|
case Host::IDENT_CLIENT_ID:
|
|
|
|
if (context_->clientid_) {
|
|
|
|
const std::vector<uint8_t>& vec = context_->clientid_->getDuid();
|
|
|
|
if (!vec.empty()) {
|
|
|
|
context_->addHostIdentifier(id_type, vec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-03-28 23:56:01 -05:00
|
|
|
case Host::IDENT_FLEX:
|
2017-03-29 10:55:53 -05:00
|
|
|
{
|
|
|
|
if (!HooksManager::calloutsPresent(Hooks.hook_index_host4_identifier_)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:56:01 -05:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(context_->query_);
|
2016-06-03 13:43:08 +02:00
|
|
|
|
2017-03-28 23:56:01 -05:00
|
|
|
Host::IdentifierType type = Host::IDENT_FLEX;
|
|
|
|
std::vector<uint8_t> id;
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2017-03-28 23:56:01 -05:00
|
|
|
|
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("query4", context_->query_);
|
|
|
|
callout_handle->setArgument("id_type", type);
|
|
|
|
callout_handle->setArgument("id_value", id);
|
|
|
|
|
2017-03-29 10:55:53 -05:00
|
|
|
// Call callouts
|
|
|
|
HooksManager::callCallouts(Hooks.hook_index_host4_identifier_,
|
|
|
|
*callout_handle);
|
2017-03-28 23:56:01 -05:00
|
|
|
|
2017-03-29 10:55:53 -05:00
|
|
|
callout_handle->getArgument("id_type", type);
|
|
|
|
callout_handle->getArgument("id_value", id);
|
2017-03-28 23:56:01 -05:00
|
|
|
|
2017-03-29 10:55:53 -05:00
|
|
|
if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_CONTINUE) &&
|
|
|
|
!id.empty()) {
|
2017-03-28 23:56:01 -05:00
|
|
|
|
2017-03-29 10:55:53 -05:00
|
|
|
LOG_DEBUG(packet4_logger, DBGLVL_TRACE_BASIC, DHCP4_FLEX_ID)
|
|
|
|
.arg(Host::getIdentifierAsText(type, &id[0], id.size()));
|
2017-03-28 23:56:01 -05:00
|
|
|
|
2017-03-29 10:55:53 -05:00
|
|
|
context_->addHostIdentifier(type, id);
|
2017-03-28 23:56:01 -05:00
|
|
|
}
|
|
|
|
break;
|
2017-03-29 10:55:53 -05:00
|
|
|
}
|
2016-04-27 09:22:29 +02:00
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 15:38:50 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Exchange::setReservedClientClasses() {
|
2017-09-11 18:47:30 +02:00
|
|
|
if (context_->currentHost() && query_) {
|
2017-11-24 01:07:17 +01:00
|
|
|
const ClientClasses& classes = context_->currentHost()->getClientClasses4();
|
|
|
|
for (ClientClasses::const_iterator cclass = classes.cbegin();
|
|
|
|
cclass != classes.cend(); ++cclass) {
|
|
|
|
query_->addClass(*cclass);
|
2016-09-02 15:38:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 16:59:21 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Exchange::setReservedMessageFields() {
|
2017-09-11 18:47:30 +02:00
|
|
|
ConstHostPtr host = context_->currentHost();
|
2016-08-22 16:59:21 +02:00
|
|
|
// Nothing to do if host reservations not specified for this client.
|
|
|
|
if (host) {
|
|
|
|
if (!host->getNextServer().isV4Zero()) {
|
|
|
|
resp_->setSiaddr(host->getNextServer());
|
|
|
|
}
|
|
|
|
|
2017-08-21 14:51:33 +02:00
|
|
|
std::string sname = host->getServerHostname();
|
|
|
|
if (!sname.empty()) {
|
|
|
|
resp_->setSname(reinterpret_cast<const uint8_t*>(sname.c_str()),
|
|
|
|
sname.size());
|
2016-08-22 16:59:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 14:51:33 +02:00
|
|
|
std::string bootfile = host->getBootFileName();
|
|
|
|
if (!bootfile.empty()) {
|
|
|
|
resp_->setFile(reinterpret_cast<const uint8_t*>(bootfile.c_str()),
|
|
|
|
bootfile.size());
|
2016-08-22 16:59:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 13:49:50 +01:00
|
|
|
const std::string Dhcpv4Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");
|
|
|
|
|
2018-12-27 19:12:10 +01:00
|
|
|
Dhcpv4Srv::Dhcpv4Srv(uint16_t server_port, uint16_t client_port,
|
|
|
|
const bool use_bcast, const bool direct_response_desired)
|
2018-12-27 16:15:30 +01:00
|
|
|
: io_service_(new IOService()), shutdown_(true), alloc_engine_(),
|
|
|
|
server_port_(server_port), use_bcast_(use_bcast),
|
2018-12-27 19:12:10 +01:00
|
|
|
client_port_(client_port),
|
2019-03-19 13:26:00 +01:00
|
|
|
network_state_(new NetworkState(NetworkState::DHCPv4)),
|
|
|
|
cb_control_(new CBControlDHCPv4()) {
|
2013-07-22 18:46:54 +02:00
|
|
|
|
2018-12-27 16:15:30 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET)
|
|
|
|
.arg(server_port);
|
2012-07-23 16:35:54 +02:00
|
|
|
try {
|
2014-07-01 15:31:10 +02:00
|
|
|
// Port 0 is used for testing purposes where we don't open broadcast
|
|
|
|
// capable sockets. So, set the packet filter handling direct traffic
|
|
|
|
// only if we are in non-test mode.
|
2018-12-27 16:15:30 +01:00
|
|
|
if (server_port) {
|
2014-01-03 16:54:16 +01:00
|
|
|
// First call to instance() will create IfaceMgr (it's a singleton)
|
|
|
|
// it may throw something if things go wrong.
|
|
|
|
// The 'true' value of the call to setMatchingPacketFilter imposes
|
|
|
|
// that IfaceMgr will try to use the mechanism to respond directly
|
|
|
|
// to the client which doesn't have address assigned. This capability
|
|
|
|
// may be lacking on some OSes, so there is no guarantee that server
|
|
|
|
// will be able to respond directly.
|
|
|
|
IfaceMgr::instance().setMatchingPacketFilter(direct_response_desired);
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2015-07-19 13:57:55 +02:00
|
|
|
// Instantiate allocation engine. The number of allocation attempts equal
|
|
|
|
// to zero indicates that the allocation engine will use the number of
|
|
|
|
// attempts depending on the pool size.
|
|
|
|
alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 0,
|
2013-09-16 14:35:46 +02:00
|
|
|
false /* false = IPv4 */));
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-07-15 18:41:19 +02:00
|
|
|
/// @todo call loadLibraries() when handling configuration changes
|
|
|
|
|
2012-07-23 16:35:54 +02:00
|
|
|
} catch (const std::exception &e) {
|
2012-09-06 11:18:51 +01:00
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_SRV_CONSTRUCT_ERROR).arg(e.what());
|
2012-07-23 16:35:54 +02:00
|
|
|
shutdown_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2019-09-04 14:49:33 +02:00
|
|
|
// Initializing all observations with default value
|
|
|
|
setPacketStatisticsDefaults();
|
2011-12-12 18:04:12 +01:00
|
|
|
shutdown_ = false;
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 14:49:33 +02:00
|
|
|
void Dhcpv4Srv::setPacketStatisticsDefaults() {
|
|
|
|
isc::stats::StatsMgr& stats_mgr = isc::stats::StatsMgr::instance();
|
|
|
|
|
|
|
|
// Iterate over set of observed statistics
|
2019-09-05 17:24:45 +02:00
|
|
|
for (auto it = dhcp4_statistics.begin(); it != dhcp4_statistics.end(); ++it) {
|
2019-09-04 14:49:33 +02:00
|
|
|
// Initialize them with default value 0
|
|
|
|
stats_mgr.setValue((*it), static_cast<int64_t>(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-27 19:35:47 +08:00
|
|
|
Dhcpv4Srv::~Dhcpv4Srv() {
|
2018-05-15 15:56:18 -04:00
|
|
|
// Discard any cached packets or parked packets
|
2018-05-16 14:42:55 +02:00
|
|
|
discardPackets();
|
2018-05-15 15:56:18 -04:00
|
|
|
|
2015-08-12 15:50:47 -04:00
|
|
|
try {
|
|
|
|
stopD2();
|
|
|
|
} catch(const std::exception& ex) {
|
|
|
|
// Highly unlikely, but lets Report it but go on
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_SRV_D2STOP_ERROR).arg(ex.what());
|
|
|
|
}
|
2015-08-12 16:28:55 -04:00
|
|
|
|
2016-06-17 02:24:29 +02:00
|
|
|
try {
|
|
|
|
Dhcp4to6Ipc::instance().close();
|
|
|
|
} catch(const std::exception& ex) {
|
|
|
|
// Highly unlikely, but lets Report it but go on
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_SRV_DHCP4O6_ERROR).arg(ex.what());
|
|
|
|
}
|
|
|
|
|
2011-12-12 19:24:36 +01:00
|
|
|
IfaceMgr::instance().closeSockets();
|
2015-10-08 23:20:49 +02:00
|
|
|
|
2015-10-15 10:34:01 +02:00
|
|
|
// The lease manager was instantiated during DHCPv4Srv configuration,
|
|
|
|
// so we should clean up after ourselves.
|
2015-10-08 23:20:49 +02:00
|
|
|
LeaseMgrFactory::destroy();
|
2016-04-29 10:24:36 -04:00
|
|
|
|
|
|
|
// Explicitly unload hooks
|
|
|
|
HooksManager::getHooksManager().unloadLibraries();
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::shutdown() {
|
2012-09-06 11:18:51 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_SHUTDOWN_REQUEST);
|
2012-05-30 18:36:58 +02:00
|
|
|
shutdown_ = true;
|
|
|
|
}
|
|
|
|
|
2015-03-10 16:52:21 +01:00
|
|
|
isc::dhcp::Subnet4Ptr
|
2018-05-02 22:00:34 +02:00
|
|
|
Dhcpv4Srv::selectSubnet(const Pkt4Ptr& query, bool& drop,
|
|
|
|
bool sanity_only) const {
|
2015-03-12 20:14:41 +01:00
|
|
|
|
2016-06-17 02:24:29 +02:00
|
|
|
// DHCPv4-over-DHCPv6 is a special (and complex) case
|
|
|
|
if (query->isDhcp4o6()) {
|
2018-05-02 22:00:34 +02:00
|
|
|
return (selectSubnet4o6(query, drop, sanity_only));
|
2016-06-17 02:24:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
Subnet4Ptr subnet;
|
|
|
|
|
2018-05-11 16:35:04 +02:00
|
|
|
const SubnetSelector& selector = CfgSubnets4::initSelector(query);
|
2015-09-19 20:08:56 +02:00
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
CfgMgr& cfgmgr = CfgMgr::instance();
|
|
|
|
subnet = cfgmgr.getCurrentCfg()->getCfgSubnets4()->selectSubnet(selector);
|
|
|
|
|
|
|
|
// Let's execute all callouts registered for subnet4_select
|
2018-05-07 17:25:17 +02:00
|
|
|
// (skip callouts if the selectSubnet was called to do sanity checks only)
|
2018-05-02 22:00:34 +02:00
|
|
|
if (!sanity_only &&
|
|
|
|
HooksManager::calloutsPresent(Hooks.hook_index_subnet4_select_)) {
|
2015-03-12 20:14:41 +01:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2015-03-12 20:14:41 +01:00
|
|
|
|
2016-07-06 12:28:49 +02:00
|
|
|
// Enable copying options from the packet within hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);
|
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
// Set new arguments
|
|
|
|
callout_handle->setArgument("query4", query);
|
|
|
|
callout_handle->setArgument("subnet4", subnet);
|
|
|
|
callout_handle->setArgument("subnet4collection",
|
|
|
|
cfgmgr.getCurrentCfg()->
|
|
|
|
getCfgSubnets4()->getAll());
|
|
|
|
|
|
|
|
// Call user (and server-side) callouts
|
2016-06-30 19:37:58 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_subnet4_select_,
|
2015-03-12 20:14:41 +01:00
|
|
|
*callout_handle);
|
|
|
|
|
|
|
|
// Callouts decided to skip this step. This means that no subnet
|
|
|
|
// will be selected. Packet processing will continue, but it will
|
|
|
|
// be severely limited (i.e. only global options will be assigned)
|
2015-10-06 13:34:27 +02:00
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
|
2015-05-15 14:27:46 +02:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
2015-05-05 19:34:13 +02:00
|
|
|
DHCP4_HOOK_SUBNET4_SELECT_SKIP)
|
|
|
|
.arg(query->getLabel());
|
2015-03-12 20:14:41 +01:00
|
|
|
return (Subnet4Ptr());
|
|
|
|
}
|
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Callouts decided to drop the packet. It is a superset of the
|
2017-12-15 18:30:59 +01:00
|
|
|
// skip case so no subnet will be selected.
|
2017-12-15 00:53:20 +01:00
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
|
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_SUBNET4_SELECT_DROP)
|
|
|
|
.arg(query->getLabel());
|
2017-12-15 18:30:59 +01:00
|
|
|
drop = true;
|
2017-12-15 00:53:20 +01:00
|
|
|
return (Subnet4Ptr());
|
|
|
|
}
|
2015-10-06 13:34:27 +02:00
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
// Use whatever subnet was specified by the callout
|
|
|
|
callout_handle->getArgument("subnet4", subnet);
|
|
|
|
}
|
|
|
|
|
2015-05-05 19:34:13 +02:00
|
|
|
if (subnet) {
|
|
|
|
// Log at higher debug level that subnet has been found.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA, DHCP4_SUBNET_SELECTED)
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(subnet->getID());
|
|
|
|
// Log detailed information about the selected subnet at the
|
|
|
|
// lower debug level.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_SUBNET_DATA)
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(subnet->toText());
|
|
|
|
|
|
|
|
} else {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL,
|
2015-05-05 19:34:13 +02:00
|
|
|
DHCP4_SUBNET_SELECTION_FAILED)
|
|
|
|
.arg(query->getLabel());
|
|
|
|
}
|
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
return (subnet);
|
2015-03-10 16:52:21 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 02:24:29 +02:00
|
|
|
isc::dhcp::Subnet4Ptr
|
2018-05-02 22:00:34 +02:00
|
|
|
Dhcpv4Srv::selectSubnet4o6(const Pkt4Ptr& query, bool& drop,
|
|
|
|
bool sanity_only) const {
|
2016-06-17 02:24:29 +02:00
|
|
|
|
|
|
|
Subnet4Ptr subnet;
|
|
|
|
|
|
|
|
SubnetSelector selector;
|
|
|
|
selector.ciaddr_ = query->getCiaddr();
|
|
|
|
selector.giaddr_ = query->getGiaddr();
|
|
|
|
selector.local_address_ = query->getLocalAddr();
|
|
|
|
selector.client_classes_ = query->classes_;
|
|
|
|
selector.iface_name_ = query->getIface();
|
|
|
|
// Mark it as DHCPv4-over-DHCPv6
|
|
|
|
selector.dhcp4o6_ = true;
|
|
|
|
// Now the DHCPv6 part
|
|
|
|
selector.remote_address_ = query->getRemoteAddr();
|
|
|
|
selector.first_relay_linkaddr_ = IOAddress("::");
|
|
|
|
|
|
|
|
// Handle a DHCPv6 relayed query
|
|
|
|
Pkt4o6Ptr query4o6 = boost::dynamic_pointer_cast<Pkt4o6>(query);
|
|
|
|
if (!query4o6) {
|
2016-06-17 18:09:41 +02:00
|
|
|
isc_throw(Unexpected, "Can't get DHCP4o6 message");
|
2016-06-17 02:24:29 +02:00
|
|
|
}
|
|
|
|
const Pkt6Ptr& query6 = query4o6->getPkt6();
|
|
|
|
|
|
|
|
// Initialize fields specific to relayed messages.
|
|
|
|
if (query6 && !query6->relay_info_.empty()) {
|
|
|
|
BOOST_REVERSE_FOREACH(Pkt6::RelayInfo relay, query6->relay_info_) {
|
|
|
|
if (!relay.linkaddr_.isV6Zero() &&
|
|
|
|
!relay.linkaddr_.isV6LinkLocal()) {
|
|
|
|
selector.first_relay_linkaddr_ = relay.linkaddr_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selector.interface_id_ =
|
|
|
|
query6->getAnyRelayOption(D6O_INTERFACE_ID,
|
2016-06-17 18:09:41 +02:00
|
|
|
Pkt6::RELAY_GET_FIRST);
|
2016-06-17 02:24:29 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:08:35 +02:00
|
|
|
// If the Subnet Selection option is present, extract its value.
|
|
|
|
OptionPtr sbnsel = query->getOption(DHO_SUBNET_SELECTION);
|
|
|
|
if (sbnsel) {
|
|
|
|
OptionCustomPtr oc = boost::dynamic_pointer_cast<OptionCustom>(sbnsel);
|
|
|
|
if (oc) {
|
|
|
|
selector.option_select_ = oc->readAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 02:24:29 +02:00
|
|
|
CfgMgr& cfgmgr = CfgMgr::instance();
|
2016-06-30 00:08:35 +02:00
|
|
|
subnet = cfgmgr.getCurrentCfg()->getCfgSubnets4()->selectSubnet4o6(selector);
|
2016-06-17 02:24:29 +02:00
|
|
|
|
2018-05-07 17:25:17 +02:00
|
|
|
// Let's execute all callouts registered for subnet4_select.
|
|
|
|
// (skip callouts if the selectSubnet was called to do sanity checks only)
|
2018-05-02 22:00:34 +02:00
|
|
|
if (!sanity_only &&
|
|
|
|
HooksManager::calloutsPresent(Hooks.hook_index_subnet4_select_)) {
|
2015-03-12 20:14:41 +01:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2015-03-12 20:14:41 +01:00
|
|
|
|
|
|
|
// Set new arguments
|
|
|
|
callout_handle->setArgument("query4", query);
|
|
|
|
callout_handle->setArgument("subnet4", subnet);
|
|
|
|
callout_handle->setArgument("subnet4collection",
|
|
|
|
cfgmgr.getCurrentCfg()->
|
|
|
|
getCfgSubnets4()->getAll());
|
|
|
|
|
|
|
|
// Call user (and server-side) callouts
|
2016-06-30 19:37:58 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_subnet4_select_,
|
2015-03-12 20:14:41 +01:00
|
|
|
*callout_handle);
|
|
|
|
|
|
|
|
// Callouts decided to skip this step. This means that no subnet
|
|
|
|
// will be selected. Packet processing will continue, but it will
|
|
|
|
// be severely limited (i.e. only global options will be assigned)
|
2015-10-06 13:34:27 +02:00
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
|
2015-05-15 14:27:46 +02:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
2015-05-05 19:34:13 +02:00
|
|
|
DHCP4_HOOK_SUBNET4_SELECT_SKIP)
|
|
|
|
.arg(query->getLabel());
|
2015-03-12 20:14:41 +01:00
|
|
|
return (Subnet4Ptr());
|
|
|
|
}
|
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Callouts decided to drop the packet. It is a superset of the
|
2017-12-15 18:30:59 +01:00
|
|
|
// skip case so no subnet will be selected.
|
2017-12-15 00:53:20 +01:00
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
|
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_SUBNET4_SELECT_DROP)
|
|
|
|
.arg(query->getLabel());
|
2017-12-15 18:30:59 +01:00
|
|
|
drop = true;
|
2017-12-15 00:53:20 +01:00
|
|
|
return (Subnet4Ptr());
|
|
|
|
}
|
2015-10-06 13:34:27 +02:00
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
// Use whatever subnet was specified by the callout
|
|
|
|
callout_handle->getArgument("subnet4", subnet);
|
|
|
|
}
|
|
|
|
|
2015-05-05 19:34:13 +02:00
|
|
|
if (subnet) {
|
|
|
|
// Log at higher debug level that subnet has been found.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA, DHCP4_SUBNET_SELECTED)
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(subnet->getID());
|
|
|
|
// Log detailed information about the selected subnet at the
|
|
|
|
// lower debug level.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_SUBNET_DATA)
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(subnet->toText());
|
|
|
|
|
|
|
|
} else {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL,
|
2015-05-05 19:34:13 +02:00
|
|
|
DHCP4_SUBNET_SELECTION_FAILED)
|
|
|
|
.arg(query->getLabel());
|
|
|
|
}
|
|
|
|
|
2015-03-12 20:14:41 +01:00
|
|
|
return (subnet);
|
2015-03-10 16:52:21 +01:00
|
|
|
}
|
|
|
|
|
2013-07-19 15:54:02 +02:00
|
|
|
Pkt4Ptr
|
|
|
|
Dhcpv4Srv::receivePacket(int timeout) {
|
2013-07-15 18:41:19 +02:00
|
|
|
return (IfaceMgr::instance().receive4(timeout));
|
|
|
|
}
|
|
|
|
|
2013-07-19 15:54:02 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::sendPacket(const Pkt4Ptr& packet) {
|
2013-07-15 18:41:19 +02:00
|
|
|
IfaceMgr::instance().send(packet);
|
|
|
|
}
|
|
|
|
|
2011-11-27 19:35:47 +08:00
|
|
|
bool
|
|
|
|
Dhcpv4Srv::run() {
|
2011-12-12 18:04:12 +01:00
|
|
|
while (!shutdown_) {
|
2015-08-17 20:21:00 +02:00
|
|
|
try {
|
2016-01-22 19:43:16 +01:00
|
|
|
run_one();
|
2017-06-21 11:40:38 +02:00
|
|
|
getIOService()->poll();
|
2015-08-17 20:21:00 +02:00
|
|
|
} catch (const std::exception& e) {
|
2015-11-01 10:39:10 +09:00
|
|
|
// General catch-all exception that are not caught by more specific
|
|
|
|
// catches. This one is for exceptions derived from std::exception.
|
2016-02-20 14:15:17 +01:00
|
|
|
LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_STD_EXCEPTION)
|
2015-08-17 20:21:00 +02:00
|
|
|
.arg(e.what());
|
2015-11-01 10:39:10 +09:00
|
|
|
} catch (...) {
|
|
|
|
// General catch-all exception that are not caught by more specific
|
|
|
|
// catches. This one is for other exceptions, not derived from
|
|
|
|
// std::exception.
|
2016-02-24 14:51:20 +01:00
|
|
|
LOG_ERROR(packet4_logger, DHCP4_PACKET_PROCESS_EXCEPTION);
|
2015-08-17 20:21:00 +02:00
|
|
|
}
|
2015-11-01 10:39:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
2014-06-03 12:08:23 +02:00
|
|
|
|
2016-01-22 19:43:16 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::run_one() {
|
|
|
|
// client's message and server's response
|
|
|
|
Pkt4Ptr query;
|
|
|
|
Pkt4Ptr rsp;
|
2015-05-15 15:54:08 +02:00
|
|
|
|
2016-01-22 19:43:16 +01:00
|
|
|
try {
|
2017-06-21 11:40:38 +02:00
|
|
|
// Set select() timeout to 1s. This value should not be modified
|
|
|
|
// because it is important that the select() returns control
|
|
|
|
// frequently so as the IOService can be polled for ready handlers.
|
|
|
|
uint32_t timeout = 1;
|
2016-01-22 19:43:16 +01:00
|
|
|
query = receivePacket(timeout);
|
|
|
|
|
|
|
|
// Log if packet has arrived. We can't log the detailed information
|
|
|
|
// about the DHCP message because it hasn't been unpacked/parsed
|
|
|
|
// yet, and it can't be parsed at this point because hooks will
|
|
|
|
// have to process it first. The only information available at this
|
|
|
|
// point are: the interface, source address and destination addresses
|
|
|
|
// and ports.
|
|
|
|
if (query) {
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC, DHCP4_BUFFER_RECEIVED)
|
|
|
|
.arg(query->getRemoteAddr().toText())
|
|
|
|
.arg(query->getRemotePort())
|
|
|
|
.arg(query->getLocalAddr().toText())
|
|
|
|
.arg(query->getLocalPort())
|
|
|
|
.arg(query->getIface());
|
2013-10-04 21:17:07 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
2017-09-04 16:02:27 +02:00
|
|
|
// We used to log that the wait was interrupted, but this is no longer
|
|
|
|
// the case. Our wait time is 1s now, so the lack of query packet more
|
|
|
|
// likely means that nothing new appeared within a second, rather than
|
|
|
|
// we were interrupted. And we don't want to print a message every
|
|
|
|
// second.
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2018-05-31 18:22:05 +02:00
|
|
|
} catch (const SignalInterruptOnSelect&) {
|
2016-01-22 19:43:16 +01:00
|
|
|
// Packet reception interrupted because a signal has been received.
|
|
|
|
// This is not an error because we might have received a SIGTERM,
|
2017-07-23 16:42:36 -04:00
|
|
|
// SIGINT, SIGHUP or SIGCHLD which are handled by the server. For
|
2016-01-22 19:43:16 +01:00
|
|
|
// signals that are not handled by the server we rely on the default
|
|
|
|
// behavior of the system.
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL, DHCP4_BUFFER_WAIT_SIGNAL)
|
|
|
|
.arg(signal_set_->getNext());
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
// Log all other errors.
|
|
|
|
LOG_ERROR(packet4_logger, DHCP4_BUFFER_RECEIVE_FAIL).arg(e.what());
|
|
|
|
}
|
2015-05-15 15:54:08 +02:00
|
|
|
|
2016-01-22 19:43:16 +01:00
|
|
|
// Handle next signal received by the process. It must be called after
|
|
|
|
// an attempt to receive a packet to properly handle server shut down.
|
|
|
|
// The SIGTERM or SIGINT will be received prior to, or during execution
|
|
|
|
// of select() (select is invoked by receivePacket()). When that
|
|
|
|
// happens, select will be interrupted. The signal handler will be
|
|
|
|
// invoked immediately after select(). The handler will set the
|
|
|
|
// shutdown flag and cause the process to terminate before the next
|
|
|
|
// select() function is called. If the function was called before
|
|
|
|
// receivePacket the process could wait up to the duration of timeout
|
|
|
|
// of select() to terminate.
|
|
|
|
try {
|
|
|
|
handleSignal();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
// Standard exception occurred. Let's be on the safe side to
|
|
|
|
// catch std::exception.
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_HANDLE_SIGNAL_EXCEPTION)
|
|
|
|
.arg(e.what());
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2016-01-22 19:43:16 +01:00
|
|
|
// Timeout may be reached or signal received, which breaks select()
|
|
|
|
// with no reception occurred. No need to log anything here because
|
|
|
|
// we have logged right after the call to receivePacket().
|
|
|
|
if (!query) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2017-12-01 17:13:11 +01:00
|
|
|
// If the DHCP service has been globally disabled, drop the packet.
|
2018-03-22 12:26:58 +01:00
|
|
|
if (!network_state_->isServiceEnabled()) {
|
2017-12-05 11:37:04 +01:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_BASIC,
|
|
|
|
DHCP4_PACKET_DROP_0008)
|
|
|
|
.arg(query->getLabel());
|
2017-12-16 00:45:06 +01:00
|
|
|
return;
|
2017-12-11 11:29:27 +01:00
|
|
|
} else {
|
2017-12-01 17:13:11 +01:00
|
|
|
processPacket(query, rsp);
|
|
|
|
}
|
2013-10-04 21:17:07 +02:00
|
|
|
|
2016-02-24 14:51:20 +01:00
|
|
|
if (!rsp) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2018-01-22 17:50:05 +01:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
|
|
|
processPacketBufferSend(callout_handle, rsp);
|
2016-02-24 14:00:14 +01:00
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2015-11-01 10:39:10 +09:00
|
|
|
void
|
2018-01-22 17:50:05 +01:00
|
|
|
Dhcpv4Srv::processPacket(Pkt4Ptr& query, Pkt4Ptr& rsp, bool allow_packet_park) {
|
2015-11-01 10:43:42 +09:00
|
|
|
// Log reception of the packet. We need to increase it early, as any
|
|
|
|
// failures in unpacking will cause the packet to be dropped. We
|
2016-02-24 14:00:14 +01:00
|
|
|
// will increase type specific statistic further down the road.
|
2015-11-01 10:43:42 +09:00
|
|
|
// See processStatsReceived().
|
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-received",
|
|
|
|
static_cast<int64_t>(1));
|
|
|
|
|
|
|
|
bool skip_unpack = false;
|
|
|
|
|
|
|
|
// The packet has just been received so contains the uninterpreted wire
|
|
|
|
// data; execute callouts registered for buffer4_receive.
|
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_buffer4_receive_)) {
|
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2016-07-06 12:28:49 +02:00
|
|
|
// Enable copying options from the packet within hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("query4", query);
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Call callouts
|
|
|
|
HooksManager::callCallouts(Hooks.hook_index_buffer4_receive_,
|
|
|
|
*callout_handle);
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Callouts decided to drop the received packet.
|
|
|
|
// The response (rsp) is null so the caller (run_one) will
|
|
|
|
// immediately return too.
|
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
|
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_HOOK_BUFFER_RCVD_DROP)
|
|
|
|
.arg(query->getRemoteAddr().toText())
|
|
|
|
.arg(query->getLocalAddr().toText())
|
|
|
|
.arg(query->getIface());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Callouts decided to skip the next processing step. The next
|
|
|
|
// processing step would to parse the packet, so skip at this
|
|
|
|
// stage means that callouts did the parsing already, so server
|
|
|
|
// should skip parsing.
|
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
|
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_HOOK_BUFFER_RCVD_SKIP)
|
|
|
|
.arg(query->getRemoteAddr().toText())
|
|
|
|
.arg(query->getLocalAddr().toText())
|
|
|
|
.arg(query->getIface());
|
|
|
|
skip_unpack = true;
|
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
callout_handle->getArgument("query4", query);
|
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Unpack the packet information unless the buffer4_receive callouts
|
|
|
|
// indicated they did it
|
|
|
|
if (!skip_unpack) {
|
2013-08-19 15:08:08 +02:00
|
|
|
try {
|
2015-11-01 10:43:42 +09:00
|
|
|
LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL, DHCP4_BUFFER_UNPACK)
|
|
|
|
.arg(query->getRemoteAddr().toText())
|
|
|
|
.arg(query->getLocalAddr().toText())
|
|
|
|
.arg(query->getIface());
|
|
|
|
query->unpack();
|
2018-02-27 14:46:40 -05:00
|
|
|
} catch (const SkipRemainingOptionsError& e) {
|
|
|
|
// An option failed to unpack but we are to attempt to process it
|
|
|
|
// anyway. Log it and let's hope for the best.
|
|
|
|
LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_PACKET_OPTIONS_SKIPPED)
|
|
|
|
.arg(e.what());
|
2015-08-17 20:49:39 +02:00
|
|
|
} catch (const std::exception& e) {
|
2015-11-01 10:43:42 +09:00
|
|
|
// Failed to parse the packet.
|
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_PACKET_DROP_0001)
|
|
|
|
.arg(query->getRemoteAddr().toText())
|
|
|
|
.arg(query->getLocalAddr().toText())
|
|
|
|
.arg(query->getIface())
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(e.what());
|
2015-05-18 20:31:06 +02:00
|
|
|
|
2017-01-24 23:18:58 +01:00
|
|
|
// Increase the statistics of parse failures and dropped packets.
|
2015-11-01 10:43:42 +09:00
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-parse-failed",
|
|
|
|
static_cast<int64_t>(1));
|
2015-06-09 16:48:51 +02:00
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
|
2015-06-16 14:33:49 +02:00
|
|
|
static_cast<int64_t>(1));
|
2015-11-01 10:43:42 +09:00
|
|
|
return;
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
2015-11-01 10:43:42 +09:00
|
|
|
}
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Update statistics accordingly for received packet.
|
|
|
|
processStatsReceived(query);
|
2013-05-21 15:18:58 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Assign this packet to one or more classes if needed. We need to do
|
|
|
|
// this before calling accept(), because getSubnet4() may need client
|
|
|
|
// class information.
|
|
|
|
classifyPacket(query);
|
2013-10-18 11:10:54 +02:00
|
|
|
|
2017-09-20 21:33:52 +02:00
|
|
|
// Now it is classified the deferred unpacking can be done.
|
|
|
|
deferredUnpack(query);
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Check whether the message should be further processed or discarded.
|
|
|
|
// There is no need to log anything here. This function logs by itself.
|
|
|
|
if (!accept(query)) {
|
|
|
|
// Increase the statistic of dropped packets.
|
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
|
|
|
|
static_cast<int64_t>(1));
|
|
|
|
return;
|
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// We have sanity checked (in accept() that the Message Type option
|
|
|
|
// exists, so we can safely get it here.
|
|
|
|
int type = query->getType();
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA, DHCP4_PACKET_RECEIVED)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(query->getName())
|
|
|
|
.arg(type)
|
|
|
|
.arg(query->getRemoteAddr())
|
|
|
|
.arg(query->getLocalAddr())
|
|
|
|
.arg(query->getIface());
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_QUERY_DATA)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(query->toText());
|
|
|
|
|
|
|
|
// Let's execute all callouts registered for pkt4_receive
|
2016-06-30 00:08:35 +02:00
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_pkt4_receive_)) {
|
2015-11-01 10:43:42 +09:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2016-07-06 12:28:49 +02:00
|
|
|
// Enable copying options from the packet within hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("query4", query);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Call callouts
|
2016-06-30 00:08:35 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_pkt4_receive_,
|
2015-11-01 10:43:42 +09:00
|
|
|
*callout_handle);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Callouts decided to skip the next processing step. The next
|
|
|
|
// processing step would to process the packet, so skip at this
|
|
|
|
// stage means drop.
|
2017-12-15 00:53:20 +01:00
|
|
|
if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
|
|
|
|
(callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
|
2015-11-01 10:43:42 +09:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_PACKET_RCVD_SKIP)
|
|
|
|
.arg(query->getLabel());
|
2015-11-01 10:39:10 +09:00
|
|
|
return;
|
2013-08-20 14:02:27 +02:00
|
|
|
}
|
2015-04-01 09:22:08 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
callout_handle->getArgument("query4", query);
|
|
|
|
}
|
2015-10-06 13:34:27 +02:00
|
|
|
|
2019-06-11 15:45:50 +02:00
|
|
|
// Check the DROP special class.
|
|
|
|
if (query->inClass("DROP")) {
|
2019-07-16 14:05:40 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBGLVL_TRACE_BASIC, DHCP4_PACKET_DROP_0010)
|
2019-06-11 15:45:50 +02:00
|
|
|
.arg(query->toText());
|
2019-07-15 19:27:04 +02:00
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
|
|
|
|
static_cast<int64_t>(1));
|
2019-06-11 15:45:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:02:07 +01:00
|
|
|
AllocEngine::ClientContext4Ptr ctx;
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
try {
|
|
|
|
switch (query->getType()) {
|
|
|
|
case DHCPDISCOVER:
|
|
|
|
rsp = processDiscover(query);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPREQUEST:
|
|
|
|
// Note that REQUEST is used for many things in DHCPv4: for
|
|
|
|
// requesting new leases, renewing existing ones and even
|
|
|
|
// for rebinding.
|
2018-01-18 20:02:07 +01:00
|
|
|
rsp = processRequest(query, ctx);
|
2015-11-01 10:43:42 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPRELEASE:
|
2018-01-18 20:02:07 +01:00
|
|
|
processRelease(query, ctx);
|
2015-11-01 10:43:42 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPDECLINE:
|
2018-01-18 20:02:07 +01:00
|
|
|
processDecline(query, ctx);
|
2015-11-01 10:43:42 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPINFORM:
|
|
|
|
rsp = processInform(query);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Only action is to output a message if debug is enabled,
|
|
|
|
// and that is covered by the debug statement before the
|
|
|
|
// "switch" statement.
|
|
|
|
;
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
2015-11-01 10:43:42 +09:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
|
|
|
// Catch-all exception (we used to call only isc::Exception, but
|
|
|
|
// std::exception could potentially be raised and if we don't catch
|
|
|
|
// it here, it would be caught in main() and the process would
|
|
|
|
// terminate). Just log the problem and ignore the packet.
|
|
|
|
// (The problem is logged as a debug message because debug is
|
|
|
|
// disabled by default - it prevents a DDOS attack based on the
|
|
|
|
// sending of problem packets.)
|
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_BASIC,
|
|
|
|
DHCP4_PACKET_DROP_0007)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(e.what());
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Increase the statistic of dropped packets.
|
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-receive-drop",
|
|
|
|
static_cast<int64_t>(1));
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2018-01-19 17:15:23 +01:00
|
|
|
bool packet_park = false;
|
|
|
|
|
2018-01-18 20:02:07 +01:00
|
|
|
if (ctx && HooksManager::calloutsPresent(Hooks.hook_index_leases4_committed_)) {
|
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2018-01-18 20:02:07 +01:00
|
|
|
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query4_options_copy(query);
|
|
|
|
|
|
|
|
// Also pass the corresponding query packet as argument
|
|
|
|
callout_handle->setArgument("query4", query);
|
|
|
|
|
|
|
|
Lease4CollectionPtr new_leases(new Lease4Collection());
|
|
|
|
if (ctx->new_lease_) {
|
|
|
|
new_leases->push_back(ctx->new_lease_);
|
|
|
|
}
|
|
|
|
callout_handle->setArgument("leases4", new_leases);
|
|
|
|
|
2018-01-30 13:26:14 +01:00
|
|
|
Lease4CollectionPtr deleted_leases(new Lease4Collection());
|
2018-01-18 20:02:07 +01:00
|
|
|
if (ctx->old_lease_) {
|
|
|
|
if ((!ctx->new_lease_) || (ctx->new_lease_->addr_ != ctx->old_lease_->addr_)) {
|
|
|
|
deleted_leases->push_back(ctx->old_lease_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callout_handle->setArgument("deleted_leases4", deleted_leases);
|
2018-01-22 08:45:39 +01:00
|
|
|
|
2018-01-18 20:02:07 +01:00
|
|
|
// Call all installed callouts
|
|
|
|
HooksManager::callCallouts(Hooks.hook_index_leases4_committed_,
|
|
|
|
*callout_handle);
|
2018-01-19 17:15:23 +01:00
|
|
|
|
2018-01-22 17:50:05 +01:00
|
|
|
if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
|
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_LEASES4_COMMITTED_DROP)
|
|
|
|
.arg(query->getLabel());
|
|
|
|
rsp.reset();
|
2018-01-19 17:15:23 +01:00
|
|
|
|
2018-01-22 17:50:05 +01:00
|
|
|
} else if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_PARK)
|
|
|
|
&& allow_packet_park) {
|
2018-01-19 17:15:23 +01:00
|
|
|
packet_park = true;
|
|
|
|
}
|
2018-01-18 20:02:07 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
if (!rsp) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2018-01-22 17:50:05 +01:00
|
|
|
// PARKING SPOT after leases4_committed hook point.
|
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
2018-01-19 17:15:23 +01:00
|
|
|
if (packet_park) {
|
2018-01-25 11:53:22 +01:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_LEASES4_COMMITTED_PARK)
|
|
|
|
.arg(query->getLabel());
|
|
|
|
|
2018-01-19 17:15:23 +01:00
|
|
|
// Park the packet. The function we bind here will be executed when the hook
|
|
|
|
// library unparks the packet.
|
|
|
|
HooksManager::park("leases4_committed", query,
|
2018-01-22 17:50:05 +01:00
|
|
|
[this, callout_handle, query, rsp]() mutable {
|
|
|
|
processPacketPktSend(callout_handle, query, rsp);
|
|
|
|
processPacketBufferSend(callout_handle, rsp);
|
|
|
|
});
|
|
|
|
|
|
|
|
// If we have parked the packet, let's reset the pointer to the
|
|
|
|
// response to indicate to the caller that it should return, as
|
|
|
|
// the packet processing will continue via the callback.
|
|
|
|
rsp.reset();
|
2018-01-19 17:15:23 +01:00
|
|
|
|
|
|
|
} else {
|
2018-01-22 17:50:05 +01:00
|
|
|
processPacketPktSend(callout_handle, query, rsp);
|
2018-01-19 17:15:23 +01:00
|
|
|
}
|
2018-01-18 20:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-22 17:50:05 +01:00
|
|
|
Dhcpv4Srv::processPacketPktSend(hooks::CalloutHandlePtr& callout_handle,
|
|
|
|
Pkt4Ptr& query, Pkt4Ptr& rsp) {
|
2015-11-01 10:43:42 +09:00
|
|
|
if (!rsp) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Specifies if server should do the packing
|
|
|
|
bool skip_pack = false;
|
2015-10-06 13:34:27 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Execute all callouts registered for pkt4_send
|
2016-06-30 19:37:58 +02:00
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_pkt4_send_)) {
|
2013-08-19 15:08:08 +02:00
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2015-05-15 15:54:08 +02:00
|
|
|
|
2016-07-07 14:58:50 +02:00
|
|
|
// Enable copying options from the query and response packets within
|
2016-07-06 12:28:49 +02:00
|
|
|
// hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query_resp_options_copy(query, rsp);
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Set our response
|
|
|
|
callout_handle->setArgument("response4", rsp);
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2016-01-22 16:23:10 +01:00
|
|
|
// Also pass the corresponding query packet as argument
|
|
|
|
callout_handle->setArgument("query4", query);
|
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
// Call all installed callouts
|
2016-06-30 19:37:58 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_pkt4_send_,
|
2015-11-01 10:43:42 +09:00
|
|
|
*callout_handle);
|
|
|
|
|
|
|
|
// Callouts decided to skip the next processing step. The next
|
|
|
|
// processing step would to send the packet, so skip at this
|
|
|
|
// stage means "drop response".
|
2017-12-15 00:53:20 +01:00
|
|
|
if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
|
|
|
|
(callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
|
2015-11-01 10:43:42 +09:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_PACKET_SEND_SKIP)
|
|
|
|
.arg(query->getLabel());
|
|
|
|
skip_pack = true;
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2015-11-01 10:43:42 +09:00
|
|
|
}
|
2013-10-18 11:10:54 +02:00
|
|
|
|
2015-11-01 10:43:42 +09:00
|
|
|
if (!skip_pack) {
|
|
|
|
try {
|
|
|
|
LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_PACK)
|
|
|
|
.arg(rsp->getLabel());
|
|
|
|
rsp->pack();
|
2015-12-07 17:48:47 +01:00
|
|
|
} catch (const std::exception& e) {
|
2015-11-01 10:43:42 +09:00
|
|
|
LOG_ERROR(options4_logger, DHCP4_PACKET_PACK_FAIL)
|
|
|
|
.arg(rsp->getLabel())
|
2015-12-07 17:48:47 +01:00
|
|
|
.arg(e.what());
|
|
|
|
}
|
2013-01-14 17:57:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:50:05 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::processPacketBufferSend(CalloutHandlePtr& callout_handle,
|
|
|
|
Pkt4Ptr& rsp) {
|
|
|
|
if (!rsp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Now all fields and options are constructed into output wire buffer.
|
|
|
|
// Option objects modification does not make sense anymore. Hooks
|
|
|
|
// can only manipulate wire buffer at this stage.
|
|
|
|
// Let's execute all callouts registered for buffer4_send
|
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_buffer4_send_)) {
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2018-01-22 17:50:05 +01:00
|
|
|
|
|
|
|
// Enable copying options from the packet within hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> resp4_options_copy(rsp);
|
|
|
|
|
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("response4", rsp);
|
|
|
|
|
|
|
|
// Call callouts
|
|
|
|
HooksManager::callCallouts(Hooks.hook_index_buffer4_send_,
|
|
|
|
*callout_handle);
|
|
|
|
|
|
|
|
// Callouts decided to skip the next processing step. The next
|
|
|
|
// processing step would to parse the packet, so skip at this
|
|
|
|
// stage means drop.
|
|
|
|
if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
|
|
|
|
(callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
|
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_BUFFER_SEND_SKIP)
|
|
|
|
.arg(rsp->getLabel());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callout_handle->getArgument("response4", rsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC, DHCP4_PACKET_SEND)
|
|
|
|
.arg(rsp->getLabel())
|
|
|
|
.arg(rsp->getName())
|
|
|
|
.arg(static_cast<int>(rsp->getType()))
|
|
|
|
.arg(rsp->getLocalAddr().isV4Zero() ? "*" : rsp->getLocalAddr().toText())
|
|
|
|
.arg(rsp->getLocalPort())
|
|
|
|
.arg(rsp->getRemoteAddr())
|
|
|
|
.arg(rsp->getRemotePort())
|
|
|
|
.arg(rsp->getIface().empty() ? "to be determined from routing" :
|
|
|
|
rsp->getIface());
|
|
|
|
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL_DATA,
|
|
|
|
DHCP4_RESPONSE_DATA)
|
|
|
|
.arg(rsp->getLabel())
|
|
|
|
.arg(rsp->getName())
|
|
|
|
.arg(static_cast<int>(rsp->getType()))
|
|
|
|
.arg(rsp->toText());
|
|
|
|
sendPacket(rsp);
|
|
|
|
|
|
|
|
// Update statistics accordingly for sent packet.
|
|
|
|
processStatsSent(rsp);
|
|
|
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
LOG_ERROR(packet4_logger, DHCP4_PACKET_SEND_FAIL)
|
|
|
|
.arg(rsp->getLabel())
|
|
|
|
.arg(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:16:28 +01:00
|
|
|
string
|
2013-01-30 19:07:54 +01:00
|
|
|
Dhcpv4Srv::srvidToString(const OptionPtr& srvid) {
|
2013-01-14 17:57:26 +01:00
|
|
|
if (!srvid) {
|
|
|
|
isc_throw(BadValue, "NULL pointer passed to srvidToString()");
|
|
|
|
}
|
|
|
|
boost::shared_ptr<Option4AddrLst> generated =
|
|
|
|
boost::dynamic_pointer_cast<Option4AddrLst>(srvid);
|
|
|
|
if (!srvid) {
|
|
|
|
isc_throw(BadValue, "Pointer to invalid option passed to srvidToString()");
|
|
|
|
}
|
|
|
|
|
|
|
|
Option4AddrLst::AddressContainer addrs = generated->getAddresses();
|
|
|
|
if (addrs.size() != 1) {
|
|
|
|
isc_throw(BadValue, "Malformed option passed to srvidToString(). "
|
|
|
|
<< "Expected to contain a single IPv4 address.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (addrs[0].toText());
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::appendServerID(Dhcpv4Exchange& ex) {
|
2017-10-06 12:39:08 +02:00
|
|
|
|
2017-10-12 11:40:49 +02:00
|
|
|
// Do not append generated server identifier if there is one appended already.
|
|
|
|
// This is when explicitly configured server identifier option is present.
|
|
|
|
if (ex.getResponse()->getOption(DHO_DHCP_SERVER_IDENTIFIER)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-06 12:39:08 +02:00
|
|
|
// Use local address on which the packet has been received as a
|
|
|
|
// server identifier. In some cases it may be a different address,
|
|
|
|
// e.g. broadcast packet or DHCPv4o6 packet.
|
|
|
|
IOAddress local_addr = ex.getQuery()->getLocalAddr();
|
|
|
|
Pkt4Ptr query = ex.getQuery();
|
|
|
|
|
|
|
|
if (local_addr.isV4Bcast() || query->isDhcp4o6()) {
|
|
|
|
SocketInfo sock_info = IfaceMgr::instance().getSocket(*query);
|
|
|
|
local_addr = sock_info.addr_;
|
|
|
|
}
|
|
|
|
|
2015-03-13 13:16:21 +01:00
|
|
|
OptionPtr opt_srvid(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
|
2017-10-06 12:39:08 +02:00
|
|
|
local_addr));
|
2015-03-13 13:16:21 +01:00
|
|
|
ex.getResponse()->addOption(opt_srvid);
|
2014-01-03 16:54:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 14:45:08 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::buildCfgOptionList(Dhcpv4Exchange& ex) {
|
|
|
|
CfgOptionList& co_list = ex.getCfgOptionList();
|
|
|
|
|
2016-05-20 19:00:00 +02:00
|
|
|
// Retrieve subnet.
|
2015-11-21 14:45:08 +01:00
|
|
|
Subnet4Ptr subnet = ex.getContext()->subnet_;
|
2015-11-25 11:27:55 +01:00
|
|
|
if (!subnet) {
|
|
|
|
// All methods using the CfgOptionList object return soon when
|
|
|
|
// there is no subnet so do the same
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 19:00:00 +02:00
|
|
|
|
|
|
|
// Firstly, host specific options.
|
2017-09-11 18:47:30 +02:00
|
|
|
const ConstHostPtr& host = ex.getContext()->currentHost();
|
2016-05-31 11:37:28 +02:00
|
|
|
if (host && !host->getCfgOption4()->empty()) {
|
2016-05-20 19:00:00 +02:00
|
|
|
co_list.push_back(host->getCfgOption4());
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:03:17 +01:00
|
|
|
// Secondly, pool specific options.
|
|
|
|
Pkt4Ptr resp = ex.getResponse();
|
|
|
|
IOAddress addr = IOAddress::IPV4_ZERO_ADDRESS();
|
|
|
|
if (resp) {
|
|
|
|
addr = resp->getYiaddr();
|
|
|
|
}
|
|
|
|
if (!addr.isV4Zero()) {
|
|
|
|
PoolPtr pool = subnet->getPool(Lease::TYPE_V4, addr, false);
|
|
|
|
if (pool && !pool->getCfgOption()->empty()) {
|
|
|
|
co_list.push_back(pool->getCfgOption());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thirdly, subnet configured options.
|
2015-11-25 11:27:55 +01:00
|
|
|
if (!subnet->getCfgOption()->empty()) {
|
2015-11-21 14:45:08 +01:00
|
|
|
co_list.push_back(subnet->getCfgOption());
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:03:17 +01:00
|
|
|
// Forthly, shared network specific options.
|
2017-09-18 10:46:31 +02:00
|
|
|
SharedNetwork4Ptr network;
|
|
|
|
subnet->getSharedNetwork(network);
|
|
|
|
if (network && !network->getCfgOption()->empty()) {
|
|
|
|
co_list.push_back(network->getCfgOption());
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:45:08 +01:00
|
|
|
// Each class in the incoming packet
|
|
|
|
const ClientClasses& classes = ex.getQuery()->getClasses();
|
2017-11-24 01:07:17 +01:00
|
|
|
for (ClientClasses::const_iterator cclass = classes.cbegin();
|
|
|
|
cclass != classes.cend(); ++cclass) {
|
2015-11-21 14:45:08 +01:00
|
|
|
// Find the client class definition for this class
|
|
|
|
const ClientClassDefPtr& ccdef = CfgMgr::instance().getCurrentCfg()->
|
|
|
|
getClientClassDictionary()->findClass(*cclass);
|
|
|
|
if (!ccdef) {
|
2017-11-28 00:23:45 +01:00
|
|
|
// Not found: the class is built-in or not configured
|
|
|
|
if (!isClientClassBuiltIn(*cclass)) {
|
2015-11-25 13:14:56 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_UNCONFIGURED)
|
2015-11-24 01:51:10 +01:00
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(*cclass);
|
|
|
|
}
|
|
|
|
// Skip it
|
2015-11-21 14:45:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-23 15:34:53 +01:00
|
|
|
if (ccdef->getCfgOption()->empty()) {
|
|
|
|
// Skip classes which don't configure options
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-21 14:45:08 +01:00
|
|
|
co_list.push_back(ccdef->getCfgOption());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last global options
|
2015-11-23 15:34:53 +01:00
|
|
|
if (!CfgMgr::instance().getCurrentCfg()->getCfgOption()->empty()) {
|
|
|
|
co_list.push_back(CfgMgr::instance().getCurrentCfg()->getCfgOption());
|
|
|
|
}
|
2015-11-21 14:45:08 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::appendRequestedOptions(Dhcpv4Exchange& ex) {
|
2013-01-15 19:53:58 +01:00
|
|
|
// Get the subnet relevant for the client. We will need it
|
|
|
|
// to get the options associated with it.
|
2015-03-10 21:36:08 +01:00
|
|
|
Subnet4Ptr subnet = ex.getContext()->subnet_;
|
2013-01-15 19:53:58 +01:00
|
|
|
// If we can't find the subnet for the client there is no way
|
|
|
|
// to get the options to be sent to a client. We don't log an
|
|
|
|
// error because it will be logged by the assignLease method
|
|
|
|
// anyway.
|
|
|
|
if (!subnet) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2015-11-23 15:34:53 +01:00
|
|
|
// Unlikely short cut
|
|
|
|
const CfgOptionList& co_list = ex.getCfgOptionList();
|
|
|
|
if (co_list.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 20:35:14 +01:00
|
|
|
Pkt4Ptr query = ex.getQuery();
|
2017-07-07 20:10:25 +02:00
|
|
|
Pkt4Ptr resp = ex.getResponse();
|
|
|
|
std::vector<uint8_t> requested_opts;
|
2015-03-12 20:35:14 +01:00
|
|
|
|
2013-01-15 19:53:58 +01:00
|
|
|
// try to get the 'Parameter Request List' option which holds the
|
|
|
|
// codes of requested options.
|
|
|
|
OptionUint8ArrayPtr option_prl = boost::dynamic_pointer_cast<
|
2015-03-12 20:35:14 +01:00
|
|
|
OptionUint8Array>(query->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
|
2017-07-07 20:10:25 +02:00
|
|
|
// Get the codes of requested options.
|
|
|
|
if (option_prl) {
|
|
|
|
requested_opts = option_prl->getValues();
|
|
|
|
}
|
|
|
|
// Iterate on the configured option list to add persistent options
|
|
|
|
for (CfgOptionList::const_iterator copts = co_list.begin();
|
|
|
|
copts != co_list.end(); ++copts) {
|
|
|
|
const OptionContainerPtr& opts = (*copts)->getAll(DHCP4_OPTION_SPACE);
|
|
|
|
if (!opts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Get persistent options
|
|
|
|
const OptionContainerPersistIndex& idx = opts->get<2>();
|
|
|
|
const OptionContainerPersistRange& range = idx.equal_range(true);
|
|
|
|
for (OptionContainerPersistIndex::const_iterator desc = range.first;
|
|
|
|
desc != range.second; ++desc) {
|
|
|
|
// Add the persistent option code to requested options
|
|
|
|
if (desc->option_) {
|
|
|
|
uint8_t code = static_cast<uint8_t>(desc->option_->getType());
|
|
|
|
requested_opts.push_back(code);
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 19:53:58 +01:00
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-01-15 19:53:58 +01:00
|
|
|
// For each requested option code get the instance of the option
|
|
|
|
// to be returned to the client.
|
|
|
|
for (std::vector<uint8_t>::const_iterator opt = requested_opts.begin();
|
|
|
|
opt != requested_opts.end(); ++opt) {
|
2015-11-21 14:45:08 +01:00
|
|
|
// Add nothing when it is already there
|
2015-03-12 20:35:14 +01:00
|
|
|
if (!resp->getOption(*opt)) {
|
2015-11-21 14:45:08 +01:00
|
|
|
// Iterate on the configured option list
|
|
|
|
for (CfgOptionList::const_iterator copts = co_list.begin();
|
|
|
|
copts != co_list.end(); ++copts) {
|
2016-10-26 08:18:05 +02:00
|
|
|
OptionDescriptor desc = (*copts)->get(DHCP4_OPTION_SPACE, *opt);
|
2015-11-21 14:45:08 +01:00
|
|
|
// Got it: add it and jump to the outer loop
|
2015-11-21 13:22:57 +01:00
|
|
|
if (desc.option_) {
|
|
|
|
resp->addOption(desc.option_);
|
2015-11-21 14:45:08 +01:00
|
|
|
break;
|
2015-11-21 13:22:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-23 17:26:24 +01:00
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-10-22 12:27:07 +02:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::appendRequestedVendorOptions(Dhcpv4Exchange& ex) {
|
2013-10-22 12:27:07 +02:00
|
|
|
// Get the configured subnet suitable for the incoming packet.
|
2015-03-10 21:36:08 +01:00
|
|
|
Subnet4Ptr subnet = ex.getContext()->subnet_;
|
2013-10-22 12:27:07 +02:00
|
|
|
// Leave if there is no subnet matching the incoming packet.
|
|
|
|
// There is no need to log the error message here because
|
|
|
|
// it will be logged in the assignLease() when it fails to
|
|
|
|
// pick the suitable subnet. We don't want to duplicate
|
|
|
|
// error messages in such case.
|
|
|
|
if (!subnet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-23 15:34:53 +01:00
|
|
|
// Unlikely short cut
|
|
|
|
const CfgOptionList& co_list = ex.getCfgOptionList();
|
|
|
|
if (co_list.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-12 20:05:57 +01:00
|
|
|
uint32_t vendor_id = 0;
|
|
|
|
|
2019-02-16 14:44:49 +01:00
|
|
|
// Try to get the vendor option from the client packet. This is how it's
|
|
|
|
// supposed to be done. Client sends vivso, we look at the vendor-id and
|
|
|
|
// then send back the vendor options specific to that client.
|
2015-03-10 21:36:08 +01:00
|
|
|
boost::shared_ptr<OptionVendor> vendor_req = boost::dynamic_pointer_cast<
|
|
|
|
OptionVendor>(ex.getQuery()->getOption(DHO_VIVSO_SUBOPTIONS));
|
2019-02-12 20:05:57 +01:00
|
|
|
if (vendor_req) {
|
|
|
|
vendor_id = vendor_req->getVendorId();
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:44:49 +01:00
|
|
|
// Something is fishy. Client was supposed to send vivso, but didn't.
|
|
|
|
// Let's try an alternative. It's possible that the server already
|
|
|
|
// inserted vivso in the response message, (e.g. by using client
|
|
|
|
// classification or perhaps a hook inserted it).
|
|
|
|
boost::shared_ptr<OptionVendor> vendor_rsp = boost::dynamic_pointer_cast<
|
|
|
|
OptionVendor>(ex.getResponse()->getOption(DHO_VIVSO_SUBOPTIONS));
|
2019-02-12 20:05:57 +01:00
|
|
|
if (vendor_rsp) {
|
|
|
|
vendor_id = vendor_rsp->getVendorId();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vendor_req && !vendor_rsp) {
|
2019-02-16 14:44:49 +01:00
|
|
|
// Ok, we're out of luck today. Neither client nor server packets
|
|
|
|
// have vivso. There is no way to figure out vendor-id here.
|
|
|
|
// We give up.
|
2013-10-22 12:27:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-07 20:10:25 +02:00
|
|
|
std::vector<uint8_t> requested_opts;
|
2013-10-22 12:27:07 +02:00
|
|
|
|
|
|
|
// Let's try to get ORO within that vendor-option
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo This is very specific to vendor-id=4491 (Cable Labs). Other
|
|
|
|
/// vendors may have different policies.
|
2019-02-12 20:05:57 +01:00
|
|
|
OptionUint8ArrayPtr oro;
|
|
|
|
if (vendor_req) {
|
|
|
|
oro = boost::dynamic_pointer_cast<OptionUint8Array>(vendor_req->getOption(DOCSIS3_V4_ORO));
|
|
|
|
}
|
2017-07-07 20:10:25 +02:00
|
|
|
// Get the list of options that client requested.
|
|
|
|
if (oro) {
|
|
|
|
requested_opts = oro->getValues();
|
|
|
|
}
|
|
|
|
// Iterate on the configured option list to add persistent options
|
|
|
|
for (CfgOptionList::const_iterator copts = co_list.begin();
|
|
|
|
copts != co_list.end(); ++copts) {
|
|
|
|
const OptionContainerPtr& opts = (*copts)->getAll(vendor_id);
|
|
|
|
if (!opts) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-12 20:05:57 +01:00
|
|
|
|
2017-07-07 20:10:25 +02:00
|
|
|
// Get persistent options
|
|
|
|
const OptionContainerPersistIndex& idx = opts->get<2>();
|
|
|
|
const OptionContainerPersistRange& range = idx.equal_range(true);
|
|
|
|
for (OptionContainerPersistIndex::const_iterator desc = range.first;
|
|
|
|
desc != range.second; ++desc) {
|
|
|
|
// Add the persistent option code to requested options
|
|
|
|
if (desc->option_) {
|
|
|
|
uint8_t code = static_cast<uint8_t>(desc->option_->getType());
|
|
|
|
requested_opts.push_back(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 12:27:07 +02:00
|
|
|
|
2017-07-07 20:10:25 +02:00
|
|
|
// If there is nothing to add don't do anything then.
|
|
|
|
if (requested_opts.empty()) {
|
2013-10-22 12:27:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-12 20:05:57 +01:00
|
|
|
if (!vendor_rsp) {
|
2019-02-16 14:44:49 +01:00
|
|
|
// It's possible that vivso was inserted already by client class or
|
|
|
|
// a hook. If that is so, let's use it.
|
2019-02-12 20:05:57 +01:00
|
|
|
vendor_rsp.reset(new OptionVendor(Option::V4, vendor_id));
|
|
|
|
}
|
2013-10-22 12:27:07 +02:00
|
|
|
|
|
|
|
// Get the list of options that client requested.
|
|
|
|
bool added = false;
|
2013-10-23 21:23:14 +02:00
|
|
|
for (std::vector<uint8_t>::const_iterator code = requested_opts.begin();
|
2013-10-22 12:27:07 +02:00
|
|
|
code != requested_opts.end(); ++code) {
|
2013-10-24 18:17:29 +02:00
|
|
|
if (!vendor_rsp->getOption(*code)) {
|
2015-11-21 14:45:08 +01:00
|
|
|
for (CfgOptionList::const_iterator copts = co_list.begin();
|
|
|
|
copts != co_list.end(); ++copts) {
|
|
|
|
OptionDescriptor desc = (*copts)->get(vendor_id, *code);
|
|
|
|
if (desc.option_) {
|
|
|
|
vendor_rsp->addOption(desc.option_);
|
|
|
|
added = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-24 18:17:29 +02:00
|
|
|
}
|
2013-10-22 12:27:07 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:44:49 +01:00
|
|
|
// If we added some sub-options and the vivso option is not in
|
|
|
|
// the response already, then add it.
|
2019-02-12 20:05:57 +01:00
|
|
|
if (added && !ex.getResponse()->getOption(DHO_VIVSO_SUBOPTIONS)) {
|
2015-03-10 21:36:08 +01:00
|
|
|
ex.getResponse()->addOption(vendor_rsp);
|
2013-01-15 19:53:58 +01:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 17:26:24 +01:00
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-10-22 12:27:07 +02:00
|
|
|
|
2013-01-22 18:40:55 +01:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::appendBasicOptions(Dhcpv4Exchange& ex) {
|
2013-01-22 18:40:55 +01:00
|
|
|
// Identify options that we always want to send to the
|
|
|
|
// client (if they are configured).
|
|
|
|
static const uint16_t required_options[] = {
|
|
|
|
DHO_ROUTERS,
|
|
|
|
DHO_DOMAIN_NAME_SERVERS,
|
2017-10-12 11:40:49 +02:00
|
|
|
DHO_DOMAIN_NAME,
|
|
|
|
DHO_DHCP_SERVER_IDENTIFIER };
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-01-22 18:40:55 +01:00
|
|
|
static size_t required_options_size =
|
|
|
|
sizeof(required_options) / sizeof(required_options[0]);
|
|
|
|
|
|
|
|
// Get the subnet.
|
2015-03-10 21:36:08 +01:00
|
|
|
Subnet4Ptr subnet = ex.getContext()->subnet_;
|
2013-01-22 18:40:55 +01:00
|
|
|
if (!subnet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-23 15:34:53 +01:00
|
|
|
// Unlikely short cut
|
|
|
|
const CfgOptionList& co_list = ex.getCfgOptionList();
|
|
|
|
if (co_list.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 20:35:14 +01:00
|
|
|
Pkt4Ptr resp = ex.getResponse();
|
|
|
|
|
2013-01-22 18:40:55 +01:00
|
|
|
// Try to find all 'required' options in the outgoing
|
|
|
|
// message. Those that are not present will be added.
|
|
|
|
for (int i = 0; i < required_options_size; ++i) {
|
2015-03-12 20:35:14 +01:00
|
|
|
OptionPtr opt = resp->getOption(required_options[i]);
|
2013-01-22 18:40:55 +01:00
|
|
|
if (!opt) {
|
|
|
|
// Check whether option has been configured.
|
2015-11-21 14:45:08 +01:00
|
|
|
for (CfgOptionList::const_iterator copts = co_list.begin();
|
|
|
|
copts != co_list.end(); ++copts) {
|
2016-10-26 08:18:05 +02:00
|
|
|
OptionDescriptor desc = (*copts)->get(DHCP4_OPTION_SPACE,
|
|
|
|
required_options[i]);
|
2015-11-21 14:45:08 +01:00
|
|
|
if (desc.option_) {
|
|
|
|
resp->addOption(desc.option_);
|
|
|
|
break;
|
|
|
|
}
|
2013-01-22 18:40:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-23 17:26:24 +01:00
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-08-28 14:46:29 +02:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::processClientName(Dhcpv4Exchange& ex) {
|
2013-08-28 14:46:29 +02:00
|
|
|
// It is possible that client has sent both Client FQDN and Hostname
|
|
|
|
// option. In such case, server should prefer Client FQDN option and
|
|
|
|
// ignore the Hostname option.
|
2013-11-20 14:04:40 +01:00
|
|
|
try {
|
2015-03-10 21:36:08 +01:00
|
|
|
Pkt4Ptr resp = ex.getResponse();
|
2013-11-20 14:04:40 +01:00
|
|
|
Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>
|
2015-03-10 21:36:08 +01:00
|
|
|
(ex.getQuery()->getOption(DHO_FQDN));
|
2013-11-20 14:04:40 +01:00
|
|
|
if (fqdn) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_CLIENT_FQDN_PROCESS)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(ex.getQuery()->getLabel());
|
2015-03-10 21:36:08 +01:00
|
|
|
processClientFqdnOption(ex);
|
2013-11-20 14:04:40 +01:00
|
|
|
|
|
|
|
} else {
|
2016-04-04 07:19:55 -04:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_CLIENT_HOSTNAME_PROCESS)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(ex.getQuery()->getLabel());
|
2016-04-04 07:19:55 -04:00
|
|
|
processHostnameOption(ex);
|
2013-11-20 14:04:40 +01:00
|
|
|
}
|
2015-05-05 19:34:13 +02:00
|
|
|
} catch (const Exception& e) {
|
2013-11-20 14:04:40 +01:00
|
|
|
// In some rare cases it is possible that the client's name processing
|
|
|
|
// fails. For example, the Hostname option may be malformed, or there
|
|
|
|
// may be an error in the server's logic which would cause multiple
|
|
|
|
// attempts to add the same option to the response message. This
|
|
|
|
// error message aggregates all these errors so they can be diagnosed
|
|
|
|
// from the log. We don't want to throw an exception here because,
|
|
|
|
// it will impact the processing of the whole packet. We rather want
|
|
|
|
// the processing to continue, even if the client's name is wrong.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_NAME_PROC_FAIL)
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(e.what());
|
2013-08-28 14:46:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::processClientFqdnOption(Dhcpv4Exchange& ex) {
|
2015-03-10 21:36:08 +01:00
|
|
|
// Obtain the FQDN option from the client's message.
|
|
|
|
Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
|
|
|
|
Option4ClientFqdn>(ex.getQuery()->getOption(DHO_FQDN));
|
|
|
|
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_FQDN_DATA)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(fqdn->toText());
|
|
|
|
|
2013-08-28 16:15:56 +02:00
|
|
|
// Create the DHCPv4 Client FQDN Option to be included in the server's
|
|
|
|
// response to a client.
|
|
|
|
Option4ClientFqdnPtr fqdn_resp(new Option4ClientFqdn(*fqdn));
|
|
|
|
|
2014-01-20 10:32:32 -05:00
|
|
|
// Set the server S, N, and O flags based on client's flags and
|
|
|
|
// current configuration.
|
|
|
|
D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
|
2019-10-03 15:11:07 -04:00
|
|
|
d2_mgr.adjustFqdnFlags<Option4ClientFqdn>(*fqdn, *fqdn_resp,
|
|
|
|
*(ex.getContext()->getDdnsParams()));
|
2014-01-20 10:32:32 -05:00
|
|
|
// Carry over the client's E flag.
|
|
|
|
fqdn_resp->setFlag(Option4ClientFqdn::FLAG_E,
|
|
|
|
fqdn->getFlag(Option4ClientFqdn::FLAG_E));
|
2013-09-09 19:22:28 +02:00
|
|
|
|
2017-09-11 18:47:30 +02:00
|
|
|
if (ex.getContext()->currentHost() &&
|
|
|
|
!ex.getContext()->currentHost()->getHostname().empty()) {
|
2015-03-11 15:18:20 +01:00
|
|
|
D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
|
2017-09-11 18:47:30 +02:00
|
|
|
fqdn_resp->setDomainName(d2_mgr.qualifyName(ex.getContext()->currentHost()->getHostname(),
|
2019-10-03 15:11:07 -04:00
|
|
|
*(ex.getContext()->getDdnsParams()), true),
|
|
|
|
Option4ClientFqdn::FULL);
|
2013-09-09 19:22:28 +02:00
|
|
|
|
2015-03-10 16:52:21 +01:00
|
|
|
} else {
|
|
|
|
// Adjust the domain name based on domain name value and type sent by the
|
|
|
|
// client and current configuration.
|
2019-10-03 15:11:07 -04:00
|
|
|
d2_mgr.adjustDomainName<Option4ClientFqdn>(*fqdn, *fqdn_resp,
|
|
|
|
*(ex.getContext()->getDdnsParams()));
|
2015-03-10 16:52:21 +01:00
|
|
|
}
|
2013-09-02 17:19:53 +02:00
|
|
|
|
|
|
|
// Add FQDN option to the response message. Note that, there may be some
|
|
|
|
// cases when server may choose not to include the FQDN option in a
|
|
|
|
// response to a client. In such cases, the FQDN should be removed from the
|
|
|
|
// outgoing message. In theory we could cease to include the FQDN option
|
|
|
|
// in this function until it is confirmed that it should be included.
|
|
|
|
// However, we include it here for simplicity. Functions used to acquire
|
|
|
|
// lease for a client will scan the response message for FQDN and if it
|
|
|
|
// is found they will take necessary actions to store the FQDN information
|
|
|
|
// in the lease database as well as to generate NameChangeRequests to DNS.
|
2015-01-22 11:25:36 +01:00
|
|
|
// If we don't store the option in the response message, we will have to
|
2013-09-02 17:19:53 +02:00
|
|
|
// propagate it in the different way to the functions which acquire the
|
|
|
|
// lease. This would require modifications to the API of this class.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_RESPONSE_FQDN_DATA)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(fqdn_resp->toText());
|
2015-03-10 21:36:08 +01:00
|
|
|
ex.getResponse()->addOption(fqdn_resp);
|
2013-08-28 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::processHostnameOption(Dhcpv4Exchange& ex) {
|
2014-01-20 10:32:32 -05:00
|
|
|
// Fetch D2 configuration.
|
|
|
|
D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
|
|
|
|
|
2016-09-12 16:47:04 +02:00
|
|
|
// Obtain the Hostname option from the client's message.
|
|
|
|
OptionStringPtr opt_hostname = boost::dynamic_pointer_cast<OptionString>
|
|
|
|
(ex.getQuery()->getOption(DHO_HOST_NAME));
|
|
|
|
|
|
|
|
if (opt_hostname) {
|
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_HOSTNAME_DATA)
|
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(opt_hostname->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocEngine::ClientContext4Ptr ctx = ex.getContext();
|
|
|
|
|
|
|
|
// Hostname reservations take precedence over any other configuration,
|
|
|
|
// i.e. DDNS configuration.
|
2017-09-11 18:47:30 +02:00
|
|
|
if (ctx->currentHost() && !ctx->currentHost()->getHostname().empty()) {
|
2016-09-12 16:47:04 +02:00
|
|
|
// In order to send a reserved hostname value we expect that at least
|
|
|
|
// one of the following is the case: the client has sent us a hostname
|
|
|
|
// option, or the client has sent Parameter Request List option with
|
|
|
|
// the hostname option code included.
|
|
|
|
|
|
|
|
// It is going to be less expensive to first check the presence of the
|
|
|
|
// hostname option.
|
|
|
|
bool should_send_hostname = static_cast<bool>(opt_hostname);
|
|
|
|
// Hostname option is not present, so we have to check PRL option.
|
|
|
|
if (!should_send_hostname) {
|
|
|
|
OptionUint8ArrayPtr
|
|
|
|
option_prl = boost::dynamic_pointer_cast<OptionUint8Array>
|
|
|
|
(ex.getQuery()->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
|
|
|
|
if (option_prl) {
|
2016-09-13 11:40:43 +02:00
|
|
|
// PRL option exists, so check if the hostname option code is
|
|
|
|
// included in it.
|
2016-09-12 16:47:04 +02:00
|
|
|
const std::vector<uint8_t>&
|
|
|
|
requested_opts = option_prl->getValues();
|
|
|
|
if (std::find(requested_opts.begin(), requested_opts.end(),
|
|
|
|
DHO_HOST_NAME) != requested_opts.end()) {
|
|
|
|
// Client has requested hostname via Parameter Request
|
|
|
|
// List option.
|
|
|
|
should_send_hostname = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the hostname or PRL option indicates that the server should
|
|
|
|
// send back a hostname option, send this option with a reserved
|
|
|
|
// name for this client.
|
|
|
|
if (should_send_hostname) {
|
2019-10-03 15:11:07 -04:00
|
|
|
std::string hostname = d2_mgr.qualifyName(ctx->currentHost()->getHostname(),
|
|
|
|
*(ex.getContext()->getDdnsParams()), false);
|
2018-12-04 10:10:30 +01:00
|
|
|
// Convert hostname to lower case.
|
|
|
|
boost::algorithm::to_lower(hostname);
|
|
|
|
|
2016-09-12 16:47:04 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA,
|
|
|
|
DHCP4_RESERVED_HOSTNAME_ASSIGNED)
|
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(hostname);
|
|
|
|
OptionStringPtr opt_hostname_resp(new OptionString(Option::V4,
|
|
|
|
DHO_HOST_NAME,
|
|
|
|
hostname));
|
|
|
|
ex.getResponse()->addOption(opt_hostname_resp);
|
|
|
|
|
|
|
|
// We're done here.
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 12:28:06 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 16:47:04 +02:00
|
|
|
// There is no reservation for this client or the client hasn't requested
|
|
|
|
// hostname option. There is still a possibility that we'll have to send
|
|
|
|
// hostname option to this client if the client has included hostname option
|
|
|
|
// but there is no reservation, or the configuration of the server requires
|
|
|
|
// that we send the option regardless.
|
2019-10-03 15:11:07 -04:00
|
|
|
D2ClientConfig::ReplaceClientNameMode replace_name_mode
|
|
|
|
= ex.getContext()->getDdnsParams()->replace_client_name_mode_;
|
2016-04-04 07:19:55 -04:00
|
|
|
|
|
|
|
// If we don't have a hostname then either we'll supply it or do nothing.
|
|
|
|
if (!opt_hostname) {
|
|
|
|
// If we're configured to supply it then add it to the response.
|
|
|
|
// Use the root domain to signal later on that we should replace it.
|
|
|
|
if (replace_name_mode == D2ClientConfig::RCM_ALWAYS ||
|
|
|
|
replace_name_mode == D2ClientConfig::RCM_WHEN_NOT_PRESENT) {
|
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA,
|
2016-04-06 11:41:38 -04:00
|
|
|
DHCP4_GENERATE_FQDN)
|
2016-04-04 07:19:55 -04:00
|
|
|
.arg(ex.getQuery()->getLabel());
|
|
|
|
OptionStringPtr opt_hostname_resp(new OptionString(Option::V4,
|
|
|
|
DHO_HOST_NAME,
|
|
|
|
"."));
|
|
|
|
ex.getResponse()->addOption(opt_hostname_resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client sent us a hostname option so figure out what to do with it.
|
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_HOSTNAME_DATA)
|
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(opt_hostname->getValue());
|
|
|
|
|
2014-02-19 09:09:00 -05:00
|
|
|
std::string hostname = isc::util::str::trim(opt_hostname->getValue());
|
2019-07-12 07:01:01 -04:00
|
|
|
unsigned int label_count;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Parsing into labels can throw on malformed content so we're
|
|
|
|
// going to explicitly catch that here.
|
|
|
|
label_count = OptionDataTypeUtil::getLabelCount(hostname);
|
|
|
|
} catch (const std::exception& exc) {
|
2019-08-15 17:02:29 -04:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_CLIENT_HOSTNAME_MALFORMED)
|
2019-07-12 07:01:01 -04:00
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(exc.what());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:04:40 +01:00
|
|
|
// The hostname option sent by the client should be at least 1 octet long.
|
2014-01-20 10:32:32 -05:00
|
|
|
// If it isn't we ignore this option. (Per RFC 2131, section 3.14)
|
|
|
|
/// @todo It would be more liberal to accept this and let it fall into
|
|
|
|
/// the case of replace or less than two below.
|
2013-11-20 14:04:40 +01:00
|
|
|
if (label_count == 0) {
|
2019-08-15 17:02:29 -04:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_EMPTY_HOSTNAME)
|
2015-05-05 19:34:13 +02:00
|
|
|
.arg(ex.getQuery()->getLabel());
|
2013-11-20 14:04:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-07-10 13:31:49 -04:00
|
|
|
|
|
|
|
// Stores the value we eventually use, so we can send it back.
|
|
|
|
OptionStringPtr opt_hostname_resp;
|
2013-09-09 14:19:12 +02:00
|
|
|
|
|
|
|
// The hostname option may be unqualified or fully qualified. The lab_count
|
|
|
|
// holds the number of labels for the name. The number of 1 means that
|
|
|
|
// there is only root label "." (even for unqualified names, as the
|
|
|
|
// getLabelCount function treats each name as a fully qualified one).
|
|
|
|
// By checking the number of labels present in the hostname we may infer
|
|
|
|
// whether client has sent the fully qualified or unqualified hostname.
|
|
|
|
|
2016-09-12 16:47:04 +02:00
|
|
|
if ((replace_name_mode == D2ClientConfig::RCM_ALWAYS ||
|
|
|
|
replace_name_mode == D2ClientConfig::RCM_WHEN_PRESENT)
|
|
|
|
|| label_count < 2) {
|
2014-02-19 09:09:00 -05:00
|
|
|
// Set to root domain to signal later on that we should replace it.
|
|
|
|
// DHO_HOST_NAME is a string option which cannot be empty.
|
2015-03-10 16:52:21 +01:00
|
|
|
/// @todo We may want to reconsider whether it is appropriate for the
|
|
|
|
/// client to send a root domain name as a Hostname. There are
|
|
|
|
/// also extensions to the auto generation of the client's name,
|
2016-04-04 07:19:55 -04:00
|
|
|
/// e.g. conversion to the puny code which may be considered at some
|
|
|
|
/// point.
|
2015-03-10 16:52:21 +01:00
|
|
|
/// For now, we just remain liberal and expect that the DNS will handle
|
|
|
|
/// conversion if needed and possible.
|
2018-07-10 13:31:49 -04:00
|
|
|
opt_hostname_resp.reset(new OptionString(Option::V4, DHO_HOST_NAME, "."));
|
|
|
|
} else {
|
|
|
|
// Sanitize the name the client sent us, if we're configured to do so.
|
2019-10-03 15:11:07 -04:00
|
|
|
isc::util::str::StringSanitizerPtr sanitizer
|
|
|
|
= ex.getContext()->getDdnsParams()->getHostnameSanitizer();
|
|
|
|
|
2018-07-10 13:31:49 -04:00
|
|
|
if (sanitizer) {
|
|
|
|
hostname = sanitizer->scrub(hostname);
|
|
|
|
}
|
|
|
|
|
2018-12-04 10:10:30 +01:00
|
|
|
// Convert hostname to lower case.
|
|
|
|
boost::algorithm::to_lower(hostname);
|
|
|
|
|
2018-07-10 13:31:49 -04:00
|
|
|
if (label_count == 2) {
|
|
|
|
// If there are two labels, it means that the client has specified
|
|
|
|
// the unqualified name. We have to concatenate the unqualified name
|
|
|
|
// with the domain name. The false value passed as a second argument
|
|
|
|
// indicates that the trailing dot should not be appended to the
|
|
|
|
// hostname. We don't want to append the trailing dot because
|
|
|
|
// we don't know whether the hostname is partial or not and some
|
|
|
|
// clients do not handle the hostnames with the trailing dot.
|
2019-10-03 15:11:07 -04:00
|
|
|
opt_hostname_resp.reset(
|
|
|
|
new OptionString(Option::V4, DHO_HOST_NAME,
|
|
|
|
d2_mgr.qualifyName(hostname, *(ex.getContext()->getDdnsParams()),
|
|
|
|
false)));
|
2018-07-10 13:31:49 -04:00
|
|
|
} else {
|
|
|
|
opt_hostname_resp.reset(new OptionString(Option::V4, DHO_HOST_NAME, hostname));
|
|
|
|
}
|
2013-09-09 14:19:12 +02:00
|
|
|
}
|
2015-06-09 16:48:51 +02:00
|
|
|
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_RESPONSE_HOSTNAME_DATA)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(ex.getQuery()->getLabel())
|
|
|
|
.arg(opt_hostname_resp->getValue());
|
2015-03-10 21:36:08 +01:00
|
|
|
ex.getResponse()->addOption(opt_hostname_resp);
|
2013-08-28 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:39:48 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::createNameChangeRequests(const Lease4Ptr& lease,
|
2013-09-04 14:35:05 +02:00
|
|
|
const Lease4Ptr& old_lease) {
|
2013-09-03 18:39:48 +02:00
|
|
|
if (!lease) {
|
|
|
|
isc_throw(isc::Unexpected,
|
|
|
|
"NULL lease specified when creating NameChangeRequest");
|
2015-05-11 17:41:30 +02:00
|
|
|
|
2016-09-13 15:13:02 +02:00
|
|
|
} else if (!old_lease || !lease->hasIdenticalFqdn(*old_lease)) {
|
2017-09-29 11:44:32 -04:00
|
|
|
if (old_lease) {
|
|
|
|
// Queue's up a remove of the old lease's DNS (if needed)
|
|
|
|
queueNCR(CHG_REMOVE, old_lease);
|
|
|
|
}
|
|
|
|
|
2015-10-19 15:30:01 +02:00
|
|
|
// We may need to generate the NameChangeRequest for the new lease. It
|
|
|
|
// will be generated only if hostname is set and if forward or reverse
|
|
|
|
// update has been requested.
|
|
|
|
queueNCR(CHG_ADD, lease);
|
2013-09-04 14:35:05 +02:00
|
|
|
}
|
2013-09-10 08:27:44 +02:00
|
|
|
}
|
2013-09-09 10:24:54 +02:00
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::assignLease(Dhcpv4Exchange& ex) {
|
2015-03-10 21:36:08 +01:00
|
|
|
// Get the pointers to the query and the response messages.
|
|
|
|
Pkt4Ptr query = ex.getQuery();
|
|
|
|
Pkt4Ptr resp = ex.getResponse();
|
|
|
|
|
|
|
|
// Get the context.
|
|
|
|
AllocEngine::ClientContext4Ptr ctx = ex.getContext();
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
// Subnet should have been already selected when the context was created.
|
|
|
|
Subnet4Ptr subnet = ctx->subnet_;
|
2012-12-31 16:49:37 +01:00
|
|
|
if (!subnet) {
|
|
|
|
// This particular client is out of luck today. We do not have
|
|
|
|
// information about the subnet he is connected to. This likely means
|
2015-04-22 15:57:37 +02:00
|
|
|
// misconfiguration of the server (or some relays).
|
|
|
|
|
|
|
|
// Perhaps this should be logged on some higher level?
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_ERROR(bad_packet4_logger, DHCP4_PACKET_NAK_0001)
|
2015-04-20 11:52:37 -04:00
|
|
|
.arg(query->getLabel())
|
2015-03-10 21:36:08 +01:00
|
|
|
.arg(query->getRemoteAddr().toText())
|
2015-05-04 18:29:54 +02:00
|
|
|
.arg(query->getName());
|
2015-03-10 21:36:08 +01:00
|
|
|
resp->setType(DHCPNAK);
|
|
|
|
resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());
|
2012-12-31 16:49:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2013-10-11 20:03:40 +02:00
|
|
|
|
2014-07-22 16:00:46 +02:00
|
|
|
// Get the server identifier. It will be used to determine the state
|
|
|
|
// of the client.
|
|
|
|
OptionCustomPtr opt_serverid = boost::dynamic_pointer_cast<
|
2015-03-10 21:36:08 +01:00
|
|
|
OptionCustom>(query->getOption(DHO_DHCP_SERVER_IDENTIFIER));
|
2014-07-22 16:00:46 +02:00
|
|
|
|
2014-12-11 22:19:10 +01:00
|
|
|
// Check if the client has sent a requested IP address option or
|
|
|
|
// ciaddr.
|
2014-07-21 22:16:01 +02:00
|
|
|
OptionCustomPtr opt_requested_address = boost::dynamic_pointer_cast<
|
2015-03-10 21:36:08 +01:00
|
|
|
OptionCustom>(query->getOption(DHO_DHCP_REQUESTED_ADDRESS));
|
2015-04-22 15:57:37 +02:00
|
|
|
IOAddress hint(IOAddress::IPV4_ZERO_ADDRESS());
|
2014-07-21 22:16:01 +02:00
|
|
|
if (opt_requested_address) {
|
|
|
|
hint = opt_requested_address->readAddress();
|
2014-12-11 22:19:10 +01:00
|
|
|
|
2015-03-17 19:39:11 +01:00
|
|
|
} else if (!query->getCiaddr().isV4Zero()) {
|
2015-03-10 21:36:08 +01:00
|
|
|
hint = query->getCiaddr();
|
2014-12-11 22:19:10 +01:00
|
|
|
|
2014-07-21 22:16:01 +02:00
|
|
|
}
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
HWAddrPtr hwaddr = query->getHWAddr();
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// "Fake" allocation is processing of DISCOVER message. We pretend to do an
|
|
|
|
// allocation, but we do not put the lease in the database. That is ok,
|
|
|
|
// because we do not guarantee that the user will get that exact lease. If
|
|
|
|
// the user selects this server to do actual allocation (i.e. sends REQUEST)
|
|
|
|
// it should include this hint. That will help us during the actual lease
|
|
|
|
// allocation.
|
2015-03-10 21:36:08 +01:00
|
|
|
bool fake_allocation = (query->getType() == DHCPDISCOVER);
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2015-05-12 19:01:27 +02:00
|
|
|
// Get client-id. It is not mandatory in DHCPv4.
|
|
|
|
ClientIdPtr client_id = ex.getContext()->clientid_;
|
|
|
|
|
2014-07-22 16:00:46 +02:00
|
|
|
// If there is no server id and there is a Requested IP Address option
|
|
|
|
// the client is in the INIT-REBOOT state in which the server has to
|
2015-01-22 10:40:51 +01:00
|
|
|
// determine whether the client's notion of the address is correct
|
|
|
|
// and whether the client is known, i.e., has a lease.
|
2014-07-22 16:00:46 +02:00
|
|
|
if (!fake_allocation && !opt_serverid && opt_requested_address) {
|
2015-04-22 15:57:37 +02:00
|
|
|
|
2016-03-23 08:28:29 -04:00
|
|
|
LOG_INFO(lease4_logger, DHCP4_INIT_REBOOT)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(hint.toText());
|
|
|
|
|
2015-01-22 10:40:51 +01:00
|
|
|
Lease4Ptr lease;
|
2017-09-18 10:46:31 +02:00
|
|
|
Subnet4Ptr original_subnet = subnet;
|
|
|
|
|
2018-03-02 16:06:13 +01:00
|
|
|
// We used to issue a separate query (two actually: one for client-id
|
|
|
|
// and another one for hw-addr for) each subnet in the shared network.
|
|
|
|
// That was horribly inefficient if the client didn't have any lease
|
|
|
|
// (or there were many subnets and the client happended to be in one
|
|
|
|
// of the last subnets).
|
|
|
|
//
|
|
|
|
// We now issue at most two queries: get all the leases for specific
|
|
|
|
// client-id and then get all leases for specific hw-address.
|
2018-02-27 10:48:53 +01:00
|
|
|
if (client_id) {
|
2018-03-02 16:06:13 +01:00
|
|
|
|
|
|
|
// Get all the leases for this client-id
|
2018-02-27 10:48:53 +01:00
|
|
|
Lease4Collection leases_client_id = LeaseMgrFactory::instance().getLease4(*client_id);
|
|
|
|
if (!leases_client_id.empty()) {
|
|
|
|
Subnet4Ptr s = original_subnet;
|
|
|
|
|
2018-03-02 16:06:13 +01:00
|
|
|
// Among those returned try to find a lease that belongs to
|
|
|
|
// current shared network.
|
2018-02-27 10:48:53 +01:00
|
|
|
while (s) {
|
|
|
|
for (auto l = leases_client_id.begin(); l != leases_client_id.end(); ++l) {
|
|
|
|
if ((*l)->subnet_id_ == s->getID()) {
|
|
|
|
lease = *l;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lease) {
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
s = s->getNextSubnet(original_subnet, query->getClasses());
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 10:46:31 +02:00
|
|
|
}
|
2018-02-27 10:48:53 +01:00
|
|
|
}
|
2014-07-22 16:00:46 +02:00
|
|
|
|
2018-03-02 16:06:13 +01:00
|
|
|
// If we haven't found a lease yet, try again by hardware-address.
|
|
|
|
// The logic is the same.
|
2018-02-27 10:48:53 +01:00
|
|
|
if (!lease && hwaddr) {
|
2018-03-02 16:06:13 +01:00
|
|
|
|
|
|
|
// Get all leases for this particular hw-address.
|
2018-02-27 10:48:53 +01:00
|
|
|
Lease4Collection leases_hwaddr = LeaseMgrFactory::instance().getLease4(*hwaddr);
|
|
|
|
if (!leases_hwaddr.empty()) {
|
|
|
|
Subnet4Ptr s = original_subnet;
|
|
|
|
|
2018-03-02 16:06:13 +01:00
|
|
|
// Pick one that belongs to a subnet in this shared network.
|
2018-02-27 10:48:53 +01:00
|
|
|
while (s) {
|
|
|
|
for (auto l = leases_hwaddr.begin(); l != leases_hwaddr.end(); ++l) {
|
|
|
|
if ((*l)->subnet_id_ == s->getID()) {
|
|
|
|
lease = *l;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 10:46:31 +02:00
|
|
|
|
2018-02-27 10:48:53 +01:00
|
|
|
if (lease) {
|
|
|
|
break;
|
2017-09-18 10:46:31 +02:00
|
|
|
|
2018-02-27 10:48:53 +01:00
|
|
|
} else {
|
|
|
|
s = s->getNextSubnet(original_subnet, query->getClasses());
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 10:46:31 +02:00
|
|
|
}
|
2015-01-22 10:40:51 +01:00
|
|
|
}
|
2015-05-12 09:54:59 +02:00
|
|
|
|
|
|
|
// Check the first error case: unknown client. We check this before
|
|
|
|
// validating the address sent because we don't want to respond if
|
2018-10-19 17:25:22 +02:00
|
|
|
// we don't know this client, except if we're authoritative.
|
|
|
|
bool authoritative = original_subnet->getAuthoritative();
|
|
|
|
bool known_client = lease && lease->belongsToClient(hwaddr, client_id);
|
|
|
|
if (!authoritative && !known_client) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
|
2015-01-22 10:40:51 +01:00
|
|
|
DHCP4_NO_LEASE_INIT_REBOOT)
|
2015-04-20 11:52:37 -04:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(hint.toText());
|
2014-07-22 16:00:46 +02:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
ex.deleteResponse();
|
2014-07-22 16:00:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-05-12 09:54:59 +02:00
|
|
|
|
2018-10-19 17:25:22 +02:00
|
|
|
// If we know this client, check if his notion of the IP address is
|
2018-11-07 16:01:09 +01:00
|
|
|
// correct, if we don't know him, check if we are authoritative.
|
|
|
|
if ((known_client && (lease->addr_ != hint)) ||
|
|
|
|
(!known_client && authoritative)) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL,
|
2015-05-12 09:54:59 +02:00
|
|
|
DHCP4_PACKET_NAK_0002)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(hint.toText());
|
2015-01-22 10:40:51 +01:00
|
|
|
|
2015-05-12 09:54:59 +02:00
|
|
|
resp->setType(DHCPNAK);
|
|
|
|
resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());
|
2015-01-22 10:40:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-07-22 16:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
2013-07-16 13:29:41 +02:00
|
|
|
|
2013-09-09 10:24:54 +02:00
|
|
|
std::string hostname;
|
|
|
|
bool fqdn_fwd = false;
|
|
|
|
bool fqdn_rev = false;
|
2014-02-19 09:09:00 -05:00
|
|
|
OptionStringPtr opt_hostname;
|
2013-09-09 10:24:54 +02:00
|
|
|
Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<
|
2015-03-10 21:36:08 +01:00
|
|
|
Option4ClientFqdn>(resp->getOption(DHO_FQDN));
|
2013-09-09 10:24:54 +02:00
|
|
|
if (fqdn) {
|
|
|
|
hostname = fqdn->getDomainName();
|
2014-03-01 10:50:21 -05:00
|
|
|
CfgMgr::instance().getD2ClientMgr().getUpdateDirections(*fqdn,
|
|
|
|
fqdn_fwd,
|
|
|
|
fqdn_rev);
|
2013-09-09 14:19:12 +02:00
|
|
|
} else {
|
2014-02-19 09:09:00 -05:00
|
|
|
opt_hostname = boost::dynamic_pointer_cast<OptionString>
|
2015-03-10 21:36:08 +01:00
|
|
|
(resp->getOption(DHO_HOST_NAME));
|
2015-04-22 15:57:37 +02:00
|
|
|
|
2013-09-09 14:19:12 +02:00
|
|
|
if (opt_hostname) {
|
2014-02-19 09:09:00 -05:00
|
|
|
hostname = opt_hostname->getValue();
|
|
|
|
// DHO_HOST_NAME is string option which cannot be blank,
|
|
|
|
// we use "." to know we should replace it with a fully
|
|
|
|
// generated name. The local string variable needs to be
|
|
|
|
// blank in logic below.
|
|
|
|
if (hostname == ".") {
|
|
|
|
hostname = "";
|
|
|
|
}
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo It could be configurable what sort of updates the
|
|
|
|
/// server is doing when Hostname option was sent.
|
2013-09-09 14:19:12 +02:00
|
|
|
fqdn_fwd = true;
|
|
|
|
fqdn_rev = true;
|
|
|
|
}
|
2013-09-09 10:24:54 +02:00
|
|
|
}
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
// We need to set these values in the context as they haven't been set yet.
|
|
|
|
ctx->requested_address_ = hint;
|
|
|
|
ctx->fwd_dns_update_ = fqdn_fwd;
|
|
|
|
ctx->rev_dns_update_ = fqdn_rev;
|
|
|
|
ctx->hostname_ = hostname;
|
|
|
|
ctx->fake_allocation_ = fake_allocation;
|
|
|
|
ctx->callout_handle_ = callout_handle;
|
2015-03-02 19:37:57 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
Lease4Ptr lease = alloc_engine_->allocateLease4(*ctx);
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2017-09-18 10:46:31 +02:00
|
|
|
// Subnet may be modified by the allocation engine, if the initial subnet
|
|
|
|
// belongs to a shared network.
|
2017-10-03 20:00:52 +02:00
|
|
|
if (subnet->getID() != ctx->subnet_->getID()) {
|
|
|
|
SharedNetwork4Ptr network;
|
|
|
|
subnet->getSharedNetwork(network);
|
|
|
|
if (network) {
|
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_BASIC_DATA, DHCP4_SUBNET_DYNAMICALLY_CHANGED)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(subnet->toText())
|
|
|
|
.arg(ctx->subnet_->toText())
|
|
|
|
.arg(network->getName());
|
|
|
|
}
|
|
|
|
subnet = ctx->subnet_;
|
|
|
|
}
|
2017-09-18 10:46:31 +02:00
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
if (lease) {
|
2013-01-08 13:53:34 +01:00
|
|
|
// We have a lease! Let's set it in the packet and send it back to
|
|
|
|
// the client.
|
2019-06-25 11:06:16 +02:00
|
|
|
if (fake_allocation) {
|
|
|
|
LOG_INFO(lease4_logger, DHCP4_LEASE_ADVERT)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(lease->addr_.toText());
|
|
|
|
} else {
|
|
|
|
LOG_INFO(lease4_logger, DHCP4_LEASE_ALLOC)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(lease->addr_.toText())
|
|
|
|
.arg(lease->valid_lft_);
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2017-09-18 12:11:51 +02:00
|
|
|
// We're logging this here, because this is the place where we know
|
|
|
|
// which subnet has been actually used for allocation. If the
|
|
|
|
// client identifier matching is disabled, we want to make sure that
|
|
|
|
// the user is notified.
|
|
|
|
if (!ctx->subnet_->getMatchClientId()) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_CLIENTID_IGNORED_FOR_LEASES)
|
|
|
|
.arg(ctx->query_->getLabel())
|
|
|
|
.arg(ctx->subnet_->getID());
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
resp->setYiaddr(lease->addr_);
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2014-10-07 16:07:48 +02:00
|
|
|
/// @todo The server should check what ciaddr the client has supplied
|
|
|
|
/// in ciaddr. Currently the ciaddr is ignored except for the subnet
|
|
|
|
/// selection. If the client supplied an invalid address, the server
|
|
|
|
/// will also return an invalid address here.
|
|
|
|
if (!fake_allocation) {
|
|
|
|
// If this is a renewing client it will set a ciaddr which the
|
|
|
|
// server may include in the response. If this is a new allocation
|
|
|
|
// the client will set ciaddr to 0 and this will also be propagated
|
2015-03-10 21:36:08 +01:00
|
|
|
// to the server's resp.
|
|
|
|
resp->setCiaddr(query->getCiaddr());
|
2014-10-07 16:07:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 10:46:31 +02:00
|
|
|
// We may need to update FQDN or hostname if the server is to generate
|
|
|
|
// new name from the allocated IP address or if the allocation engine
|
|
|
|
// has switched to a different subnet (from the same shared network)
|
|
|
|
// where the client has hostname reservations.
|
|
|
|
if (fqdn || opt_hostname) {
|
|
|
|
bool should_update = false;
|
|
|
|
|
|
|
|
// If there is a reservation in the current subnet for a hostname,
|
|
|
|
// we need to use this reserved name.
|
|
|
|
if (ctx->currentHost() && !ctx->currentHost()->getHostname().empty()) {
|
|
|
|
|
|
|
|
lease->hostname_ = CfgMgr::instance().getD2ClientMgr()
|
|
|
|
.qualifyName(ctx->currentHost()->getHostname(),
|
2019-10-03 15:11:07 -04:00
|
|
|
*(ex.getContext()->getDdnsParams()), static_cast<bool>(fqdn));
|
2017-09-18 10:46:31 +02:00
|
|
|
should_update = true;
|
|
|
|
|
|
|
|
// If there has been Client FQDN or Hostname option sent, but the
|
|
|
|
// hostname is empty, it means that server is responsible for
|
|
|
|
// generating the entire hostname for the client. The example of the
|
|
|
|
// client's name, generated from the IP address is: host-192-0-2-3.
|
|
|
|
} else if (lease->hostname_.empty()) {
|
|
|
|
|
|
|
|
// Note that if we have received the hostname option, rather than
|
|
|
|
// Client FQDN the trailing dot is not appended to the generated
|
|
|
|
// hostname because some clients don't handle the trailing dot in
|
|
|
|
// the hostname. Whether the trailing dot is appended or not is
|
|
|
|
// controlled by the second argument to the generateFqdn().
|
|
|
|
lease->hostname_ = CfgMgr::instance().getD2ClientMgr()
|
2019-10-03 15:11:07 -04:00
|
|
|
.generateFqdn(lease->addr_, *(ex.getContext()->getDdnsParams()),
|
|
|
|
static_cast<bool>(fqdn));
|
2017-09-18 10:46:31 +02:00
|
|
|
|
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_RESPONSE_HOSTNAME_GENERATE)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(lease->hostname_);
|
2015-04-22 15:57:37 +02:00
|
|
|
|
2017-09-18 10:46:31 +02:00
|
|
|
should_update = true;
|
|
|
|
}
|
2015-05-05 10:54:16 -04:00
|
|
|
|
2017-09-18 10:46:31 +02:00
|
|
|
if (should_update) {
|
|
|
|
|
|
|
|
// The operations below are rather safe, but we want to catch
|
|
|
|
// any potential exceptions (e.g. invalid lease database backend
|
|
|
|
// implementation) and log an error.
|
|
|
|
try {
|
|
|
|
if (!fake_allocation) {
|
|
|
|
// The lease update should be safe, because the lease should
|
|
|
|
// be already in the database. In most cases the exception
|
|
|
|
// would be thrown if the lease was missing.
|
|
|
|
LeaseMgrFactory::instance().updateLease4(lease);
|
|
|
|
}
|
2013-09-09 19:22:28 +02:00
|
|
|
|
2017-09-18 10:46:31 +02:00
|
|
|
// The name update in the option should be also safe,
|
|
|
|
// because the generated name is well formed.
|
|
|
|
if (fqdn) {
|
|
|
|
fqdn->setDomainName(lease->hostname_,
|
|
|
|
Option4ClientFqdn::FULL);
|
|
|
|
} else if (opt_hostname) {
|
|
|
|
opt_hostname->setValue(lease->hostname_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
LOG_ERROR(ddns4_logger, DHCP4_POST_ALLOCATION_NAME_UPDATE_FAIL)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(lease->hostname_)
|
|
|
|
.arg(ex.what());
|
|
|
|
}
|
2013-09-09 19:22:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// IP Address Lease time (type 51)
|
2015-03-12 20:20:37 +01:00
|
|
|
OptionPtr opt(new OptionUint32(Option::V4, DHO_DHCP_LEASE_TIME,
|
|
|
|
lease->valid_lft_));
|
2015-03-10 21:36:08 +01:00
|
|
|
resp->addOption(opt);
|
2012-12-31 16:49:37 +01:00
|
|
|
|
|
|
|
// Subnet mask (type 1)
|
2015-03-10 21:36:08 +01:00
|
|
|
resp->addOption(getNetmaskOption(subnet));
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2019-01-09 11:14:01 -05:00
|
|
|
// Set T1 and T2 per configuration.
|
|
|
|
setTeeTimes(lease, subnet, resp);
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2019-10-03 15:11:07 -04:00
|
|
|
|
2014-01-20 10:32:32 -05:00
|
|
|
// Create NameChangeRequests if DDNS is enabled and this is a
|
|
|
|
// real allocation.
|
2019-10-03 15:11:07 -04:00
|
|
|
if (!fake_allocation && (ex.getContext()->getDdnsParams()->enable_updates_)) {
|
2013-09-05 15:11:26 +02:00
|
|
|
try {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(ddns4_logger, DBG_DHCP4_DETAIL, DHCP4_NCR_CREATE)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(query->getLabel());
|
2015-03-10 21:36:08 +01:00
|
|
|
createNameChangeRequests(lease, ctx->old_lease_);
|
2015-04-22 15:57:37 +02:00
|
|
|
|
2013-09-05 15:11:26 +02:00
|
|
|
} catch (const Exception& ex) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_ERROR(ddns4_logger, DHCP4_NCR_CREATION_FAILED)
|
2015-04-22 15:57:37 +02:00
|
|
|
.arg(query->getLabel())
|
2013-09-05 15:11:26 +02:00
|
|
|
.arg(ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
} else {
|
|
|
|
// Allocation engine did not allocate a lease. The engine logged
|
2015-04-22 15:57:37 +02:00
|
|
|
// cause of that failure.
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, fake_allocation ?
|
2015-04-15 14:08:33 -04:00
|
|
|
DHCP4_PACKET_NAK_0003 : DHCP4_PACKET_NAK_0004)
|
2015-04-20 11:52:37 -04:00
|
|
|
.arg(query->getLabel())
|
2015-03-17 19:39:11 +01:00
|
|
|
.arg(query->getCiaddr().toText())
|
|
|
|
.arg(opt_requested_address ?
|
|
|
|
opt_requested_address->readAddress().toText() : "(no address)");
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
resp->setType(DHCPNAK);
|
|
|
|
resp->setYiaddr(IOAddress::IPV4_ZERO_ADDRESS());
|
2013-09-09 10:24:54 +02:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
resp->delOption(DHO_FQDN);
|
|
|
|
resp->delOption(DHO_HOST_NAME);
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:31:21 +01:00
|
|
|
void
|
2019-01-09 11:14:01 -05:00
|
|
|
Dhcpv4Srv::setTeeTimes(const Lease4Ptr& lease, const Subnet4Ptr& subnet, Pkt4Ptr resp) {
|
|
|
|
|
|
|
|
uint32_t t2_time = 0;
|
|
|
|
// If T2 is explicitly configured we'll use try value.
|
2019-01-10 15:31:21 +01:00
|
|
|
if (!subnet->getT2().unspecified()) {
|
|
|
|
t2_time = subnet->getT2();
|
|
|
|
} else if (subnet->getCalculateTeeTimes()) {
|
|
|
|
// Calculating tee times is enabled, so calculated it.
|
2019-05-14 15:42:34 -04:00
|
|
|
t2_time = static_cast<uint32_t>(round(subnet->getT2Percent() * (lease->valid_lft_)));
|
2019-01-09 11:14:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the T2 candidate value only if it's sane: to be sane it must be less than
|
|
|
|
// the valid life time.
|
|
|
|
uint32_t timer_ceiling = lease->valid_lft_;
|
|
|
|
if (t2_time > 0 && t2_time < timer_ceiling) {
|
|
|
|
OptionUint32Ptr t2(new OptionUint32(Option::V4, DHO_DHCP_REBINDING_TIME, t2_time));
|
|
|
|
resp->addOption(t2);
|
|
|
|
// When we send T2, timer ceiling for T1 becomes T2.
|
|
|
|
timer_ceiling = t2_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t t1_time = 0;
|
|
|
|
// If T1 is explicitly configured we'll use try value.
|
2019-01-10 15:31:21 +01:00
|
|
|
if (!subnet->getT1().unspecified()) {
|
|
|
|
t1_time = subnet->getT1();
|
|
|
|
} else if (subnet->getCalculateTeeTimes()) {
|
2019-01-09 11:14:01 -05:00
|
|
|
// Calculating tee times is enabled, so calculate it.
|
2019-05-14 15:42:34 -04:00
|
|
|
t1_time = static_cast<uint32_t>(round(subnet->getT1Percent() * (lease->valid_lft_)));
|
2019-01-09 11:14:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send T1 if it's sane: If we sent T2, T1 must be less than that. If not it must be
|
|
|
|
// less than the valid life time.
|
|
|
|
if (t1_time > 0 && t1_time < timer_ceiling) {
|
|
|
|
OptionUint32Ptr t1(new OptionUint32(Option::V4, DHO_DHCP_RENEWAL_TIME, t1_time));
|
|
|
|
resp->addOption(t1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-19 23:29:58 +01:00
|
|
|
uint16_t
|
|
|
|
Dhcpv4Srv::checkRelayPort(const Dhcpv4Exchange& ex) {
|
|
|
|
|
|
|
|
// Look for a relay-port RAI sub-option in the query.
|
|
|
|
const Pkt4Ptr& query = ex.getQuery();
|
|
|
|
const OptionPtr& rai = query->getOption(DHO_DHCP_AGENT_OPTIONS);
|
|
|
|
if (rai && rai->getOption(RAI_OPTION_RELAY_PORT)) {
|
|
|
|
// Got the sub-option so use the remote port set by the relay.
|
|
|
|
return (query->getRemotePort());
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:18:58 +02:00
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::adjustIfaceData(Dhcpv4Exchange& ex) {
|
2015-03-10 21:36:08 +01:00
|
|
|
adjustRemoteAddr(ex);
|
|
|
|
|
|
|
|
// Initialize the pointers to the client's message and the server's
|
|
|
|
// response.
|
|
|
|
Pkt4Ptr query = ex.getQuery();
|
|
|
|
Pkt4Ptr response = ex.getResponse();
|
2014-01-03 16:54:16 +01:00
|
|
|
|
2016-07-25 19:35:12 +02:00
|
|
|
// The DHCPINFORM is generally unicast to the client. The only situation
|
|
|
|
// when the server is unable to unicast to the client is when the client
|
|
|
|
// doesn't include ciaddr and the message is relayed. In this case the
|
|
|
|
// server has to reply via relay agent. For other messages we send back
|
|
|
|
// through relay if message is relayed, and unicast to the client if the
|
|
|
|
// message is not relayed.
|
2018-12-27 19:12:10 +01:00
|
|
|
// If client port was set from the command line enforce all responses
|
|
|
|
// to it. Of course it is only for testing purposes.
|
2014-01-08 21:09:15 +01:00
|
|
|
// Note that the call to this function may throw if invalid combination
|
|
|
|
// of hops and giaddr is found (hops = 0 if giaddr = 0 and hops != 0 if
|
|
|
|
// giaddr != 0). The exception will propagate down and eventually cause the
|
|
|
|
// packet to be discarded.
|
2018-12-27 19:12:10 +01:00
|
|
|
if (client_port_) {
|
|
|
|
response->setRemotePort(client_port_);
|
|
|
|
} else if (((query->getType() == DHCPINFORM) &&
|
2016-07-25 19:35:12 +02:00
|
|
|
((!query->getCiaddr().isV4Zero()) ||
|
|
|
|
(!query->isRelayed() && !query->getRemoteAddr().isV4Zero()))) ||
|
|
|
|
((query->getType() != DHCPINFORM) && !query->isRelayed())) {
|
|
|
|
response->setRemotePort(DHCP4_CLIENT_PORT);
|
|
|
|
|
|
|
|
} else {
|
2018-04-04 16:44:56 +02:00
|
|
|
// RFC 8357 section 5.1
|
2017-12-19 23:29:58 +01:00
|
|
|
uint16_t relay_port = checkRelayPort(ex);
|
|
|
|
response->setRemotePort(relay_port ? relay_port : DHCP4_SERVER_PORT);
|
2016-07-25 19:35:12 +02:00
|
|
|
}
|
2015-02-12 20:15:24 +01:00
|
|
|
|
2017-10-06 12:39:08 +02:00
|
|
|
CfgIfacePtr cfg_iface = CfgMgr::instance().getCurrentCfg()->getCfgIface();
|
|
|
|
if (query->isRelayed() &&
|
|
|
|
(cfg_iface->getSocketType() == CfgIface::SOCKET_UDP) &&
|
|
|
|
(cfg_iface->getOutboundIface() == CfgIface::USE_ROUTING)) {
|
2015-02-12 20:15:24 +01:00
|
|
|
|
2018-01-26 11:50:55 +01:00
|
|
|
// Mark the response to follow routing
|
2017-10-06 12:39:08 +02:00
|
|
|
response->setLocalAddr(IOAddress::IPV4_ZERO_ADDRESS());
|
|
|
|
response->resetIndex();
|
2018-01-26 11:50:55 +01:00
|
|
|
// But keep the interface name
|
|
|
|
response->setIface(query->getIface());
|
2017-10-06 12:39:08 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
IOAddress local_addr = query->getLocalAddr();
|
|
|
|
|
|
|
|
// In many cases the query is sent to a broadcast address. This address
|
|
|
|
// appears as a local address in the query message. We can't simply copy
|
|
|
|
// this address to a response message and use it as a source address.
|
|
|
|
// Instead we will need to use the address assigned to the interface
|
|
|
|
// on which the query has been received. In other cases, we will just
|
|
|
|
// use this address as a source address for the response.
|
|
|
|
// Do the same for DHCPv4-over-DHCPv6 exchanges.
|
|
|
|
if (local_addr.isV4Bcast() || query->isDhcp4o6()) {
|
|
|
|
SocketInfo sock_info = IfaceMgr::instance().getSocket(*query);
|
|
|
|
local_addr = sock_info.addr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume that there is an appropriate socket bound to this address
|
|
|
|
// and that the address is correct. This is safe assumption because
|
|
|
|
// the local address of the query is set when the query is received.
|
|
|
|
// The query sent to an incorrect address wouldn't have been received.
|
|
|
|
// However, if socket is closed for this address between the reception
|
|
|
|
// of the query and sending a response, the IfaceMgr should detect it
|
|
|
|
// and return an error.
|
|
|
|
response->setLocalAddr(local_addr);
|
|
|
|
// In many cases the query is sent to a broadcast address. This address
|
|
|
|
// appears as a local address in the query message. Therefore we can't
|
|
|
|
// simply copy local address from the query and use it as a source
|
|
|
|
// address for the response. Instead, we have to check what address our
|
|
|
|
// socket is bound to and use it as a source address. This operation
|
|
|
|
// may throw if for some reason the socket is closed.
|
|
|
|
/// @todo Consider an optimization that we use local address from
|
|
|
|
/// the query if this address is not broadcast.
|
|
|
|
response->setIndex(query->getIndex());
|
2018-01-26 11:50:55 +01:00
|
|
|
response->setIface(query->getIface());
|
2015-02-12 20:15:24 +01:00
|
|
|
}
|
2017-10-06 12:39:08 +02:00
|
|
|
|
2014-01-03 16:54:16 +01:00
|
|
|
response->setLocalPort(DHCP4_SERVER_PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-12 19:14:01 +01:00
|
|
|
Dhcpv4Srv::adjustRemoteAddr(Dhcpv4Exchange& ex) {
|
2015-03-10 21:36:08 +01:00
|
|
|
// Initialize the pointers to the client's message and the server's
|
|
|
|
// response.
|
|
|
|
Pkt4Ptr query = ex.getQuery();
|
|
|
|
Pkt4Ptr response = ex.getResponse();
|
|
|
|
|
2016-06-17 02:24:29 +02:00
|
|
|
// DHCPv4-over-DHCPv6 is simple
|
|
|
|
if (query->isDhcp4o6()) {
|
|
|
|
response->setRemoteAddr(query->getRemoteAddr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-15 12:06:40 +02:00
|
|
|
// The DHCPINFORM is slightly different than other messages in a sense
|
|
|
|
// that the server should always unicast the response to the ciaddr.
|
|
|
|
// It appears however that some clients don't set the ciaddr. We still
|
|
|
|
// want to provision these clients and we do what we can't to send the
|
|
|
|
// packet to the address where client can receive it.
|
2015-03-10 21:36:08 +01:00
|
|
|
if (query->getType() == DHCPINFORM) {
|
2014-07-15 12:06:40 +02:00
|
|
|
// If client adheres to RFC2131 it will set the ciaddr and in this
|
|
|
|
// case we always unicast our response to this address.
|
2015-09-19 17:15:44 +02:00
|
|
|
if (!query->getCiaddr().isV4Zero()) {
|
2015-03-10 21:36:08 +01:00
|
|
|
response->setRemoteAddr(query->getCiaddr());
|
2014-07-15 12:06:40 +02:00
|
|
|
|
2015-01-22 11:25:36 +01:00
|
|
|
// If we received DHCPINFORM via relay and the ciaddr is not set we
|
2014-07-15 12:06:40 +02:00
|
|
|
// will try to send the response via relay. The caveat is that the
|
|
|
|
// relay will not have any idea where to forward the packet because
|
|
|
|
// the yiaddr is likely not set. So, the broadcast flag is set so
|
|
|
|
// as the response may be broadcast.
|
2015-03-10 21:36:08 +01:00
|
|
|
} else if (query->isRelayed()) {
|
|
|
|
response->setRemoteAddr(query->getGiaddr());
|
2014-07-15 12:06:40 +02:00
|
|
|
response->setFlags(response->getFlags() | BOOTP_BROADCAST);
|
|
|
|
|
|
|
|
// If there is no ciaddr and no giaddr the only thing we can do is
|
|
|
|
// to use the source address of the packet.
|
|
|
|
} else {
|
2015-03-10 21:36:08 +01:00
|
|
|
response->setRemoteAddr(query->getRemoteAddr());
|
2014-07-15 12:06:40 +02:00
|
|
|
}
|
2015-01-22 11:25:36 +01:00
|
|
|
// Remote address is now set so return.
|
2014-07-15 12:06:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:18:58 +02:00
|
|
|
// If received relayed message, server responds to the relay address.
|
2015-03-10 21:36:08 +01:00
|
|
|
if (query->isRelayed()) {
|
2014-07-18 18:32:59 +02:00
|
|
|
// The client should set the ciaddr when sending the DHCPINFORM
|
|
|
|
// but in case he didn't, the relay may not be able to determine the
|
|
|
|
// address of the client, because yiaddr is not set when responding
|
|
|
|
// to Confirm and the only address available was the source address
|
|
|
|
// of the client. The source address is however not used here because
|
|
|
|
// the message is relayed. Therefore, we set the BROADCAST flag so
|
|
|
|
// as the relay can broadcast the packet.
|
2015-03-10 21:36:08 +01:00
|
|
|
if ((query->getType() == DHCPINFORM) &&
|
2015-09-19 17:15:44 +02:00
|
|
|
query->getCiaddr().isV4Zero()) {
|
2014-07-15 12:06:40 +02:00
|
|
|
response->setFlags(BOOTP_BROADCAST);
|
|
|
|
}
|
2015-03-10 21:36:08 +01:00
|
|
|
response->setRemoteAddr(query->getGiaddr());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
// If giaddr is 0 but client set ciaddr, server should unicast the
|
|
|
|
// response to ciaddr.
|
2015-09-19 17:15:44 +02:00
|
|
|
} else if (!query->getCiaddr().isV4Zero()) {
|
2015-03-10 21:36:08 +01:00
|
|
|
response->setRemoteAddr(query->getCiaddr());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
// We can't unicast the response to the client when sending NAK,
|
|
|
|
// because we haven't allocated address for him. Therefore,
|
|
|
|
// NAK is broadcast.
|
2014-01-03 16:54:16 +01:00
|
|
|
} else if (response->getType() == DHCPNAK) {
|
2015-02-12 20:15:24 +01:00
|
|
|
response->setRemoteAddr(IOAddress::IPV4_BCAST_ADDRESS());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
// If yiaddr is set it means that we have created a lease for a client.
|
2015-09-19 17:15:44 +02:00
|
|
|
} else if (!response->getYiaddr().isV4Zero()) {
|
2013-05-21 15:18:58 +02:00
|
|
|
// If the broadcast bit is set in the flags field, we have to
|
|
|
|
// send the response to broadcast address. Client may have requested it
|
|
|
|
// because it doesn't support reception of messages on the interface
|
|
|
|
// which doesn't have an address assigned. The other case when response
|
|
|
|
// must be broadcasted is when our server does not support responding
|
|
|
|
// directly to a client without address assigned.
|
2015-03-10 21:36:08 +01:00
|
|
|
const bool bcast_flag = ((query->getFlags() & Pkt4::FLAG_BROADCAST_MASK) != 0);
|
2013-05-21 15:18:58 +02:00
|
|
|
if (!IfaceMgr::instance().isDirectResponseSupported() || bcast_flag) {
|
2015-02-12 20:15:24 +01:00
|
|
|
response->setRemoteAddr(IOAddress::IPV4_BCAST_ADDRESS());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
// Client cleared the broadcast bit and we support direct responses
|
|
|
|
// so we should unicast the response to a newly allocated address -
|
|
|
|
// yiaddr.
|
|
|
|
} else {
|
2014-01-03 16:54:16 +01:00
|
|
|
response->setRemoteAddr(response ->getYiaddr());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// In most cases, we should have the remote address found already. If we
|
|
|
|
// found ourselves at this point, the rational thing to do is to respond
|
|
|
|
// to the address we got the query from.
|
|
|
|
} else {
|
2015-03-10 21:36:08 +01:00
|
|
|
response->setRemoteAddr(query->getRemoteAddr());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:17:19 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::setFixedFields(Dhcpv4Exchange& ex) {
|
|
|
|
Pkt4Ptr query = ex.getQuery();
|
|
|
|
Pkt4Ptr response = ex.getResponse();
|
|
|
|
|
2016-08-26 13:41:15 +02:00
|
|
|
// Step 1: Start with fixed fields defined on subnet level.
|
|
|
|
Subnet4Ptr subnet = ex.getContext()->subnet_;
|
|
|
|
if (subnet) {
|
|
|
|
IOAddress subnet_next_server = subnet->getSiaddr();
|
|
|
|
if (!subnet_next_server.isV4Zero()) {
|
|
|
|
response->setSiaddr(subnet_next_server);
|
2016-08-24 13:17:19 +02:00
|
|
|
}
|
2017-10-01 23:20:38 +02:00
|
|
|
|
|
|
|
const string& sname = subnet->getSname();
|
|
|
|
if (!sname.empty()) {
|
|
|
|
// Converting string to (const uint8_t*, size_t len) format is
|
|
|
|
// tricky. reinterpret_cast is not the most elegant solution,
|
|
|
|
// but it does avoid us making unnecessary copy. We will convert
|
|
|
|
// sname and file fields in Pkt4 to string one day and life
|
|
|
|
// will be easier.
|
|
|
|
response->setSname(reinterpret_cast<const uint8_t*>(sname.c_str()),
|
|
|
|
sname.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
const string& filename = subnet->getFilename();
|
|
|
|
if (!filename.empty()) {
|
|
|
|
// Converting string to (const uint8_t*, size_t len) format is
|
|
|
|
// tricky. reinterpret_cast is not the most elegant solution,
|
|
|
|
// but it does avoid us making unnecessary copy. We will convert
|
|
|
|
// sname and file fields in Pkt4 to string one day and life
|
|
|
|
// will be easier.
|
|
|
|
response->setFile(reinterpret_cast<const uint8_t*>(filename.c_str()),
|
|
|
|
filename.size());
|
|
|
|
}
|
2016-08-26 13:41:15 +02:00
|
|
|
}
|
2016-08-24 13:17:19 +02:00
|
|
|
|
2016-08-26 13:41:15 +02:00
|
|
|
// Step 2: Try to set the values based on classes.
|
|
|
|
// Any values defined in classes will override those from subnet level.
|
|
|
|
const ClientClasses classes = query->getClasses();
|
|
|
|
if (!classes.empty()) {
|
|
|
|
|
|
|
|
// Let's get class definitions
|
2017-11-24 18:39:44 +01:00
|
|
|
const ClientClassDictionaryPtr& dict =
|
|
|
|
CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
|
2016-08-26 13:41:15 +02:00
|
|
|
|
|
|
|
// Now we need to iterate over the classes assigned to the
|
2017-01-24 23:18:58 +01:00
|
|
|
// query packet and find corresponding class definitions for it.
|
2017-11-24 01:07:17 +01:00
|
|
|
for (ClientClasses::const_iterator name = classes.cbegin();
|
|
|
|
name != classes.cend(); ++name) {
|
2016-08-26 13:41:15 +02:00
|
|
|
|
2017-11-24 18:39:44 +01:00
|
|
|
ClientClassDefPtr cl = dict->findClass(*name);
|
|
|
|
if (!cl) {
|
2016-08-26 13:41:15 +02:00
|
|
|
// Let's skip classes that don't have definitions. Currently
|
|
|
|
// these are automatic classes VENDOR_CLASS_something, but there
|
|
|
|
// may be other classes assigned under other circumstances, e.g.
|
|
|
|
// by hooks.
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-24 13:17:19 +02:00
|
|
|
|
2017-11-24 18:39:44 +01:00
|
|
|
IOAddress next_server = cl->getNextServer();
|
2016-08-26 13:41:15 +02:00
|
|
|
if (!next_server.isV4Zero()) {
|
|
|
|
response->setSiaddr(next_server);
|
|
|
|
}
|
2016-08-24 13:17:19 +02:00
|
|
|
|
2017-11-24 18:39:44 +01:00
|
|
|
const string& sname = cl->getSname();
|
2016-08-26 13:41:15 +02:00
|
|
|
if (!sname.empty()) {
|
|
|
|
// Converting string to (const uint8_t*, size_t len) format is
|
2017-01-24 23:18:58 +01:00
|
|
|
// tricky. reinterpret_cast is not the most elegant solution,
|
2016-08-26 13:41:15 +02:00
|
|
|
// but it does avoid us making unnecessary copy. We will convert
|
2016-08-26 13:53:09 +02:00
|
|
|
// sname and file fields in Pkt4 to string one day and life
|
2016-08-26 13:41:15 +02:00
|
|
|
// will be easier.
|
|
|
|
response->setSname(reinterpret_cast<const uint8_t*>(sname.c_str()),
|
|
|
|
sname.size());
|
|
|
|
}
|
2016-12-14 16:57:44 +02:00
|
|
|
|
2017-11-24 18:39:44 +01:00
|
|
|
const string& filename = cl->getFilename();
|
2016-08-26 13:41:15 +02:00
|
|
|
if (!filename.empty()) {
|
|
|
|
// Converting string to (const uint8_t*, size_t len) format is
|
2017-01-24 23:18:58 +01:00
|
|
|
// tricky. reinterpret_cast is not the most elegant solution,
|
2016-08-26 13:41:15 +02:00
|
|
|
// but it does avoid us making unnecessary copy. We will convert
|
2016-08-26 13:53:09 +02:00
|
|
|
// sname and file fields in Pkt4 to string one day and life
|
2016-08-26 13:41:15 +02:00
|
|
|
// will be easier.
|
|
|
|
response->setFile(reinterpret_cast<const uint8_t*>(filename.c_str()),
|
|
|
|
filename.size());
|
|
|
|
}
|
2016-08-24 13:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 13:41:15 +02:00
|
|
|
// Step 3: try to set values using HR. Any values coming from there will override
|
|
|
|
// the subnet or class values.
|
|
|
|
ex.setReservedMessageFields();
|
2016-08-24 18:51:28 +02:00
|
|
|
}
|
2013-05-21 15:18:58 +02:00
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
OptionPtr
|
|
|
|
Dhcpv4Srv::getNetmaskOption(const Subnet4Ptr& subnet) {
|
2016-09-20 09:29:06 +02:00
|
|
|
uint32_t netmask = getNetmask4(subnet->get().second).toUint32();
|
2012-12-31 16:49:37 +01:00
|
|
|
|
|
|
|
OptionPtr opt(new OptionInt<uint32_t>(Option::V4,
|
|
|
|
DHO_SUBNET_MASK, netmask));
|
|
|
|
|
|
|
|
return (opt);
|
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
Pkt4Ptr
|
|
|
|
Dhcpv4Srv::processDiscover(Pkt4Ptr& discover) {
|
2019-09-04 11:38:07 -04:00
|
|
|
// server-id is forbidden.
|
2013-07-16 13:58:01 +02:00
|
|
|
sanityCheck(discover, FORBIDDEN);
|
|
|
|
|
2017-12-15 18:30:59 +01:00
|
|
|
bool drop = false;
|
|
|
|
Dhcpv4Exchange ex(alloc_engine_, discover, selectSubnet(discover, drop));
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Stop here if selectSubnet decided to drop the packet
|
2017-12-15 18:30:59 +01:00
|
|
|
if (drop) {
|
2017-12-15 00:53:20 +01:00
|
|
|
return (Pkt4Ptr());
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
// If DHCPDISCOVER message contains the FQDN or Hostname option, server
|
2013-09-09 10:24:54 +02:00
|
|
|
// may respond to the client with the appropriate FQDN or Hostname
|
|
|
|
// option to indicate that whether it will take responsibility for
|
2015-03-10 21:36:08 +01:00
|
|
|
// updating DNS when the client sends DHCPREQUEST message.
|
|
|
|
processClientName(ex);
|
2013-09-09 10:24:54 +02:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
assignLease(ex);
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
if (!ex.getResponse()) {
|
2015-01-22 10:40:51 +01:00
|
|
|
// The offer is empty so return it *now*!
|
2015-03-10 21:36:08 +01:00
|
|
|
return (Pkt4Ptr());
|
2015-01-22 10:40:51 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 14:41:54 +02:00
|
|
|
// Adding any other options makes sense only when we got the lease.
|
2015-03-10 21:36:08 +01:00
|
|
|
if (!ex.getResponse()->getYiaddr().isV4Zero()) {
|
2017-11-27 02:59:59 +01:00
|
|
|
// Assign reserved classes.
|
|
|
|
ex.setReservedClientClasses();
|
2018-04-06 00:12:52 +02:00
|
|
|
// Required classification
|
|
|
|
requiredClassify(ex);
|
2017-11-27 02:59:59 +01:00
|
|
|
|
2015-11-21 14:45:08 +01:00
|
|
|
buildCfgOptionList(ex);
|
2015-03-10 21:36:08 +01:00
|
|
|
appendRequestedOptions(ex);
|
|
|
|
appendRequestedVendorOptions(ex);
|
2013-10-21 14:41:54 +02:00
|
|
|
// There are a few basic options that we always want to
|
|
|
|
// include in the response. If client did not request
|
|
|
|
// them we append them for him.
|
2015-03-10 21:36:08 +01:00
|
|
|
appendBasicOptions(ex);
|
2014-12-15 10:56:16 +01:00
|
|
|
|
2016-08-25 16:44:55 +02:00
|
|
|
// Set fixed fields (siaddr, sname, filename) if defined in
|
|
|
|
// the reservation, class or subnet specific configuration.
|
2016-08-24 14:51:19 +02:00
|
|
|
setFixedFields(ex);
|
|
|
|
|
2014-12-15 10:56:16 +01:00
|
|
|
} else {
|
|
|
|
// If the server can't offer an address, it drops the packet.
|
|
|
|
return (Pkt4Ptr());
|
|
|
|
|
2013-10-21 14:41:54 +02:00
|
|
|
}
|
2013-01-22 18:40:55 +01:00
|
|
|
|
2014-01-03 16:54:16 +01:00
|
|
|
// Set the src/dest IP address, port and interface for the outgoing
|
|
|
|
// packet.
|
2015-03-10 21:36:08 +01:00
|
|
|
adjustIfaceData(ex);
|
2014-01-03 16:54:16 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
appendServerID(ex);
|
2014-01-03 16:54:16 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
return (ex.getResponse());
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
Pkt4Ptr
|
2018-01-18 20:02:07 +01:00
|
|
|
Dhcpv4Srv::processRequest(Pkt4Ptr& request, AllocEngine::ClientContext4Ptr& context) {
|
2019-09-04 11:38:07 -04:00
|
|
|
// Since we cannot distinguish between client states
|
|
|
|
// we'll make server-id is optional for REQUESTs.
|
|
|
|
sanityCheck(request, OPTIONAL);
|
2013-08-20 15:18:27 +02:00
|
|
|
|
2017-12-15 18:30:59 +01:00
|
|
|
bool drop = false;
|
|
|
|
Dhcpv4Exchange ex(alloc_engine_, request, selectSubnet(request, drop));
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Stop here if selectSubnet decided to drop the packet
|
2017-12-15 18:30:59 +01:00
|
|
|
if (drop) {
|
2017-12-15 00:53:20 +01:00
|
|
|
return (Pkt4Ptr());
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
// If DHCPREQUEST message contains the FQDN or Hostname option, server
|
2013-09-09 10:24:54 +02:00
|
|
|
// should respond to the client with the appropriate FQDN or Hostname
|
|
|
|
// option to indicate if it takes responsibility for the DNS updates.
|
|
|
|
// This is performed by the function below.
|
2015-03-10 21:36:08 +01:00
|
|
|
processClientName(ex);
|
2013-09-09 10:24:54 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Note that we treat REQUEST message uniformly, regardless if this is a
|
|
|
|
// first request (requesting for new address), renewing existing address
|
|
|
|
// or even rebinding.
|
2015-03-10 21:36:08 +01:00
|
|
|
assignLease(ex);
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
if (!ex.getResponse()) {
|
2015-01-22 10:40:51 +01:00
|
|
|
// The ack is empty so return it *now*!
|
2015-03-10 21:36:08 +01:00
|
|
|
return (Pkt4Ptr());
|
2015-01-22 10:40:51 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 14:41:54 +02:00
|
|
|
// Adding any other options makes sense only when we got the lease.
|
2015-03-10 21:36:08 +01:00
|
|
|
if (!ex.getResponse()->getYiaddr().isV4Zero()) {
|
2017-11-27 02:59:59 +01:00
|
|
|
// Assign reserved classes.
|
|
|
|
ex.setReservedClientClasses();
|
2018-04-06 00:12:52 +02:00
|
|
|
// Required classification
|
|
|
|
requiredClassify(ex);
|
2017-11-27 02:59:59 +01:00
|
|
|
|
2015-11-21 14:45:08 +01:00
|
|
|
buildCfgOptionList(ex);
|
2015-03-10 21:36:08 +01:00
|
|
|
appendRequestedOptions(ex);
|
|
|
|
appendRequestedVendorOptions(ex);
|
2013-10-21 14:41:54 +02:00
|
|
|
// There are a few basic options that we always want to
|
|
|
|
// include in the response. If client did not request
|
|
|
|
// them we append them for him.
|
2015-03-10 21:36:08 +01:00
|
|
|
appendBasicOptions(ex);
|
2016-08-24 14:51:19 +02:00
|
|
|
|
2016-08-25 16:44:55 +02:00
|
|
|
// Set fixed fields (siaddr, sname, filename) if defined in
|
|
|
|
// the reservation, class or subnet specific configuration.
|
2016-08-24 14:51:19 +02:00
|
|
|
setFixedFields(ex);
|
2013-10-21 14:41:54 +02:00
|
|
|
}
|
2013-01-22 18:40:55 +01:00
|
|
|
|
2014-01-03 16:54:16 +01:00
|
|
|
// Set the src/dest IP address, port and interface for the outgoing
|
|
|
|
// packet.
|
2015-03-10 21:36:08 +01:00
|
|
|
adjustIfaceData(ex);
|
2014-01-03 16:54:16 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
appendServerID(ex);
|
2014-01-03 16:54:16 +01:00
|
|
|
|
2018-01-18 20:02:07 +01:00
|
|
|
// Return the pointer to the context, which will be required by the
|
|
|
|
// leases4_comitted callouts.
|
|
|
|
context = ex.getContext();
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
return (ex.getResponse());
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
2018-01-18 20:02:07 +01:00
|
|
|
Dhcpv4Srv::processRelease(Pkt4Ptr& release, AllocEngine::ClientContext4Ptr& context) {
|
2019-09-04 11:38:07 -04:00
|
|
|
// Server-id is mandatory in DHCPRELEASE (see table 5, RFC2131)
|
|
|
|
// but ISC DHCP does not enforce this, so we'll follow suit.
|
|
|
|
sanityCheck(release, OPTIONAL);
|
2013-08-20 15:18:27 +02:00
|
|
|
|
2015-05-12 19:01:27 +02:00
|
|
|
// Try to find client-id. Note that for the DHCPRELEASE we don't check if the
|
2015-05-13 19:41:48 +02:00
|
|
|
// match-client-id configuration parameter is disabled because this parameter
|
2015-05-12 19:01:27 +02:00
|
|
|
// is configured for subnets and we don't select subnet for the DHCPRELEASE.
|
|
|
|
// Bogus clients usually generate new client identifiers when they first
|
|
|
|
// connect to the network, so whatever client identifier has been used to
|
|
|
|
// acquire the lease, the client identifier carried in the DHCPRELEASE is
|
|
|
|
// likely to be the same and the lease will be correctly identified in the
|
|
|
|
// lease database. If supplied client identifier differs from the one used
|
|
|
|
// to acquire the lease then the lease will remain in the database and
|
|
|
|
// simply expire.
|
2012-12-31 16:49:37 +01:00
|
|
|
ClientIdPtr client_id;
|
|
|
|
OptionPtr opt = release->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
|
|
|
if (opt) {
|
|
|
|
client_id = ClientIdPtr(new ClientId(opt->getData()));
|
|
|
|
}
|
|
|
|
|
2013-01-08 13:53:34 +01:00
|
|
|
try {
|
|
|
|
// Do we have a lease for that particular address?
|
2013-09-09 10:24:54 +02:00
|
|
|
Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(release->getCiaddr());
|
2013-01-08 13:53:34 +01:00
|
|
|
|
|
|
|
if (!lease) {
|
|
|
|
// No such lease - bogus release
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(lease4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_NO_LEASE)
|
2015-05-04 15:07:22 +02:00
|
|
|
.arg(release->getLabel())
|
|
|
|
.arg(release->getCiaddr().toText());
|
2013-01-08 13:53:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2015-05-12 09:16:19 +02:00
|
|
|
if (!lease->belongsToClient(release->getHWAddr(), client_id)) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(lease4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_WRONG_CLIENT)
|
2015-05-04 15:07:22 +02:00
|
|
|
.arg(release->getLabel())
|
2015-05-19 14:29:18 +02:00
|
|
|
.arg(release->getCiaddr().toText());
|
2013-01-08 13:53:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
bool skip = false;
|
2013-01-08 13:53:34 +01:00
|
|
|
|
2013-08-23 11:26:36 +02:00
|
|
|
// Execute all callouts registered for lease4_release
|
2013-10-10 18:02:06 +02:00
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_lease4_release_)) {
|
2013-08-19 15:08:08 +02:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(release);
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2013-01-08 13:53:34 +01:00
|
|
|
|
2016-07-06 12:28:49 +02:00
|
|
|
// Enable copying options from the packet within hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query4_options_copy(release);
|
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Pass the original packet
|
|
|
|
callout_handle->setArgument("query4", release);
|
|
|
|
|
|
|
|
// Pass the lease to be updated
|
|
|
|
callout_handle->setArgument("lease4", lease);
|
|
|
|
|
|
|
|
// Call all installed callouts
|
2013-08-23 11:26:36 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_lease4_release_,
|
|
|
|
*callout_handle);
|
2013-08-19 15:08:08 +02:00
|
|
|
|
|
|
|
// Callouts decided to skip the next processing step. The next
|
|
|
|
// processing step would to send the packet, so skip at this
|
|
|
|
// stage means "drop response".
|
2017-12-15 00:53:20 +01:00
|
|
|
if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
|
|
|
|
(callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
|
2013-08-19 15:08:08 +02:00
|
|
|
skip = true;
|
2015-05-15 14:27:46 +02:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS,
|
2015-05-04 15:07:22 +02:00
|
|
|
DHCP4_HOOK_LEASE4_RELEASE_SKIP)
|
|
|
|
.arg(release->getLabel());
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 13:53:34 +01:00
|
|
|
|
2015-05-04 15:07:22 +02:00
|
|
|
// Callout didn't indicate to skip the release process. Let's release
|
|
|
|
// the lease.
|
2013-08-19 15:08:08 +02:00
|
|
|
if (!skip) {
|
2013-08-26 15:21:37 +02:00
|
|
|
bool success = LeaseMgrFactory::instance().deleteLease(lease->addr_);
|
2013-08-19 15:08:08 +02:00
|
|
|
|
|
|
|
if (success) {
|
2018-01-18 20:02:07 +01:00
|
|
|
|
|
|
|
context.reset(new AllocEngine::ClientContext4());
|
|
|
|
context->old_lease_ = lease;
|
|
|
|
|
2013-09-09 10:24:54 +02:00
|
|
|
// Release successful
|
2016-03-23 08:28:29 -04:00
|
|
|
LOG_INFO(lease4_logger, DHCP4_RELEASE)
|
2015-05-04 15:07:22 +02:00
|
|
|
.arg(release->getLabel())
|
|
|
|
.arg(lease->addr_.toText());
|
2013-09-09 10:24:54 +02:00
|
|
|
|
2015-05-27 14:29:06 +02:00
|
|
|
// Need to decrease statistic for assigned addresses.
|
2015-06-15 12:25:12 +02:00
|
|
|
StatsMgr::instance().addValue(
|
|
|
|
StatsMgr::generateName("subnet", lease->subnet_id_, "assigned-addresses"),
|
|
|
|
static_cast<int64_t>(-1));
|
2015-05-27 14:29:06 +02:00
|
|
|
|
2015-10-19 15:30:01 +02:00
|
|
|
// Remove existing DNS entries for the lease, if any.
|
|
|
|
queueNCR(CHG_REMOVE, lease);
|
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
} else {
|
2015-05-04 15:07:22 +02:00
|
|
|
// Release failed
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_ERROR(lease4_logger, DHCP4_RELEASE_FAIL)
|
2015-05-04 15:07:22 +02:00
|
|
|
.arg(release->getLabel())
|
|
|
|
.arg(lease->addr_.toText());
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
2013-01-08 13:53:34 +01:00
|
|
|
}
|
|
|
|
} catch (const isc::Exception& ex) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_ERROR(lease4_logger, DHCP4_RELEASE_EXCEPTION)
|
2015-05-04 15:07:22 +02:00
|
|
|
.arg(release->getLabel())
|
|
|
|
.arg(release->getCiaddr())
|
|
|
|
.arg(ex.what());
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2011-11-30 19:00:57 +01:00
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
2018-01-18 20:02:07 +01:00
|
|
|
Dhcpv4Srv::processDecline(Pkt4Ptr& decline, AllocEngine::ClientContext4Ptr& context) {
|
2015-09-08 14:35:28 +02:00
|
|
|
// Server-id is mandatory in DHCPDECLINE (see table 5, RFC2131)
|
2019-09-04 11:38:07 -04:00
|
|
|
// but ISC DHCP does not enforce this, so we'll follow suit.
|
|
|
|
sanityCheck(decline, OPTIONAL);
|
2015-09-08 14:35:28 +02:00
|
|
|
|
|
|
|
// Client is supposed to specify the address being declined in
|
|
|
|
// Requested IP address option, but must not set its ciaddr.
|
|
|
|
// (again, see table 5 in RFC2131).
|
|
|
|
|
|
|
|
OptionCustomPtr opt_requested_address = boost::dynamic_pointer_cast<
|
2015-09-08 19:08:42 +02:00
|
|
|
OptionCustom>(decline->getOption(DHO_DHCP_REQUESTED_ADDRESS));
|
2015-09-08 14:35:28 +02:00
|
|
|
if (!opt_requested_address) {
|
|
|
|
|
|
|
|
isc_throw(RFCViolation, "Mandatory 'Requested IP address' option missing"
|
2019-09-04 11:38:07 -04:00
|
|
|
" in DHCPDECLINE sent from " << decline->getLabel());
|
2015-09-08 14:35:28 +02:00
|
|
|
}
|
|
|
|
IOAddress addr(opt_requested_address->readAddress());
|
|
|
|
|
|
|
|
// We could also extract client's address from ciaddr, but that's clearly
|
|
|
|
// against RFC2131.
|
|
|
|
|
|
|
|
// Now we need to check whether this address really belongs to the client
|
|
|
|
// that attempts to decline it.
|
2015-09-08 19:08:42 +02:00
|
|
|
const Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(addr);
|
2015-09-08 14:35:28 +02:00
|
|
|
|
|
|
|
if (!lease) {
|
|
|
|
// Client tried to decline an address, but we don't have a lease for
|
|
|
|
// that address. Let's ignore it.
|
|
|
|
//
|
|
|
|
// We could assume that we're recovering from a mishandled migration
|
|
|
|
// to a new server and mark the address as declined, but the window of
|
|
|
|
// opportunity for that to be useful is small and the attack vector
|
|
|
|
// would be pretty severe.
|
|
|
|
LOG_WARN(dhcp4_logger, DHCP4_DECLINE_LEASE_NOT_FOUND)
|
2015-09-08 19:08:42 +02:00
|
|
|
.arg(addr.toText()).arg(decline->getLabel());
|
2015-09-08 14:35:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get client-id, if available.
|
2015-09-08 19:08:42 +02:00
|
|
|
OptionPtr opt_clientid = decline->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
2015-09-08 14:35:28 +02:00
|
|
|
ClientIdPtr client_id;
|
|
|
|
if (opt_clientid) {
|
|
|
|
client_id.reset(new ClientId(opt_clientid->getData()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the client attempted to decline a lease it doesn't own.
|
2015-09-08 19:08:42 +02:00
|
|
|
if (!lease->belongsToClient(decline->getHWAddr(), client_id)) {
|
2015-09-08 14:35:28 +02:00
|
|
|
|
|
|
|
// Get printable hardware addresses
|
|
|
|
string client_hw = decline->getHWAddr() ?
|
|
|
|
decline->getHWAddr()->toText(false) : "(none)";
|
|
|
|
string lease_hw = lease->hwaddr_ ?
|
2015-09-08 19:08:42 +02:00
|
|
|
lease->hwaddr_->toText(false) : "(none)";
|
2015-09-08 14:35:28 +02:00
|
|
|
|
|
|
|
// Get printable client-ids
|
2015-09-08 19:08:42 +02:00
|
|
|
string client_id_txt = client_id ? client_id->toText() : "(none)";
|
|
|
|
string lease_id_txt = lease->client_id_ ?
|
|
|
|
lease->client_id_->toText() : "(none)";
|
2015-09-08 14:35:28 +02:00
|
|
|
|
2015-09-08 19:08:42 +02:00
|
|
|
// Print the warning and we're done here.
|
2015-09-08 14:35:28 +02:00
|
|
|
LOG_WARN(dhcp4_logger, DHCP4_DECLINE_LEASE_MISMATCH)
|
2015-09-08 19:08:42 +02:00
|
|
|
.arg(addr.toText()).arg(decline->getLabel())
|
|
|
|
.arg(client_hw).arg(lease_hw).arg(client_id_txt).arg(lease_id_txt);
|
|
|
|
|
2015-09-08 14:35:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, all is good. The client is reporting its own address. Let's
|
|
|
|
// process it.
|
2018-01-18 20:02:07 +01:00
|
|
|
declineLease(lease, decline, context);
|
2015-09-08 14:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-18 20:02:07 +01:00
|
|
|
Dhcpv4Srv::declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline,
|
|
|
|
AllocEngine::ClientContext4Ptr& context) {
|
2015-10-06 23:51:33 +02:00
|
|
|
|
|
|
|
// Let's check if there are hooks installed for decline4 hook point.
|
|
|
|
// If they are, let's pass the lease and client's packet. If the hook
|
|
|
|
// sets status to drop, we reject this Decline.
|
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_lease4_decline_)) {
|
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(decline);
|
|
|
|
|
2018-06-29 15:33:32 +02:00
|
|
|
// Use the RAII wrapper to make sure that the callout handle state is
|
|
|
|
// reset when this object goes out of scope. All hook points must do
|
|
|
|
// it to prevent possible circular dependency between the callout
|
|
|
|
// handle and its arguments.
|
|
|
|
ScopedCalloutHandleState callout_handle_state(callout_handle);
|
2015-10-06 23:51:33 +02:00
|
|
|
|
2016-07-06 12:28:49 +02:00
|
|
|
// Enable copying options from the packet within hook library.
|
|
|
|
ScopedEnableOptionsCopy<Pkt4> query4_options_copy(decline);
|
|
|
|
|
2015-10-06 23:51:33 +02:00
|
|
|
// Pass incoming Decline and the lease to be declined.
|
|
|
|
callout_handle->setArgument("lease4", lease);
|
|
|
|
callout_handle->setArgument("query4", decline);
|
|
|
|
|
|
|
|
// Call callouts
|
|
|
|
HooksManager::callCallouts(Hooks.hook_index_lease4_decline_,
|
|
|
|
*callout_handle);
|
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Check if callouts decided to skip the next processing step.
|
|
|
|
// If any of them did, we will drop the packet.
|
|
|
|
if ((callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) ||
|
|
|
|
(callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP)) {
|
2015-10-06 23:51:33 +02:00
|
|
|
LOG_DEBUG(hooks_logger, DBG_DHCP4_HOOKS, DHCP4_HOOK_DECLINE_SKIP)
|
|
|
|
.arg(decline->getLabel()).arg(lease->addr_.toText());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 14:35:28 +02:00
|
|
|
|
2015-10-19 15:30:01 +02:00
|
|
|
// Remove existing DNS entries for the lease, if any.
|
|
|
|
// queueNCR will do the necessary checks and will skip the update, if not needed.
|
|
|
|
queueNCR(CHG_REMOVE, lease);
|
2015-09-08 14:35:28 +02:00
|
|
|
|
|
|
|
// Bump up the statistics.
|
2015-09-16 15:55:25 +02:00
|
|
|
|
|
|
|
// Per subnet declined addresses counter.
|
|
|
|
StatsMgr::instance().addValue(
|
|
|
|
StatsMgr::generateName("subnet", lease->subnet_id_, "declined-addresses"),
|
|
|
|
static_cast<int64_t>(1));
|
|
|
|
|
|
|
|
// Global declined addresses counter.
|
|
|
|
StatsMgr::instance().addValue("declined-addresses", static_cast<int64_t>(1));
|
|
|
|
|
|
|
|
// We do not want to decrease the assigned-addresses at this time. While
|
2015-09-16 17:11:14 -07:00
|
|
|
// technically a declined address is no longer allocated, the primary usage
|
|
|
|
// of the assigned-addresses statistic is to monitor pool utilization. Most
|
2015-09-16 15:55:25 +02:00
|
|
|
// people would forget to include declined-addresses in the calculation,
|
|
|
|
// and simply do assigned-addresses/total-addresses. This would have a bias
|
|
|
|
// towards under-representing pool utilization, if we decreased allocated
|
|
|
|
// immediately after receiving DHCPDECLINE, rather than later when we recover
|
|
|
|
// the address.
|
2015-09-08 14:35:28 +02:00
|
|
|
|
|
|
|
// @todo: Call hooks.
|
|
|
|
|
|
|
|
// We need to disassociate the lease from the client. Once we move a lease
|
|
|
|
// to declined state, it is no longer associated with the client in any
|
|
|
|
// way.
|
2015-09-18 18:07:39 +02:00
|
|
|
lease->decline(CfgMgr::instance().getCurrentCfg()->getDeclinePeriod());
|
|
|
|
|
2015-09-08 19:08:42 +02:00
|
|
|
LeaseMgrFactory::instance().updateLease4(lease);
|
2015-09-08 14:35:28 +02:00
|
|
|
|
2018-01-18 20:02:07 +01:00
|
|
|
context.reset(new AllocEngine::ClientContext4());
|
|
|
|
context->new_lease_ = lease;
|
|
|
|
|
2016-03-23 08:28:29 -04:00
|
|
|
LOG_INFO(lease4_logger, DHCP4_DECLINE_LEASE).arg(lease->addr_.toText())
|
2015-10-06 23:51:33 +02:00
|
|
|
.arg(decline->getLabel()).arg(lease->valid_lft_);
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
Pkt4Ptr
|
|
|
|
Dhcpv4Srv::processInform(Pkt4Ptr& inform) {
|
2019-09-04 11:38:07 -04:00
|
|
|
// server-id is supposed to be forbidden (as is requested address)
|
|
|
|
// but ISC DHCP does not enforce either. So neither will we.
|
|
|
|
sanityCheck(inform, OPTIONAL);
|
2014-07-18 18:32:59 +02:00
|
|
|
|
2017-12-15 18:30:59 +01:00
|
|
|
bool drop = false;
|
|
|
|
Dhcpv4Exchange ex(alloc_engine_, inform, selectSubnet(inform, drop));
|
2015-03-10 21:36:08 +01:00
|
|
|
|
2017-12-15 00:53:20 +01:00
|
|
|
// Stop here if selectSubnet decided to drop the packet
|
2017-12-15 18:30:59 +01:00
|
|
|
if (drop) {
|
2017-12-15 00:53:20 +01:00
|
|
|
return (Pkt4Ptr());
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
Pkt4Ptr ack = ex.getResponse();
|
2014-07-18 18:32:59 +02:00
|
|
|
|
2017-09-18 10:46:31 +02:00
|
|
|
ex.setReservedClientClasses();
|
2018-04-06 00:12:52 +02:00
|
|
|
requiredClassify(ex);
|
2017-09-18 10:46:31 +02:00
|
|
|
|
2015-11-21 14:45:08 +01:00
|
|
|
buildCfgOptionList(ex);
|
2015-03-10 21:36:08 +01:00
|
|
|
appendRequestedOptions(ex);
|
|
|
|
appendRequestedVendorOptions(ex);
|
|
|
|
appendBasicOptions(ex);
|
|
|
|
adjustIfaceData(ex);
|
2014-07-15 12:06:40 +02:00
|
|
|
|
2016-08-25 16:44:55 +02:00
|
|
|
// Set fixed fields (siaddr, sname, filename) if defined in
|
|
|
|
// the reservation, class or subnet specific configuration.
|
2016-08-24 14:51:19 +02:00
|
|
|
setFixedFields(ex);
|
|
|
|
|
2014-07-15 12:06:40 +02:00
|
|
|
// There are cases for the DHCPINFORM that the server receives it via
|
|
|
|
// relay but will send the response to the client's unicast address
|
|
|
|
// carried in the ciaddr. In this case, the giaddr and hops field should
|
|
|
|
// be cleared (these fields were copied by the copyDefaultFields function).
|
|
|
|
// Also Relay Agent Options should be removed if present.
|
|
|
|
if (ack->getRemoteAddr() != inform->getGiaddr()) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(packet4_logger, DBG_DHCP4_DETAIL, DHCP4_INFORM_DIRECT_REPLY)
|
2015-05-04 15:07:22 +02:00
|
|
|
.arg(inform->getLabel())
|
2015-05-15 10:30:47 +02:00
|
|
|
.arg(ack->getRemoteAddr())
|
|
|
|
.arg(ack->getIface());
|
2014-07-15 12:06:40 +02:00
|
|
|
ack->setHops(0);
|
2015-02-12 20:15:24 +01:00
|
|
|
ack->setGiaddr(IOAddress::IPV4_ZERO_ADDRESS());
|
2014-07-15 12:06:40 +02:00
|
|
|
ack->delOption(DHO_DHCP_AGENT_OPTIONS);
|
|
|
|
}
|
2013-08-20 15:18:27 +02:00
|
|
|
|
2014-07-15 12:06:40 +02:00
|
|
|
// The DHCPACK must contain server id.
|
2015-03-10 21:36:08 +01:00
|
|
|
appendServerID(ex);
|
2015-03-12 20:14:41 +01:00
|
|
|
|
2015-03-10 21:36:08 +01:00
|
|
|
return (ex.getResponse());
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2012-09-06 12:58:17 +01:00
|
|
|
|
2014-01-13 18:18:46 +01:00
|
|
|
bool
|
2017-12-15 18:30:59 +01:00
|
|
|
Dhcpv4Srv::accept(const Pkt4Ptr& query) const {
|
2014-07-15 12:06:40 +02:00
|
|
|
// Check that the message type is accepted by the server. We rely on the
|
|
|
|
// function called to log a message if needed.
|
|
|
|
if (!acceptMessageType(query)) {
|
|
|
|
return (false);
|
|
|
|
}
|
2014-02-03 18:40:36 +01:00
|
|
|
// Check if the message from directly connected client (if directly
|
|
|
|
// connected) should be dropped or processed.
|
|
|
|
if (!acceptDirectRequest(query)) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DROP_0002)
|
2015-04-20 11:52:37 -04:00
|
|
|
.arg(query->getLabel())
|
2014-02-03 18:40:36 +01:00
|
|
|
.arg(query->getIface());
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the DHCPv4 packet has been sent to us or to someone else.
|
|
|
|
// If it hasn't been sent to us, drop it!
|
|
|
|
if (!acceptServerId(query)) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DROP_0003)
|
2015-04-20 11:52:37 -04:00
|
|
|
.arg(query->getLabel())
|
2014-02-03 18:40:36 +01:00
|
|
|
.arg(query->getIface());
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-12-15 18:30:59 +01:00
|
|
|
Dhcpv4Srv::acceptDirectRequest(const Pkt4Ptr& pkt) const {
|
2015-03-17 11:37:41 +01:00
|
|
|
// Accept all relayed messages.
|
|
|
|
if (pkt->isRelayed()) {
|
|
|
|
return (true);
|
2014-02-03 18:40:36 +01:00
|
|
|
}
|
2016-06-17 02:24:29 +02:00
|
|
|
|
|
|
|
// Accept all DHCPv4-over-DHCPv6 messages.
|
|
|
|
if (pkt->isDhcp4o6()) {
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2014-07-15 12:06:40 +02:00
|
|
|
// The source address must not be zero for the DHCPINFORM message from
|
|
|
|
// the directly connected client because the server will not know where
|
|
|
|
// to respond if the ciaddr was not present.
|
|
|
|
try {
|
|
|
|
if (pkt->getType() == DHCPINFORM) {
|
2015-09-19 17:15:44 +02:00
|
|
|
if (pkt->getRemoteAddr().isV4Zero() &&
|
|
|
|
pkt->getCiaddr().isV4Zero()) {
|
2014-07-15 12:06:40 +02:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
// If we got here, it is probably because the message type hasn't
|
|
|
|
// been set. But, this should not really happen assuming that
|
|
|
|
// we validate the message type prior to calling this function.
|
|
|
|
return (false);
|
|
|
|
}
|
2017-12-15 18:30:59 +01:00
|
|
|
bool drop = false;
|
2018-05-02 22:00:34 +02:00
|
|
|
bool result = (!pkt->getLocalAddr().isV4Bcast() ||
|
|
|
|
selectSubnet(pkt, drop, true));
|
2017-12-15 18:30:59 +01:00
|
|
|
if (drop) {
|
2018-05-02 22:00:34 +02:00
|
|
|
// The packet must be dropped but as sanity_only is true it is dead code.
|
2017-12-15 00:53:20 +01:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
return (result);
|
2014-02-06 18:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Dhcpv4Srv::acceptMessageType(const Pkt4Ptr& query) const {
|
|
|
|
// When receiving a packet without message type option, getType() will
|
|
|
|
// throw.
|
|
|
|
int type;
|
|
|
|
try {
|
|
|
|
type = query->getType();
|
|
|
|
|
|
|
|
} catch (...) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DROP_0004)
|
2015-04-20 11:52:37 -04:00
|
|
|
.arg(query->getLabel())
|
2014-02-06 18:10:07 +01:00
|
|
|
.arg(query->getIface());
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we know that the message type is within a range of defined DHCPv4
|
|
|
|
// messages, we do a detailed check to make sure that the received message
|
|
|
|
// is targeted at server. Note that we could have received some Offer
|
|
|
|
// message broadcasted by the other server to a relay. Even though, the
|
|
|
|
// server would rather unicast its response to a relay, let's be on the
|
|
|
|
// safe side. Also, we want to drop other messages which we don't support.
|
|
|
|
// All these valid messages that we are not going to process are dropped
|
|
|
|
// silently.
|
2018-02-23 09:15:33 -05:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case DHCPDISCOVER:
|
|
|
|
case DHCPREQUEST:
|
|
|
|
case DHCPRELEASE:
|
|
|
|
case DHCPDECLINE:
|
|
|
|
case DHCPINFORM:
|
|
|
|
return (true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_NOTYPE:
|
2018-03-01 10:06:46 -05:00
|
|
|
LOG_INFO(bad_packet4_logger, DHCP4_PACKET_DROP_0009)
|
|
|
|
.arg(query->getLabel());
|
2018-02-23 09:15:33 -05:00
|
|
|
break;
|
2018-03-01 10:06:46 -05:00
|
|
|
|
2018-02-23 09:15:33 -05:00
|
|
|
default:
|
|
|
|
// If we receive a message with a non-existing type, we are logging it.
|
2018-03-01 10:06:46 -05:00
|
|
|
if (type >= DHCP_TYPES_EOF) {
|
2018-02-23 09:15:33 -05:00
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DROP_0005)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(type);
|
|
|
|
} else {
|
|
|
|
// Exists but we don't support it.
|
|
|
|
LOG_DEBUG(bad_packet4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DROP_0006)
|
|
|
|
.arg(query->getLabel())
|
|
|
|
.arg(type);
|
|
|
|
}
|
|
|
|
break;
|
2014-02-06 18:10:07 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 09:15:33 -05:00
|
|
|
return (false);
|
2014-02-03 18:40:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-13 18:18:46 +01:00
|
|
|
bool
|
2014-02-06 18:10:07 +01:00
|
|
|
Dhcpv4Srv::acceptServerId(const Pkt4Ptr& query) const {
|
2014-01-13 18:18:46 +01:00
|
|
|
// This function is meant to be called internally by the server class, so
|
|
|
|
// we rely on the caller to sanity check the pointer and we don't check
|
|
|
|
// it here.
|
|
|
|
|
|
|
|
// Check if server identifier option is present. If it is not present
|
2015-01-22 11:25:36 +01:00
|
|
|
// we accept the message because it is targeted to all servers.
|
2014-01-13 18:18:46 +01:00
|
|
|
// Note that we don't check cases that server identifier is mandatory
|
|
|
|
// but not present. This is meant to be sanity checked in other
|
|
|
|
// functions.
|
2014-02-06 18:10:07 +01:00
|
|
|
OptionPtr option = query->getOption(DHO_DHCP_SERVER_IDENTIFIER);
|
2014-01-13 18:18:46 +01:00
|
|
|
if (!option) {
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
// Server identifier is present. Let's convert it to 4-byte address
|
|
|
|
// and try to match with server identifiers used by the server.
|
2014-01-14 18:47:42 +01:00
|
|
|
OptionCustomPtr option_custom =
|
|
|
|
boost::dynamic_pointer_cast<OptionCustom>(option);
|
2014-01-13 18:35:36 +01:00
|
|
|
// Unable to convert the option to the option type which encapsulates it.
|
|
|
|
// We treat this as non-matching server id.
|
2014-01-14 18:47:42 +01:00
|
|
|
if (!option_custom) {
|
2014-01-13 18:35:36 +01:00
|
|
|
return (false);
|
|
|
|
}
|
2014-01-14 16:27:53 +01:00
|
|
|
// The server identifier option should carry exactly one IPv4 address.
|
2014-01-14 18:47:42 +01:00
|
|
|
// If the option definition for the server identifier doesn't change,
|
|
|
|
// the OptionCustom object should have exactly one IPv4 address and
|
|
|
|
// this check is somewhat redundant. On the other hand, if someone
|
|
|
|
// breaks option it may be better to check that here.
|
|
|
|
if (option_custom->getDataFieldsNum() != 1) {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The server identifier MUST be an IPv4 address. If given address is
|
|
|
|
// v6, it is wrong.
|
|
|
|
IOAddress server_id = option_custom->readAddress();
|
|
|
|
if (!server_id.isV4()) {
|
2014-01-13 18:18:46 +01:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2014-01-14 16:27:53 +01:00
|
|
|
// This function iterates over all interfaces on which the
|
|
|
|
// server is listening to find the one which has a socket bound
|
|
|
|
// to the address carried in the server identifier option.
|
|
|
|
// This has some performance implications. However, given that
|
|
|
|
// typically there will be just a few active interfaces the
|
|
|
|
// performance hit should be acceptable. If it turns out to
|
|
|
|
// be significant, we will have to cache server identifiers
|
|
|
|
// when sockets are opened.
|
2017-10-17 11:23:37 +02:00
|
|
|
if (IfaceMgr::instance().hasOpenSocket(server_id)) {
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are some cases when an administrator explicitly sets server
|
|
|
|
// identifier (option 54) that should be used for a given, subnet,
|
|
|
|
// network etc. It doesn't have to be an address assigned to any of
|
|
|
|
// the server interfaces. Thus, we have to check if the server
|
|
|
|
// identifier received is the one that we explicitly set in the
|
|
|
|
// server configuration. At this point, we don't know which subnet
|
|
|
|
// the client belongs to so we can't match the server id with any
|
|
|
|
// subnet. We simply check if this server identifier is configured
|
|
|
|
// anywhere. This should be good enough to eliminate exchanges
|
|
|
|
// with other servers in the same network.
|
|
|
|
|
|
|
|
/// @todo Currently we only check subnet identifiers configured on the
|
|
|
|
/// subnet level, shared network level and global level. This should
|
|
|
|
/// be sufficient for most of cases. At this point, trying to support
|
|
|
|
/// server identifiers on the class level seems to be an overkill and
|
|
|
|
/// is probably not needed. Same with host reservations. In fact,
|
|
|
|
/// at this point we don't know the reservations for the client
|
|
|
|
/// communicating with the server. We may revise some of these choices
|
|
|
|
/// in the future.
|
|
|
|
|
|
|
|
SrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
|
|
|
|
|
|
|
|
// Check if there is at least one subnet configured with this server
|
|
|
|
// identifier.
|
|
|
|
ConstCfgSubnets4Ptr cfg_subnets = cfg->getCfgSubnets4();
|
|
|
|
if (cfg_subnets->hasSubnetWithServerId(server_id)) {
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This server identifier is not configured for any of the subnets, so
|
|
|
|
// check on the shared network level.
|
|
|
|
CfgSharedNetworks4Ptr cfg_networks = cfg->getCfgSharedNetworks4();
|
2017-10-17 17:21:27 +02:00
|
|
|
if (cfg_networks->hasNetworkWithServerId(server_id)) {
|
2017-10-17 11:23:37 +02:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, it is possible that the server identifier is specified
|
|
|
|
// on the global level.
|
|
|
|
ConstCfgOptionPtr cfg_global_options = cfg->getCfgOption();
|
|
|
|
OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
|
|
|
|
(cfg_global_options->get(DHCP4_OPTION_SPACE, DHO_DHCP_SERVER_IDENTIFIER).option_);
|
|
|
|
|
|
|
|
return (opt_server_id && (opt_server_id->readAddress() == server_id));
|
2014-01-13 18:18:46 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
2015-03-12 20:14:41 +01:00
|
|
|
Dhcpv4Srv::sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid) {
|
|
|
|
OptionPtr server_id = query->getOption(DHO_DHCP_SERVER_IDENTIFIER);
|
2012-12-31 16:49:37 +01:00
|
|
|
switch (serverid) {
|
|
|
|
case FORBIDDEN:
|
|
|
|
if (server_id) {
|
2019-09-04 11:38:07 -04:00
|
|
|
isc_throw(RFCViolation, "Server-id option was not expected, but"
|
|
|
|
<< " received in message "
|
2015-05-04 18:29:54 +02:00
|
|
|
<< query->getName());
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MANDATORY:
|
|
|
|
if (!server_id) {
|
2019-09-04 11:38:07 -04:00
|
|
|
isc_throw(RFCViolation, "Server-id option was expected, but not"
|
2012-12-31 16:49:37 +01:00
|
|
|
" received in message "
|
2015-05-04 18:29:54 +02:00
|
|
|
<< query->getName());
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTIONAL:
|
|
|
|
// do nothing here
|
|
|
|
;
|
|
|
|
}
|
2013-07-16 13:58:01 +02:00
|
|
|
|
|
|
|
// If there is HWAddress set and it is non-empty, then we're good
|
2015-03-12 20:14:41 +01:00
|
|
|
if (query->getHWAddr() && !query->getHWAddr()->hwaddr_.empty()) {
|
2013-07-16 13:58:01 +02:00
|
|
|
return;
|
2013-07-19 15:54:02 +02:00
|
|
|
}
|
2013-07-16 13:58:01 +02:00
|
|
|
|
|
|
|
// There has to be something to uniquely identify the client:
|
|
|
|
// either non-zero MAC address or client-id option present (or both)
|
2015-03-12 20:14:41 +01:00
|
|
|
OptionPtr client_id = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
2013-07-16 13:58:01 +02:00
|
|
|
|
|
|
|
// If there's no client-id (or a useless one is provided, i.e. 0 length)
|
|
|
|
if (!client_id || client_id->len() == client_id->getHeaderLen()) {
|
2019-09-04 11:38:07 -04:00
|
|
|
isc_throw(RFCViolation, "Missing or useless client-id and no HW address"
|
2013-07-16 13:58:01 +02:00
|
|
|
" provided in message "
|
2015-05-04 18:29:54 +02:00
|
|
|
<< query->getName());
|
2013-07-16 13:58:01 +02:00
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
2013-01-31 12:16:22 +00:00
|
|
|
|
2016-09-02 15:38:50 +02:00
|
|
|
void Dhcpv4Srv::classifyByVendor(const Pkt4Ptr& pkt) {
|
2015-11-20 14:41:18 +01:00
|
|
|
// Built-in vendor class processing
|
2013-10-18 11:10:54 +02:00
|
|
|
boost::shared_ptr<OptionString> vendor_class =
|
|
|
|
boost::dynamic_pointer_cast<OptionString>(pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER));
|
|
|
|
|
|
|
|
if (!vendor_class) {
|
2015-11-20 23:18:21 +01:00
|
|
|
return;
|
2013-10-18 11:10:54 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 16:44:55 +02:00
|
|
|
pkt->addClass(VENDOR_CLASS_PREFIX + vendor_class->getValue());
|
2015-11-20 23:18:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dhcpv4Srv::classifyPacket(const Pkt4Ptr& pkt) {
|
2018-04-07 15:15:41 +02:00
|
|
|
// All packets belongs to ALL.
|
|
|
|
pkt->addClass("ALL");
|
|
|
|
|
2018-05-18 23:59:32 +02:00
|
|
|
// First: built-in vendor class processing.
|
2016-09-02 15:38:50 +02:00
|
|
|
classifyByVendor(pkt);
|
2015-11-20 14:41:18 +01:00
|
|
|
|
2018-06-07 13:59:55 +02:00
|
|
|
// Run match expressions on classes not depending on KNOWN/UNKNOWN.
|
2018-05-18 23:59:32 +02:00
|
|
|
evaluateClasses(pkt, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dhcpv4Srv::evaluateClasses(const Pkt4Ptr& pkt, bool depend_on_known) {
|
2015-11-20 14:41:18 +01:00
|
|
|
// Note getClientClassDictionary() cannot be null
|
2017-11-24 18:39:44 +01:00
|
|
|
const ClientClassDictionaryPtr& dict =
|
|
|
|
CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
|
|
|
|
const ClientClassDefListPtr& defs_ptr = dict->getClasses();
|
|
|
|
for (ClientClassDefList::const_iterator it = defs_ptr->cbegin();
|
2017-11-24 01:07:17 +01:00
|
|
|
it != defs_ptr->cend(); ++it) {
|
2015-11-20 14:41:18 +01:00
|
|
|
// Note second cannot be null
|
2017-11-24 18:39:44 +01:00
|
|
|
const ExpressionPtr& expr_ptr = (*it)->getMatchExpr();
|
2015-11-20 14:41:18 +01:00
|
|
|
// Nothing to do without an expression to evaluate
|
|
|
|
if (!expr_ptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-06 00:12:52 +02:00
|
|
|
// Not the right time if only when required
|
|
|
|
if ((*it)->getRequired()) {
|
2017-11-27 02:59:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-18 23:59:32 +02:00
|
|
|
// Not the right pass.
|
|
|
|
if ((*it)->getDependOnKnown() != depend_on_known) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-20 14:41:18 +01:00
|
|
|
// Evaluate the expression which can return false (no match),
|
|
|
|
// true (match) or raise an exception (error)
|
|
|
|
try {
|
2017-03-23 00:01:05 -07:00
|
|
|
bool status = evaluateBool(*expr_ptr, *pkt);
|
2015-11-20 14:41:18 +01:00
|
|
|
if (status) {
|
|
|
|
LOG_INFO(options4_logger, EVAL_RESULT)
|
2017-11-24 18:39:44 +01:00
|
|
|
.arg((*it)->getName())
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg(status);
|
|
|
|
// Matching: add the class
|
2017-11-24 18:39:44 +01:00
|
|
|
pkt->addClass((*it)->getName());
|
2015-11-20 14:41:18 +01:00
|
|
|
} else {
|
|
|
|
LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL, EVAL_RESULT)
|
2017-11-24 18:39:44 +01:00
|
|
|
.arg((*it)->getName())
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg(status);
|
|
|
|
}
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
LOG_ERROR(options4_logger, EVAL_RESULT)
|
2017-11-24 18:39:44 +01:00
|
|
|
.arg((*it)->getName())
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg(ex.what());
|
|
|
|
} catch (...) {
|
|
|
|
LOG_ERROR(options4_logger, EVAL_RESULT)
|
2017-11-24 18:39:44 +01:00
|
|
|
.arg((*it)->getName())
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg("get exception?");
|
|
|
|
}
|
2013-10-18 11:10:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 00:12:52 +02:00
|
|
|
void Dhcpv4Srv::requiredClassify(Dhcpv4Exchange& ex) {
|
|
|
|
// First collect required classes
|
2017-11-27 02:59:59 +01:00
|
|
|
Pkt4Ptr query = ex.getQuery();
|
|
|
|
ClientClasses classes = query->getClasses(true);
|
|
|
|
Subnet4Ptr subnet = ex.getContext()->subnet_;
|
|
|
|
|
|
|
|
if (subnet) {
|
|
|
|
// Begin by the shared-network
|
|
|
|
SharedNetwork4Ptr network;
|
|
|
|
subnet->getSharedNetwork(network);
|
|
|
|
if (network) {
|
2018-04-06 00:12:52 +02:00
|
|
|
const ClientClasses& to_add = network->getRequiredClasses();
|
2017-11-27 02:59:59 +01:00
|
|
|
for (ClientClasses::const_iterator cclass = to_add.cbegin();
|
|
|
|
cclass != to_add.cend(); ++cclass) {
|
|
|
|
classes.insert(*cclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Followed by the subnet
|
2018-04-06 00:12:52 +02:00
|
|
|
const ClientClasses& to_add = subnet->getRequiredClasses();
|
2017-11-27 02:59:59 +01:00
|
|
|
for(ClientClasses::const_iterator cclass = to_add.cbegin();
|
|
|
|
cclass != to_add.cend(); ++cclass) {
|
|
|
|
classes.insert(*cclass);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finish by the pool
|
|
|
|
Pkt4Ptr resp = ex.getResponse();
|
|
|
|
IOAddress addr = IOAddress::IPV4_ZERO_ADDRESS();
|
|
|
|
if (resp) {
|
|
|
|
addr = resp->getYiaddr();
|
|
|
|
}
|
|
|
|
if (!addr.isV4Zero()) {
|
|
|
|
PoolPtr pool = subnet->getPool(Lease::TYPE_V4, addr, false);
|
|
|
|
if (pool) {
|
2018-04-06 00:12:52 +02:00
|
|
|
const ClientClasses& to_add = pool->getRequiredClasses();
|
2017-11-27 02:59:59 +01:00
|
|
|
for (ClientClasses::const_iterator cclass = to_add.cbegin();
|
|
|
|
cclass != to_add.cend(); ++cclass) {
|
|
|
|
classes.insert(*cclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// host reservation???
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run match expressions
|
|
|
|
// Note getClientClassDictionary() cannot be null
|
|
|
|
const ClientClassDictionaryPtr& dict =
|
|
|
|
CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
|
|
|
|
for (ClientClasses::const_iterator cclass = classes.cbegin();
|
|
|
|
cclass != classes.cend(); ++cclass) {
|
|
|
|
const ClientClassDefPtr class_def = dict->findClass(*cclass);
|
|
|
|
if (!class_def) {
|
2018-04-06 00:12:52 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_UNDEFINED)
|
2017-11-28 00:23:45 +01:00
|
|
|
.arg(*cclass);
|
2017-11-27 02:59:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const ExpressionPtr& expr_ptr = class_def->getMatchExpr();
|
|
|
|
// Nothing to do without an expression to evaluate
|
|
|
|
if (!expr_ptr) {
|
2017-11-28 00:23:45 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_UNTESTABLE)
|
|
|
|
.arg(*cclass);
|
2017-11-27 02:59:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Evaluate the expression which can return false (no match),
|
|
|
|
// true (match) or raise an exception (error)
|
|
|
|
try {
|
|
|
|
bool status = evaluateBool(*expr_ptr, *query);
|
|
|
|
if (status) {
|
|
|
|
LOG_INFO(options4_logger, EVAL_RESULT)
|
|
|
|
.arg(*cclass)
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg(status);
|
|
|
|
// Matching: add the class
|
2017-11-27 02:59:59 +01:00
|
|
|
query->addClass(*cclass);
|
2015-11-20 14:41:18 +01:00
|
|
|
} else {
|
|
|
|
LOG_DEBUG(options4_logger, DBG_DHCP4_DETAIL, EVAL_RESULT)
|
2017-11-27 02:59:59 +01:00
|
|
|
.arg(*cclass)
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg(status);
|
|
|
|
}
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
LOG_ERROR(options4_logger, EVAL_RESULT)
|
2017-11-27 02:59:59 +01:00
|
|
|
.arg(*cclass)
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg(ex.what());
|
|
|
|
} catch (...) {
|
|
|
|
LOG_ERROR(options4_logger, EVAL_RESULT)
|
2017-11-27 02:59:59 +01:00
|
|
|
.arg(*cclass)
|
2015-11-20 14:41:18 +01:00
|
|
|
.arg("get exception?");
|
|
|
|
}
|
2013-10-18 11:10:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 21:33:52 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::deferredUnpack(Pkt4Ptr& query)
|
|
|
|
{
|
|
|
|
// Iterate on the list of deferred option codes
|
2017-09-25 15:27:54 +02:00
|
|
|
BOOST_FOREACH(const uint16_t& code, query->getDeferredOptions()) {
|
2017-09-20 21:33:52 +02:00
|
|
|
OptionDefinitionPtr def;
|
|
|
|
// Iterate on client classes
|
|
|
|
const ClientClasses& classes = query->getClasses();
|
2017-11-24 01:07:17 +01:00
|
|
|
for (ClientClasses::const_iterator cclass = classes.cbegin();
|
|
|
|
cclass != classes.cend(); ++cclass) {
|
2017-09-20 21:33:52 +02:00
|
|
|
// Get the client class definition for this class
|
|
|
|
const ClientClassDefPtr& ccdef =
|
|
|
|
CfgMgr::instance().getCurrentCfg()->
|
|
|
|
getClientClassDictionary()->findClass(*cclass);
|
|
|
|
// If not found skip it
|
|
|
|
if (!ccdef) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If there is no option definition skip it
|
|
|
|
if (!ccdef->getCfgOptionDef()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
def = ccdef->getCfgOptionDef()->get(DHCP4_OPTION_SPACE, code);
|
|
|
|
// Stop at the first client class with a defition
|
|
|
|
if (def) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If not found try the global definition
|
|
|
|
if (!def) {
|
|
|
|
def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, code);
|
|
|
|
}
|
|
|
|
if (!def) {
|
|
|
|
def = LibDHCP::getRuntimeOptionDef(DHCP4_OPTION_SPACE, code);
|
|
|
|
}
|
2017-09-20 22:48:58 +02:00
|
|
|
// Finish by last resort definition
|
|
|
|
if (!def) {
|
|
|
|
def = LibDHCP::getLastResortOptionDef(DHCP4_OPTION_SPACE, code);
|
2017-09-20 21:33:52 +02:00
|
|
|
}
|
|
|
|
// If not defined go to the next option
|
|
|
|
if (!def) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Get the existing option for its content and remove all
|
2017-09-21 23:50:48 +02:00
|
|
|
OptionPtr opt = query->getOption(code);
|
|
|
|
if (!opt) {
|
2017-09-25 15:27:54 +02:00
|
|
|
// should not happen but do not crash anyway
|
2017-09-21 23:50:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const OptionBuffer buf = opt->getData();
|
2017-09-20 21:33:52 +02:00
|
|
|
while (query->delOption(code)) {
|
2017-09-25 15:27:54 +02:00
|
|
|
// continue
|
2017-09-20 21:33:52 +02:00
|
|
|
}
|
|
|
|
// Unpack the option and add it
|
2017-09-21 23:50:48 +02:00
|
|
|
opt = def->optionFactory(Option::V4, code, buf.cbegin(), buf.cend());
|
2017-09-20 21:33:52 +02:00
|
|
|
query->addOption(opt);
|
|
|
|
}
|
2018-02-27 14:46:40 -05:00
|
|
|
}
|
2017-09-20 21:33:52 +02:00
|
|
|
|
2014-02-13 16:08:43 -05:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::startD2() {
|
|
|
|
D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
|
|
|
|
if (d2_mgr.ddnsEnabled()) {
|
|
|
|
// Updates are enabled, so lets start the sender, passing in
|
|
|
|
// our error handler.
|
|
|
|
// This may throw so wherever this is called needs to ready.
|
|
|
|
d2_mgr.startSender(boost::bind(&Dhcpv4Srv::d2ClientErrorHandler,
|
|
|
|
this, _1, _2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 15:50:47 -04:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::stopD2() {
|
|
|
|
D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
|
|
|
|
if (d2_mgr.ddnsEnabled()) {
|
|
|
|
// Updates are enabled, so lets stop the sender
|
|
|
|
d2_mgr.stopSender();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:08:43 -05:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::d2ClientErrorHandler(const
|
|
|
|
dhcp_ddns::NameChangeSender::Result result,
|
|
|
|
dhcp_ddns::NameChangeRequestPtr& ncr) {
|
2015-07-09 10:38:07 +02:00
|
|
|
LOG_ERROR(ddns4_logger, DHCP4_DDNS_REQUEST_SEND_FAILED).
|
2014-02-13 16:08:43 -05:00
|
|
|
arg(result).arg((ncr ? ncr->toText() : " NULL "));
|
2015-01-22 11:25:36 +01:00
|
|
|
// We cannot communicate with kea-dhcp-ddns, suspend further updates.
|
|
|
|
/// @todo We may wish to revisit this, but for now we will simply turn
|
2014-02-13 16:08:43 -05:00
|
|
|
/// them off.
|
|
|
|
CfgMgr::instance().getD2ClientMgr().suspendUpdates();
|
|
|
|
}
|
|
|
|
|
2015-05-17 16:32:28 +02:00
|
|
|
// Refer to config_report so it will be embedded in the binary
|
|
|
|
const char* const* dhcp4_config_report = isc::detail::config_report;
|
|
|
|
|
2014-08-11 22:37:29 +02:00
|
|
|
std::string
|
2015-05-22 08:29:03 +02:00
|
|
|
Dhcpv4Srv::getVersion(bool extended) {
|
2014-08-11 22:37:29 +02:00
|
|
|
std::stringstream tmp;
|
|
|
|
|
|
|
|
tmp << VERSION;
|
|
|
|
if (extended) {
|
2015-05-17 13:38:10 +02:00
|
|
|
tmp << endl << EXTENDED_VERSION << endl;
|
2015-06-09 09:43:49 +02:00
|
|
|
tmp << "linked with:" << endl;
|
|
|
|
tmp << Logger::getVersion() << endl;
|
|
|
|
tmp << CryptoLink::getVersion() << endl;
|
2015-06-19 14:39:02 +02:00
|
|
|
tmp << "database:" << endl;
|
2015-05-17 13:38:10 +02:00
|
|
|
#ifdef HAVE_MYSQL
|
2015-06-19 14:39:02 +02:00
|
|
|
tmp << MySqlLeaseMgr::getDBVersion() << endl;
|
2015-05-17 13:38:10 +02:00
|
|
|
#endif
|
2015-06-19 14:39:02 +02:00
|
|
|
#ifdef HAVE_PGSQL
|
|
|
|
tmp << PgSqlLeaseMgr::getDBVersion() << endl;
|
2016-05-19 17:09:32 +03:00
|
|
|
#endif
|
2016-06-23 14:24:39 +02:00
|
|
|
#ifdef HAVE_CQL
|
|
|
|
tmp << CqlLeaseMgr::getDBVersion() << endl;
|
2015-05-17 13:38:10 +02:00
|
|
|
#endif
|
2015-06-19 14:39:02 +02:00
|
|
|
tmp << Memfile_LeaseMgr::getDBVersion();
|
|
|
|
|
2015-05-17 13:38:10 +02:00
|
|
|
// @todo: more details about database runtime
|
2014-08-11 22:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (tmp.str());
|
|
|
|
}
|
|
|
|
|
2015-05-15 15:54:08 +02:00
|
|
|
void Dhcpv4Srv::processStatsReceived(const Pkt4Ptr& query) {
|
|
|
|
// Note that we're not bumping pkt4-received statistic as it was
|
|
|
|
// increased early in the packet reception code.
|
|
|
|
|
|
|
|
string stat_name = "pkt4-unknown-received";
|
|
|
|
try {
|
|
|
|
switch (query->getType()) {
|
|
|
|
case DHCPDISCOVER:
|
|
|
|
stat_name = "pkt4-discover-received";
|
|
|
|
break;
|
|
|
|
case DHCPOFFER:
|
2015-06-09 16:50:56 +02:00
|
|
|
// Should not happen, but let's keep a counter for it
|
2015-05-15 15:54:08 +02:00
|
|
|
stat_name = "pkt4-offer-received";
|
|
|
|
break;
|
|
|
|
case DHCPREQUEST:
|
|
|
|
stat_name = "pkt4-request-received";
|
|
|
|
break;
|
|
|
|
case DHCPACK:
|
2015-06-09 16:50:56 +02:00
|
|
|
// Should not happen, but let's keep a counter for it
|
2015-05-15 15:54:08 +02:00
|
|
|
stat_name = "pkt4-ack-received";
|
|
|
|
break;
|
|
|
|
case DHCPNAK:
|
2015-06-09 16:50:56 +02:00
|
|
|
// Should not happen, but let's keep a counter for it
|
2015-05-15 15:54:08 +02:00
|
|
|
stat_name = "pkt4-nak-received";
|
|
|
|
break;
|
|
|
|
case DHCPRELEASE:
|
|
|
|
stat_name = "pkt4-release-received";
|
|
|
|
break;
|
|
|
|
case DHCPDECLINE:
|
|
|
|
stat_name = "pkt4-decline-received";
|
|
|
|
break;
|
|
|
|
case DHCPINFORM:
|
|
|
|
stat_name = "pkt4-inform-received";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
; // do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// If the incoming packet doesn't have option 53 (message type)
|
|
|
|
// or a hook set pkt4_receive_skip, then Pkt4::getType() may
|
|
|
|
// throw an exception. That's ok, we'll then use the default
|
|
|
|
// name of pkt4-unknown-received.
|
|
|
|
}
|
|
|
|
|
2015-06-09 16:48:51 +02:00
|
|
|
isc::stats::StatsMgr::instance().addValue(stat_name,
|
2015-06-16 14:33:49 +02:00
|
|
|
static_cast<int64_t>(1));
|
2015-05-15 15:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dhcpv4Srv::processStatsSent(const Pkt4Ptr& response) {
|
|
|
|
// Increase generic counter for sent packets.
|
2015-06-09 17:05:02 +02:00
|
|
|
isc::stats::StatsMgr::instance().addValue("pkt4-sent",
|
2015-06-16 14:33:49 +02:00
|
|
|
static_cast<int64_t>(1));
|
2015-05-15 15:54:08 +02:00
|
|
|
|
|
|
|
// Increase packet type specific counter for packets sent.
|
|
|
|
string stat_name;
|
|
|
|
switch (response->getType()) {
|
|
|
|
case DHCPOFFER:
|
|
|
|
stat_name = "pkt4-offer-sent";
|
|
|
|
break;
|
|
|
|
case DHCPACK:
|
|
|
|
stat_name = "pkt4-ack-sent";
|
|
|
|
break;
|
|
|
|
case DHCPNAK:
|
|
|
|
stat_name = "pkt4-nak-sent";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// That should never happen
|
2015-06-09 16:48:51 +02:00
|
|
|
return;
|
2015-05-15 15:54:08 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 17:05:02 +02:00
|
|
|
isc::stats::StatsMgr::instance().addValue(stat_name,
|
2015-06-16 14:33:49 +02:00
|
|
|
static_cast<int64_t>(1));
|
2015-05-15 15:54:08 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:08:35 +02:00
|
|
|
int Dhcpv4Srv::getHookIndexBuffer4Receive() {
|
|
|
|
return (Hooks.hook_index_buffer4_receive_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Dhcpv4Srv::getHookIndexPkt4Receive() {
|
|
|
|
return (Hooks.hook_index_pkt4_receive_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Dhcpv4Srv::getHookIndexSubnet4Select() {
|
|
|
|
return (Hooks.hook_index_subnet4_select_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Dhcpv4Srv::getHookIndexLease4Release() {
|
|
|
|
return (Hooks.hook_index_lease4_release_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Dhcpv4Srv::getHookIndexPkt4Send() {
|
|
|
|
return (Hooks.hook_index_pkt4_send_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Dhcpv4Srv::getHookIndexBuffer4Send() {
|
|
|
|
return (Hooks.hook_index_buffer4_send_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Dhcpv4Srv::getHookIndexLease4Decline() {
|
|
|
|
return (Hooks.hook_index_lease4_decline_);
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:42:55 +02:00
|
|
|
void Dhcpv4Srv::discardPackets() {
|
2018-05-15 15:56:18 -04:00
|
|
|
// Clear any packets held by the callhout handle store and
|
|
|
|
// all parked packets
|
|
|
|
isc::dhcp::Pkt4Ptr pkt4ptr_empty;
|
|
|
|
isc::dhcp::getCalloutHandle(pkt4ptr_empty);
|
|
|
|
HooksManager::clearParkingLots();
|
|
|
|
}
|
|
|
|
|
2013-01-31 12:16:22 +00:00
|
|
|
} // namespace dhcp
|
|
|
|
} // namespace isc
|