2014-01-13 18:18:46 +01:00
|
|
|
// Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
|
2011-11-27 19:35:47 +08:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <asiolink/io_address.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>
|
2011-12-20 19:59:14 +01:00
|
|
|
#include <dhcp/option4_addrlst.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>
|
2013-10-22 12:27:07 +02:00
|
|
|
#include <dhcp/docsis3_option_defs.h>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <dhcp4/dhcp4_log.h>
|
|
|
|
#include <dhcp4/dhcp4_srv.h>
|
2013-07-24 13:41:27 +01:00
|
|
|
#include <dhcpsrv/addr_utilities.h>
|
|
|
|
#include <dhcpsrv/callout_handle_store.h>
|
2012-12-31 16:49:37 +01:00
|
|
|
#include <dhcpsrv/cfgmgr.h>
|
|
|
|
#include <dhcpsrv/lease_mgr.h>
|
|
|
|
#include <dhcpsrv/lease_mgr_factory.h>
|
|
|
|
#include <dhcpsrv/subnet.h>
|
|
|
|
#include <dhcpsrv/utils.h>
|
2013-07-24 13:41:27 +01:00
|
|
|
#include <dhcpsrv/utils.h>
|
2013-07-15 18:41:19 +02:00
|
|
|
#include <hooks/callout_handle.h>
|
2013-07-24 13:41:27 +01:00
|
|
|
#include <hooks/hooks_manager.h>
|
2013-11-20 14:04:40 +01:00
|
|
|
#include <util/strutil.h>
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2013-10-04 21:17:07 +02:00
|
|
|
#include <boost/bind.hpp>
|
2013-10-22 12:27:07 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2013-01-14 17:57:26 +01:00
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
2011-11-27 19:35:47 +08:00
|
|
|
using namespace isc;
|
|
|
|
using namespace isc::asiolink;
|
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;
|
|
|
|
using namespace std;
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2013-07-15 18:41:19 +02:00
|
|
|
/// Structure that holds registered hook indexes
|
2013-08-19 15:08:08 +02:00
|
|
|
struct Dhcp4Hooks {
|
|
|
|
int hook_index_buffer4_receive_;///< index for "buffer4_receive" hook point
|
2013-07-15 18:41:19 +02:00
|
|
|
int hook_index_pkt4_receive_; ///< index for "pkt4_receive" hook point
|
|
|
|
int hook_index_subnet4_select_; ///< index for "subnet4_select" hook point
|
2013-08-19 15:08:08 +02:00
|
|
|
int hook_index_lease4_release_; ///< index for "lease4_release" hook point
|
2013-07-15 18:41:19 +02:00
|
|
|
int hook_index_pkt4_send_; ///< index for "pkt4_send" hook point
|
2013-08-19 15:08:08 +02:00
|
|
|
int hook_index_buffer4_send_; ///< index for "buffer4_send" 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() {
|
|
|
|
hook_index_buffer4_receive_= HooksManager::registerHook("buffer4_receive");
|
2013-07-15 18:41:19 +02:00
|
|
|
hook_index_pkt4_receive_ = HooksManager::registerHook("pkt4_receive");
|
|
|
|
hook_index_subnet4_select_ = HooksManager::registerHook("subnet4_select");
|
|
|
|
hook_index_pkt4_send_ = HooksManager::registerHook("pkt4_send");
|
2013-08-19 15:08:08 +02:00
|
|
|
hook_index_lease4_release_ = HooksManager::registerHook("lease4_release");
|
|
|
|
hook_index_buffer4_send_ = HooksManager::registerHook("buffer4_send");
|
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
|
|
|
|
2013-01-31 12:16:22 +00:00
|
|
|
namespace isc {
|
|
|
|
namespace dhcp {
|
|
|
|
|
2014-02-18 13:49:50 +01:00
|
|
|
const std::string Dhcpv4Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");
|
|
|
|
|
2014-06-04 18:57:51 +02:00
|
|
|
Dhcpv4Srv::Dhcpv4Srv(uint16_t port, const bool use_bcast,
|
2013-07-11 13:31:01 +02:00
|
|
|
const bool direct_response_desired)
|
2014-06-04 18:57:51 +02:00
|
|
|
: shutdown_(true), alloc_engine_(), port_(port),
|
|
|
|
use_bcast_(use_bcast), hook_index_pkt4_receive_(-1),
|
|
|
|
hook_index_subnet4_select_(-1), hook_index_pkt4_send_(-1) {
|
2013-07-22 18:46:54 +02:00
|
|
|
|
2012-09-06 11:18:51 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET).arg(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.
|
2012-12-31 16:49:37 +01:00
|
|
|
if (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
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// Instantiate allocation engine
|
2013-09-16 14:35:46 +02:00
|
|
|
alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100,
|
|
|
|
false /* false = IPv4 */));
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-07-15 18:41:19 +02:00
|
|
|
// Register hook points
|
|
|
|
hook_index_pkt4_receive_ = Hooks.hook_index_pkt4_receive_;
|
|
|
|
hook_index_subnet4_select_ = Hooks.hook_index_subnet4_select_;
|
|
|
|
hook_index_pkt4_send_ = Hooks.hook_index_pkt4_send_;
|
|
|
|
|
|
|
|
/// @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
|
|
|
|
2011-12-12 18:04:12 +01:00
|
|
|
shutdown_ = false;
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Dhcpv4Srv::~Dhcpv4Srv() {
|
2011-12-12 19:24:36 +01:00
|
|
|
IfaceMgr::instance().closeSockets();
|
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;
|
|
|
|
}
|
|
|
|
|
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_) {
|
2012-05-30 18:36:58 +02:00
|
|
|
/// @todo: calculate actual timeout once we have lease database
|
2013-06-12 11:34:33 +02:00
|
|
|
//cppcheck-suppress variableScope This is temporary anyway
|
|
|
|
const int timeout = 1000;
|
2012-05-30 18:36:58 +02:00
|
|
|
|
2012-03-08 14:52:46 +01:00
|
|
|
// client's message and server's response
|
2012-09-26 12:01:03 +02:00
|
|
|
Pkt4Ptr query;
|
2012-03-08 14:52:46 +01:00
|
|
|
Pkt4Ptr rsp;
|
2011-12-07 15:00:37 +01:00
|
|
|
|
2012-09-26 12:01:03 +02:00
|
|
|
try {
|
2013-07-15 18:41:19 +02:00
|
|
|
query = receivePacket(timeout);
|
2012-09-26 12:01:03 +02:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_PACKET_RECEIVE_FAIL).arg(e.what());
|
|
|
|
}
|
|
|
|
|
2014-06-03 12:08:23 +02: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 recivePacket()). 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.
|
|
|
|
handleSignal();
|
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Timeout may be reached or signal received, which breaks select()
|
|
|
|
// with no reception ocurred
|
|
|
|
if (!query) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-04 21:17:07 +02:00
|
|
|
// In order to parse the DHCP options, the server needs to use some
|
|
|
|
// configuration information such as: existing option spaces, option
|
|
|
|
// definitions etc. This is the kind of information which is not
|
|
|
|
// available in the libdhcp, so we need to supply our own implementation
|
|
|
|
// of the option parsing function here, which would rely on the
|
|
|
|
// configuration data.
|
|
|
|
query->setCallback(boost::bind(&Dhcpv4Srv::unpackOptions, this,
|
|
|
|
_1, _2, _3));
|
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
bool skip_unpack = false;
|
|
|
|
|
|
|
|
// The packet has just been received so contains the uninterpreted wire
|
2013-08-20 14:02:27 +02:00
|
|
|
// data; execute callouts registered for buffer4_receive.
|
2013-10-10 18:02:06 +02:00
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_buffer4_receive_)) {
|
2013-08-19 15:08:08 +02:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
|
|
|
|
|
|
|
// Delete previously set arguments
|
|
|
|
callout_handle->deleteAllArguments();
|
|
|
|
|
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("query4", query);
|
|
|
|
|
|
|
|
// Call callouts
|
2013-08-23 11:26:36 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_buffer4_receive_,
|
|
|
|
*callout_handle);
|
2013-08-19 15:08:08 +02: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->getSkip()) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_HOOKS, DHCP4_HOOK_BUFFER_RCVD_SKIP);
|
|
|
|
skip_unpack = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
callout_handle->getArgument("query4", query);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack the packet information unless the buffer4_receive callouts
|
|
|
|
// indicated they did it
|
|
|
|
if (!skip_unpack) {
|
2011-12-12 19:24:36 +01:00
|
|
|
try {
|
|
|
|
query->unpack();
|
|
|
|
} catch (const std::exception& e) {
|
2012-09-06 11:18:51 +01:00
|
|
|
// Failed to parse the packet.
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_PACKET_PARSE_FAIL).arg(e.what());
|
2011-11-27 19:35:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2014-02-27 18:30:25 +01: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.
|
2014-02-27 11:12:47 +01:00
|
|
|
classifyPacket(query);
|
|
|
|
|
2014-02-03 18:40:36 +01: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)) {
|
2013-08-20 14:52:24 +02:00
|
|
|
continue;
|
2013-08-20 14:02:27 +02:00
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2014-02-06 18:10:07 +01:00
|
|
|
// We have sanity checked (in accept() that the Message Type option
|
2014-02-03 18:40:36 +01:00
|
|
|
// exists, so we can safely get it here.
|
|
|
|
int type = query->getType();
|
2013-08-19 15:08:08 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_RECEIVED)
|
2013-08-20 14:52:24 +02:00
|
|
|
.arg(serverReceivedPacketName(type))
|
2013-08-20 14:02:27 +02:00
|
|
|
.arg(type)
|
2013-08-19 15:08:08 +02:00
|
|
|
.arg(query->getIface());
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_QUERY_DATA)
|
2013-08-20 14:02:27 +02:00
|
|
|
.arg(type)
|
2013-08-19 15:08:08 +02:00
|
|
|
.arg(query->toText());
|
|
|
|
|
2013-08-23 11:26:36 +02:00
|
|
|
// Let's execute all callouts registered for pkt4_receive
|
2013-08-19 15:08:08 +02:00
|
|
|
if (HooksManager::calloutsPresent(hook_index_pkt4_receive_)) {
|
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
|
|
|
|
|
|
|
// Delete previously set arguments
|
|
|
|
callout_handle->deleteAllArguments();
|
|
|
|
|
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("query4", query);
|
|
|
|
|
|
|
|
// Call callouts
|
|
|
|
HooksManager::callCallouts(hook_index_pkt4_receive_,
|
|
|
|
*callout_handle);
|
|
|
|
|
|
|
|
// Callouts decided to skip the next processing step. The next
|
|
|
|
// processing step would to process the packet, so skip at this
|
|
|
|
// stage means drop.
|
|
|
|
if (callout_handle->getSkip()) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_HOOKS, DHCP4_HOOK_PACKET_RCVD_SKIP);
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
callout_handle->getArgument("query4", query);
|
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02: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.
|
|
|
|
rsp = processRequest(query);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPRELEASE:
|
|
|
|
processRelease(query);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPDECLINE:
|
|
|
|
processDecline(query);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPINFORM:
|
2014-07-15 12:06:40 +02:00
|
|
|
rsp = processInform(query);
|
2013-08-19 15:08:08 +02:00
|
|
|
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-07-15 18:41:19 +02:00
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
} catch (const isc::Exception& e) {
|
|
|
|
|
2014-08-06 11:24:16 +02:00
|
|
|
// Catch-all exception (at least for ones based on the isc Exception
|
|
|
|
// class, which covers more or less all that are explicitly raised
|
|
|
|
// in the Kea code). 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.)
|
2013-08-19 15:08:08 +02:00
|
|
|
if (dhcp4_logger.isDebugEnabled(DBG_DHCP4_BASIC)) {
|
|
|
|
std::string source = "unknown";
|
|
|
|
HWAddrPtr hwptr = query->getHWAddr();
|
|
|
|
if (hwptr) {
|
|
|
|
source = hwptr->toText();
|
2013-02-11 13:42:18 +00:00
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC,
|
|
|
|
DHCP4_PACKET_PROCESS_FAIL)
|
|
|
|
.arg(source).arg(e.what());
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
2011-11-27 19:35:47 +08:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
if (!rsp) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-21 15:18:58 +02:00
|
|
|
|
2014-01-17 20:51:34 +01:00
|
|
|
// Let's do class specific processing. This is done before
|
|
|
|
// pkt4_send.
|
|
|
|
//
|
|
|
|
/// @todo: decide whether we want to add a new hook point for
|
|
|
|
/// doing class specific processing.
|
2013-10-18 11:10:54 +02:00
|
|
|
if (!classSpecificProcessing(query, rsp)) {
|
|
|
|
/// @todo add more verbosity here
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_PROCESSING_FAILED);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Specifies if server should do the packing
|
|
|
|
bool skip_pack = false;
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-23 11:26:36 +02:00
|
|
|
// Execute all callouts registered for pkt4_send
|
2013-08-19 15:08:08 +02:00
|
|
|
if (HooksManager::calloutsPresent(hook_index_pkt4_send_)) {
|
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Delete all previous arguments
|
|
|
|
callout_handle->deleteAllArguments();
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Clear skip flag if it was set in previous callouts
|
|
|
|
callout_handle->setSkip(false);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Set our response
|
|
|
|
callout_handle->setArgument("response4", rsp);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Call all installed callouts
|
|
|
|
HooksManager::callCallouts(hook_index_pkt4_send_,
|
|
|
|
*callout_handle);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
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".
|
|
|
|
if (callout_handle->getSkip()) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_HOOKS, DHCP4_HOOK_PACKET_SEND_SKIP);
|
|
|
|
skip_pack = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skip_pack) {
|
|
|
|
try {
|
|
|
|
rsp->pack();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_PACKET_SEND_FAIL)
|
|
|
|
.arg(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2013-08-23 11:26:36 +02:00
|
|
|
// Let's execute all callouts registered for buffer4_send
|
2013-10-10 18:02:06 +02:00
|
|
|
if (HooksManager::calloutsPresent(Hooks.hook_index_buffer4_send_)) {
|
2013-08-19 15:08:08 +02:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(query);
|
2012-09-06 15:40:12 +01:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Delete previously set arguments
|
|
|
|
callout_handle->deleteAllArguments();
|
2012-09-06 15:40:12 +01:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Pass incoming packet as argument
|
|
|
|
callout_handle->setArgument("response4", rsp);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Call callouts
|
2013-08-23 11:26:36 +02:00
|
|
|
HooksManager::callCallouts(Hooks.hook_index_buffer4_send_,
|
|
|
|
*callout_handle);
|
2012-09-06 15:40:12 +01:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// 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->getSkip()) {
|
2013-08-23 11:26:36 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_BUFFER_SEND_SKIP);
|
2013-08-19 15:08:08 +02:00
|
|
|
continue;
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
|
|
|
callout_handle->getArgument("response4", rsp);
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA,
|
|
|
|
DHCP4_RESPONSE_DATA)
|
|
|
|
.arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
|
|
|
|
|
|
|
|
sendPacket(rsp);
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_PACKET_SEND_FAIL)
|
|
|
|
.arg(e.what());
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2013-01-14 17:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
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-09-02 17:21:11 +02:00
|
|
|
isc::dhcp_ddns::D2Dhcid
|
2013-09-03 18:39:48 +02:00
|
|
|
Dhcpv4Srv::computeDhcid(const Lease4Ptr& lease) {
|
|
|
|
if (!lease) {
|
|
|
|
isc_throw(DhcidComputeError, "a pointer to the lease must be not"
|
|
|
|
" NULL to compute DHCID");
|
|
|
|
|
|
|
|
} else if (lease->hostname_.empty()) {
|
|
|
|
isc_throw(DhcidComputeError, "unable to compute the DHCID for the"
|
|
|
|
" lease which has empty hostname set");
|
|
|
|
|
2013-09-02 17:21:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:39:48 +02:00
|
|
|
// In order to compute DHCID the client's hostname must be encoded in
|
|
|
|
// canonical wire format. It is unlikely that the FQDN is malformed
|
|
|
|
// because it is validated by the classes which encapsulate options
|
|
|
|
// carrying client FQDN. However, if the client name was carried in the
|
|
|
|
// Hostname option it is more likely as it carries the hostname as a
|
|
|
|
// regular string.
|
|
|
|
std::vector<uint8_t> fqdn_wire;
|
|
|
|
try {
|
|
|
|
OptionDataTypeUtil::writeFqdn(lease->hostname_, fqdn_wire, true);
|
2013-09-02 17:21:11 +02:00
|
|
|
|
2013-09-03 18:39:48 +02:00
|
|
|
} catch (const Exception& ex) {
|
|
|
|
isc_throw(DhcidComputeError, "unable to compute DHCID because the"
|
|
|
|
" hostname: " << lease->hostname_ << " is invalid");
|
2013-09-02 17:21:11 +02:00
|
|
|
|
|
|
|
}
|
2013-09-03 18:39:48 +02:00
|
|
|
|
|
|
|
// Prefer client id to HW address to compute DHCID. If Client Id is
|
|
|
|
// NULL, use HW address.
|
2013-09-02 17:21:11 +02:00
|
|
|
try {
|
2013-09-03 18:39:48 +02:00
|
|
|
if (lease->client_id_) {
|
|
|
|
return (D2Dhcid(lease->client_id_->getClientId(), fqdn_wire));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
HWAddrPtr hwaddr(new HWAddr(lease->hwaddr_, HTYPE_ETHER));
|
|
|
|
return (D2Dhcid(hwaddr, fqdn_wire));
|
|
|
|
}
|
2013-09-02 17:21:11 +02:00
|
|
|
} catch (const Exception& ex) {
|
2013-09-03 18:39:48 +02:00
|
|
|
isc_throw(DhcidComputeError, "unable to compute DHCID: "
|
|
|
|
<< ex.what());
|
2013-09-02 17:21:11 +02:00
|
|
|
|
2013-09-03 18:39:48 +02:00
|
|
|
}
|
2013-09-02 17:21:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer) {
|
2011-12-21 16:37:11 +01:00
|
|
|
answer->setIface(question->getIface());
|
|
|
|
answer->setIndex(question->getIndex());
|
|
|
|
answer->setCiaddr(question->getCiaddr());
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2013-04-09 08:15:22 -05:00
|
|
|
answer->setSiaddr(IOAddress("0.0.0.0")); // explicitly set this to 0
|
2011-12-21 16:37:11 +01:00
|
|
|
answer->setHops(question->getHops());
|
2011-12-20 19:59:14 +01:00
|
|
|
|
|
|
|
// copy MAC address
|
2012-12-27 19:45:00 +01:00
|
|
|
answer->setHWAddr(question->getHWAddr());
|
2011-12-20 19:59:14 +01:00
|
|
|
|
|
|
|
// relay address
|
2011-12-21 16:37:11 +01:00
|
|
|
answer->setGiaddr(question->getGiaddr());
|
|
|
|
|
2013-01-08 13:53:34 +01:00
|
|
|
// Let's copy client-id to response. See RFC6842.
|
2013-10-24 14:35:28 +02:00
|
|
|
// It is possible to disable RFC6842 to keep backward compatibility
|
|
|
|
bool echo = CfgMgr::instance().echoClientId();
|
2012-12-31 16:49:37 +01:00
|
|
|
OptionPtr client_id = question->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
2013-10-24 14:35:28 +02:00
|
|
|
if (client_id && echo) {
|
2012-12-31 16:49:37 +01:00
|
|
|
answer->addOption(client_id);
|
|
|
|
}
|
2013-04-29 19:05:22 +02:00
|
|
|
|
|
|
|
// If src/dest HW addresses are used by the packet filtering class
|
2013-05-21 10:45:22 +02:00
|
|
|
// 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.
|
2013-04-29 19:05:22 +02:00
|
|
|
HWAddrPtr src_hw_addr = question->getLocalHWAddr();
|
|
|
|
if (src_hw_addr) {
|
2013-04-29 19:42:58 +02:00
|
|
|
answer->setLocalHWAddr(src_hw_addr);
|
2013-04-29 19:05:22 +02:00
|
|
|
}
|
2013-04-29 19:42:58 +02:00
|
|
|
HWAddrPtr dst_hw_addr = question->getRemoteHWAddr();
|
2013-04-29 19:05:22 +02:00
|
|
|
if (dst_hw_addr) {
|
2013-04-29 19:42:58 +02:00
|
|
|
answer->setRemoteHWAddr(dst_hw_addr);
|
2013-04-29 19:05:22 +02:00
|
|
|
}
|
2013-10-10 19:20:02 +02:00
|
|
|
|
|
|
|
// If this packet is relayed, we want to copy Relay Agent Info option
|
|
|
|
OptionPtr rai = question->getOption(DHO_DHCP_AGENT_OPTIONS);
|
|
|
|
if (rai) {
|
|
|
|
answer->addOption(rai);
|
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type) {
|
2012-02-03 16:54:41 +01:00
|
|
|
OptionPtr opt;
|
2011-12-20 19:59:14 +01:00
|
|
|
|
|
|
|
// add Message Type Option (type 53)
|
2013-01-08 12:07:07 +01:00
|
|
|
msg->setType(msg_type);
|
2011-12-21 16:37:11 +01:00
|
|
|
|
|
|
|
// more options will be added here later
|
|
|
|
}
|
|
|
|
|
2014-01-03 16:54:16 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::appendServerID(const Pkt4Ptr& response) {
|
|
|
|
// The source address for the outbound message should have been set already.
|
|
|
|
// This is the address that to the best of the server's knowledge will be
|
|
|
|
// available from the client.
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo: perhaps we should consider some more sophisticated server id
|
|
|
|
/// generation, but for the current use cases, it should be ok.
|
2014-01-03 16:54:16 +01:00
|
|
|
response->addOption(OptionPtr(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
|
|
|
|
response->getLocalAddr()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::appendRequestedOptions(const Pkt4Ptr& question, Pkt4Ptr& msg) {
|
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.
|
|
|
|
Subnet4Ptr subnet = selectSubnet(question);
|
|
|
|
// 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
|
|
|
|
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<
|
|
|
|
OptionUint8Array>(question->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
|
|
|
|
// If there is no PRL option in the message from the client then
|
|
|
|
// there is nothing to do.
|
|
|
|
if (!option_prl) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-01-15 19:53:58 +01:00
|
|
|
// Get the codes of requested options.
|
|
|
|
const std::vector<uint8_t>& requested_opts = option_prl->getValues();
|
|
|
|
// 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) {
|
2013-10-24 18:17:29 +02:00
|
|
|
if (!msg->getOption(*opt)) {
|
|
|
|
Subnet::OptionDescriptor desc =
|
|
|
|
subnet->getOptionDescriptor("dhcp4", *opt);
|
|
|
|
if (desc.option && !msg->getOption(*opt)) {
|
|
|
|
msg->addOption(desc.option);
|
|
|
|
}
|
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
|
|
|
void
|
|
|
|
Dhcpv4Srv::appendRequestedVendorOptions(const Pkt4Ptr& question, Pkt4Ptr& answer) {
|
|
|
|
// Get the configured subnet suitable for the incoming packet.
|
|
|
|
Subnet4Ptr subnet = selectSubnet(question);
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get the vendor option
|
|
|
|
boost::shared_ptr<OptionVendor> vendor_req =
|
|
|
|
boost::dynamic_pointer_cast<OptionVendor>(question->getOption(DHO_VIVSO_SUBOPTIONS));
|
|
|
|
if (!vendor_req) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t vendor_id = vendor_req->getVendorId();
|
|
|
|
|
|
|
|
// 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.
|
2013-10-23 21:23:14 +02:00
|
|
|
OptionUint8ArrayPtr oro =
|
|
|
|
boost::dynamic_pointer_cast<OptionUint8Array>(vendor_req->getOption(DOCSIS3_V4_ORO));
|
2013-10-22 12:27:07 +02:00
|
|
|
|
|
|
|
// Option ORO not found. Don't do anything then.
|
|
|
|
if (!oro) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<OptionVendor> vendor_rsp(new OptionVendor(Option::V4, vendor_id));
|
|
|
|
|
|
|
|
// Get the list of options that client requested.
|
|
|
|
bool added = false;
|
2013-10-24 18:10:07 +02:00
|
|
|
const std::vector<uint8_t>& requested_opts = oro->getValues();
|
2013-10-22 12:27:07 +02:00
|
|
|
|
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)) {
|
|
|
|
Subnet::OptionDescriptor desc = subnet->getVendorOptionDescriptor(vendor_id,
|
|
|
|
*code);
|
|
|
|
if (desc.option) {
|
|
|
|
vendor_rsp->addOption(desc.option);
|
|
|
|
added = true;
|
|
|
|
}
|
2013-10-22 12:27:07 +02:00
|
|
|
}
|
|
|
|
|
2013-10-24 18:17:29 +02:00
|
|
|
if (added) {
|
|
|
|
answer->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
|
|
|
|
Dhcpv4Srv::appendBasicOptions(const Pkt4Ptr& question, Pkt4Ptr& msg) {
|
|
|
|
// 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,
|
|
|
|
DHO_DOMAIN_NAME };
|
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.
|
|
|
|
Subnet4Ptr subnet = selectSubnet(question);
|
|
|
|
if (!subnet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
OptionPtr opt = msg->getOption(required_options[i]);
|
|
|
|
if (!opt) {
|
|
|
|
// Check whether option has been configured.
|
|
|
|
Subnet::OptionDescriptor desc =
|
|
|
|
subnet->getOptionDescriptor("dhcp4", required_options[i]);
|
|
|
|
if (desc.option) {
|
|
|
|
msg->addOption(desc.option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
Dhcpv4Srv::processClientName(const Pkt4Ptr& query, Pkt4Ptr& answer) {
|
|
|
|
// 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 {
|
|
|
|
Option4ClientFqdnPtr fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>
|
|
|
|
(query->getOption(DHO_FQDN));
|
|
|
|
if (fqdn) {
|
|
|
|
processClientFqdnOption(fqdn, answer);
|
|
|
|
|
|
|
|
} else {
|
2014-02-19 09:09:00 -05:00
|
|
|
OptionStringPtr hostname = boost::dynamic_pointer_cast<OptionString>
|
2013-11-20 14:04:40 +01:00
|
|
|
(query->getOption(DHO_HOST_NAME));
|
|
|
|
if (hostname) {
|
|
|
|
processHostnameOption(hostname, answer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
// 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.
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_CLIENT_NAME_PROC_FAIL)
|
|
|
|
.arg(ex.what());
|
2013-08-28 14:46:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-28 16:15:56 +02:00
|
|
|
Dhcpv4Srv::processClientFqdnOption(const Option4ClientFqdnPtr& fqdn,
|
|
|
|
Pkt4Ptr& answer) {
|
|
|
|
// 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();
|
|
|
|
d2_mgr.adjustFqdnFlags<Option4ClientFqdn>(*fqdn, *fqdn_resp);
|
2013-09-02 17:19:53 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2014-01-20 10:32:32 -05:00
|
|
|
// Adjust the domain name based on domain name value and type sent by the
|
|
|
|
// client and current configuration.
|
|
|
|
d2_mgr.adjustDomainName<Option4ClientFqdn>(*fqdn, *fqdn_resp);
|
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.
|
|
|
|
// If we don't store the option in the reponse message, we will have to
|
|
|
|
// propagate it in the different way to the functions which acquire the
|
|
|
|
// lease. This would require modifications to the API of this class.
|
2013-08-28 16:15:56 +02:00
|
|
|
answer->addOption(fqdn_resp);
|
2013-08-28 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-19 09:09:00 -05:00
|
|
|
Dhcpv4Srv::processHostnameOption(const OptionStringPtr& opt_hostname,
|
2013-09-09 12:28:06 +02:00
|
|
|
Pkt4Ptr& answer) {
|
2014-01-20 10:32:32 -05:00
|
|
|
// Fetch D2 configuration.
|
|
|
|
D2ClientMgr& d2_mgr = CfgMgr::instance().getD2ClientMgr();
|
|
|
|
|
2013-09-09 14:19:12 +02:00
|
|
|
// Do nothing if the DNS updates are disabled.
|
2014-01-20 10:32:32 -05:00
|
|
|
if (!d2_mgr.ddnsEnabled()) {
|
2013-09-09 12:28:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-19 09:09:00 -05:00
|
|
|
std::string hostname = isc::util::str::trim(opt_hostname->getValue());
|
2013-09-09 14:19:12 +02:00
|
|
|
unsigned int label_count = OptionDataTypeUtil::getLabelCount(hostname);
|
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) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_EMPTY_HOSTNAME);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 14:19:12 +02:00
|
|
|
// Copy construct the hostname provided by the client. It is entirely
|
|
|
|
// possible that we will use the hostname option provided by the client
|
|
|
|
// to perform the DNS update and we will send the same option to him to
|
|
|
|
// indicate that we accepted this hostname.
|
2014-02-19 09:09:00 -05:00
|
|
|
OptionStringPtr opt_hostname_resp(new OptionString(*opt_hostname));
|
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.
|
|
|
|
|
2014-01-20 10:32:32 -05: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,
|
|
|
|
/// e.g. conversion to the puny code which may be considered at some point.
|
|
|
|
/// For now, we just remain liberal and expect that the DNS will handle
|
|
|
|
/// conversion if needed and possible.
|
|
|
|
if ((d2_mgr.getD2ClientConfig()->getReplaceClientName()) ||
|
|
|
|
(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.
|
|
|
|
opt_hostname_resp->setValue(".");
|
2013-09-09 14:19:12 +02:00
|
|
|
} else if (label_count == 2) {
|
2014-01-20 10:32:32 -05:00
|
|
|
// If there are two labels, it means that the client has specified
|
|
|
|
// the unqualified name. We have to concatenate the unqalified name
|
|
|
|
// with the domain name.
|
2014-02-19 09:09:00 -05:00
|
|
|
opt_hostname_resp->setValue(d2_mgr.qualifyName(hostname));
|
2013-09-09 14:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
answer->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");
|
|
|
|
}
|
|
|
|
|
2013-09-04 14:35:05 +02:00
|
|
|
// If old lease is not NULL, it is an indication that the lease has
|
|
|
|
// just been renewed. In such case we may need to generate the
|
|
|
|
// additional NameChangeRequest to remove an existing entry before
|
|
|
|
// we create a NameChangeRequest to add the entry for an updated lease.
|
|
|
|
// We may also decide not to generate any requests at all. This is when
|
|
|
|
// we discover that nothing has changed in the client's FQDN data.
|
|
|
|
if (old_lease) {
|
|
|
|
if (!lease->matches(*old_lease)) {
|
|
|
|
isc_throw(isc::Unexpected,
|
|
|
|
"there is no match between the current instance of the"
|
|
|
|
" lease: " << lease->toText() << ", and the previous"
|
|
|
|
" instance: " << lease->toText());
|
|
|
|
} else {
|
|
|
|
// There will be a NameChangeRequest generated to remove existing
|
|
|
|
// DNS entries if the following conditions are met:
|
|
|
|
// - The hostname is set for the existing lease, we can't generate
|
|
|
|
// removal request for non-existent hostname.
|
|
|
|
// - A server has performed reverse, forward or both updates.
|
|
|
|
// - FQDN data between the new and old lease do not match.
|
2014-01-27 17:11:59 +01:00
|
|
|
if (!lease->hasIdenticalFqdn(*old_lease)) {
|
2013-09-09 10:24:54 +02:00
|
|
|
queueNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE,
|
|
|
|
old_lease);
|
2013-09-04 14:35:05 +02:00
|
|
|
|
|
|
|
// If FQDN data from both leases match, there is no need to update.
|
2014-01-27 17:11:59 +01:00
|
|
|
} else if (lease->hasIdenticalFqdn(*old_lease)) {
|
2013-09-04 14:35:05 +02:00
|
|
|
return;
|
2014-01-27 17:11:59 +01:00
|
|
|
|
2013-09-04 14:35:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 10:24:54 +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.
|
|
|
|
queueNameChangeRequest(isc::dhcp_ddns::CHG_ADD, lease);
|
2013-09-03 18:39:48 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 15:11:26 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::
|
2013-09-09 10:24:54 +02:00
|
|
|
queueNameChangeRequest(const isc::dhcp_ddns::NameChangeType chg_type,
|
|
|
|
const Lease4Ptr& lease) {
|
|
|
|
// The hostname must not be empty, and at least one type of update
|
|
|
|
// should be requested.
|
|
|
|
if (!lease || lease->hostname_.empty() ||
|
|
|
|
(!lease->fqdn_rev_ && !lease->fqdn_fwd_)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-19 17:29:40 +01:00
|
|
|
|
|
|
|
// Create the DHCID for the NameChangeRequest.
|
2013-09-09 10:24:54 +02:00
|
|
|
D2Dhcid dhcid;
|
|
|
|
try {
|
|
|
|
dhcid = computeDhcid(lease);
|
|
|
|
} catch (const DhcidComputeError& ex) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_DHCID_COMPUTE_ERROR)
|
|
|
|
.arg(lease->toText())
|
|
|
|
.arg(ex.what());
|
|
|
|
return;
|
|
|
|
}
|
2014-02-14 09:30:58 -05:00
|
|
|
|
2013-11-19 17:29:40 +01:00
|
|
|
// Create NameChangeRequest
|
2014-02-14 09:30:58 -05:00
|
|
|
NameChangeRequestPtr ncr(new NameChangeRequest(chg_type, lease->fqdn_fwd_,
|
|
|
|
lease->fqdn_rev_,
|
|
|
|
lease->hostname_,
|
|
|
|
lease->addr_.toText(),
|
|
|
|
dhcid,
|
|
|
|
(lease->cltt_ +
|
|
|
|
lease->valid_lft_),
|
|
|
|
lease->valid_lft_));
|
|
|
|
|
2013-11-19 17:29:40 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_QUEUE_NCR)
|
|
|
|
.arg(chg_type == CHG_ADD ? "add" : "remove")
|
2014-02-14 09:30:58 -05:00
|
|
|
.arg(ncr->toText());
|
2013-09-05 15:11:26 +02:00
|
|
|
|
2014-02-14 09:30:58 -05:00
|
|
|
// And pass it to the the manager.
|
|
|
|
CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
|
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
|
|
|
|
Dhcpv4Srv::assignLease(const Pkt4Ptr& question, Pkt4Ptr& answer) {
|
2012-12-31 16:49:37 +01:00
|
|
|
|
|
|
|
// We need to select a subnet the client is connected in.
|
|
|
|
Subnet4Ptr subnet = selectSubnet(question);
|
|
|
|
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
|
|
|
|
// misconfiguration of the server (or some relays). We will continue to
|
|
|
|
// process this message, but our response will be almost useless: no
|
|
|
|
// addresses or prefixes, no subnet specific configuration etc. The only
|
|
|
|
// thing this client can get is some global information (like DNS
|
|
|
|
// servers).
|
|
|
|
|
2014-01-20 10:32:32 -05:00
|
|
|
// perhaps this should be logged on some higher level? This is most
|
|
|
|
// likely configuration bug.
|
2012-12-31 16:49:37 +01:00
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_SUBNET_SELECTION_FAILED)
|
|
|
|
.arg(question->getRemoteAddr().toText())
|
|
|
|
.arg(serverReceivedPacketName(question->getType()));
|
2013-01-08 12:07:07 +01:00
|
|
|
answer->setType(DHCPNAK);
|
2012-12-31 16:49:37 +01:00
|
|
|
answer->setYiaddr(IOAddress("0.0.0.0"));
|
|
|
|
return;
|
|
|
|
}
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2013-10-11 20:03:40 +02:00
|
|
|
// Set up siaddr. Perhaps assignLease is not the best place to call this
|
|
|
|
// as siaddr has nothing to do with a lease, but otherwise we would have
|
|
|
|
// to select subnet twice (performance hit) or update too many functions
|
|
|
|
// at once.
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo: move subnet selection to a common code
|
2013-10-11 20:03:40 +02:00
|
|
|
answer->setSiaddr(subnet->getSiaddr());
|
|
|
|
|
2013-01-08 13:53:34 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_SUBNET_SELECTED)
|
|
|
|
.arg(subnet->toText());
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// Get client-id option
|
|
|
|
ClientIdPtr client_id;
|
|
|
|
OptionPtr opt = question->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
|
|
|
if (opt) {
|
|
|
|
client_id = ClientIdPtr(new ClientId(opt->getData()));
|
|
|
|
}
|
2014-07-22 16:00:46 +02:00
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// client-id is not mandatory in DHCPv4
|
2011-12-20 19:59:14 +01: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<
|
|
|
|
OptionCustom>(question->getOption(DHO_DHCP_SERVER_IDENTIFIER));
|
|
|
|
|
2014-07-21 22:16:01 +02:00
|
|
|
// Try to get the Requested IP Address option and use the address as a hint
|
|
|
|
// for the allocation engine. If the server doesn't already have a lease
|
|
|
|
// for this client it will try to allocate the one requested.
|
|
|
|
OptionCustomPtr opt_requested_address = boost::dynamic_pointer_cast<
|
|
|
|
OptionCustom>(question->getOption(DHO_DHCP_REQUESTED_ADDRESS));
|
|
|
|
IOAddress hint("0.0.0.0");
|
|
|
|
if (opt_requested_address) {
|
|
|
|
hint = opt_requested_address->readAddress();
|
|
|
|
}
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
HWAddrPtr hwaddr = question->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.
|
2013-01-08 13:53:34 +01:00
|
|
|
bool fake_allocation = (question->getType() == DHCPDISCOVER);
|
2012-12-31 16:49:37 +01:00
|
|
|
|
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
|
|
|
|
// determine whether the client's notion of the address has to be verified.
|
|
|
|
if (!fake_allocation && !opt_serverid && opt_requested_address) {
|
|
|
|
Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(hint);
|
|
|
|
if (!lease) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_INVALID_ADDRESS_INIT_REBOOT)
|
2014-07-23 14:44:21 +02:00
|
|
|
.arg(hint.toText())
|
2014-07-22 16:00:46 +02:00
|
|
|
.arg(client_id ? client_id->toText():"(no client-id)")
|
2014-07-23 14:44:21 +02:00
|
|
|
.arg(hwaddr ? hwaddr->toText():"(no hwaddr info)");
|
2014-07-22 16:00:46 +02:00
|
|
|
|
|
|
|
answer->setType(DHCPNAK);
|
|
|
|
answer->setYiaddr(IOAddress("0.0.0.0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-16 13:29:41 +02:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(question);
|
|
|
|
|
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<
|
|
|
|
Option4ClientFqdn>(answer->getOption(DHO_FQDN));
|
|
|
|
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>
|
2013-09-09 19:22:28 +02:00
|
|
|
(answer->getOption(DHO_HOST_NAME));
|
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
|
|
|
}
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// Use allocation engine to pick a lease for this client. Allocation engine
|
|
|
|
// will try to honour the hint, but it is just a hint - some other address
|
|
|
|
// may be used instead. If fake_allocation is set to false, the lease will
|
|
|
|
// be inserted into the LeaseMgr as well.
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo pass the actual FQDN data.
|
2013-08-21 14:23:42 +02:00
|
|
|
Lease4Ptr old_lease;
|
2013-09-24 11:25:55 +02:00
|
|
|
Lease4Ptr lease = alloc_engine_->allocateLease4(subnet, client_id, hwaddr,
|
2014-07-22 16:00:46 +02:00
|
|
|
hint, fqdn_fwd, fqdn_rev,
|
|
|
|
hostname,
|
2013-09-24 11:25:55 +02:00
|
|
|
fake_allocation,
|
|
|
|
callout_handle,
|
|
|
|
old_lease);
|
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.
|
2012-12-31 16:49:37 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, fake_allocation?
|
|
|
|
DHCP4_LEASE_ADVERT:DHCP4_LEASE_ALLOC)
|
2013-01-03 13:33:06 +01:00
|
|
|
.arg(lease->addr_.toText())
|
2012-12-31 16:49:37 +01:00
|
|
|
.arg(client_id?client_id->toText():"(no client-id)")
|
2013-01-03 13:33:06 +01:00
|
|
|
.arg(hwaddr?hwaddr->toText():"(no hwaddr info)");
|
2012-12-31 16:49:37 +01:00
|
|
|
|
|
|
|
answer->setYiaddr(lease->addr_);
|
|
|
|
|
2013-09-09 19:22:28 +02:00
|
|
|
// 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.
|
|
|
|
if ((fqdn || opt_hostname) && lease->hostname_.empty()) {
|
2014-01-20 10:32:32 -05:00
|
|
|
lease->hostname_ = CfgMgr::instance()
|
|
|
|
.getD2ClientMgr().generateFqdn(lease->addr_);
|
|
|
|
|
2013-09-09 19:22:28 +02:00
|
|
|
// 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 {
|
|
|
|
// 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);
|
|
|
|
// 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) {
|
2014-02-19 09:09:00 -05:00
|
|
|
opt_hostname->setValue(lease->hostname_);
|
2013-09-09 19:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_NAME_GEN_UPDATE_FAIL)
|
|
|
|
.arg(ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
// IP Address Lease time (type 51)
|
2014-05-27 19:58:11 +02:00
|
|
|
opt.reset(new OptionUint32(Option::V4, DHO_DHCP_LEASE_TIME,
|
|
|
|
lease->valid_lft_));
|
2012-12-31 16:49:37 +01:00
|
|
|
answer->addOption(opt);
|
|
|
|
|
|
|
|
// Subnet mask (type 1)
|
|
|
|
answer->addOption(getNetmaskOption(subnet));
|
|
|
|
|
2014-05-27 19:58:11 +02:00
|
|
|
// renewal-timer (type 58)
|
|
|
|
if (!subnet->getT1().unspecified()) {
|
|
|
|
OptionUint32Ptr t1(new OptionUint32(Option::V4,
|
|
|
|
DHO_DHCP_RENEWAL_TIME,
|
|
|
|
subnet->getT1()));
|
|
|
|
answer->addOption(t1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// rebind timer (type 59)
|
|
|
|
if (!subnet->getT2().unspecified()) {
|
|
|
|
OptionUint32Ptr t2(new OptionUint32(Option::V4,
|
|
|
|
DHO_DHCP_REBINDING_TIME,
|
|
|
|
subnet->getT2()));
|
|
|
|
answer->addOption(t2);
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2014-01-20 10:32:32 -05:00
|
|
|
// Create NameChangeRequests if DDNS is enabled and this is a
|
|
|
|
// real allocation.
|
|
|
|
if (!fake_allocation && CfgMgr::instance().ddnsEnabled()) {
|
2013-09-05 15:11:26 +02:00
|
|
|
try {
|
|
|
|
createNameChangeRequests(lease, old_lease);
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_NCR_CREATION_FAILED)
|
|
|
|
.arg(ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
} else {
|
|
|
|
// Allocation engine did not allocate a lease. The engine logged
|
2014-07-23 14:44:21 +02:00
|
|
|
// cause of that failure. The only thing left is to insert
|
2012-12-31 16:49:37 +01:00
|
|
|
// status code to pass the sad news to the client.
|
|
|
|
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, fake_allocation?
|
|
|
|
DHCP4_LEASE_ADVERT_FAIL:DHCP4_LEASE_ALLOC_FAIL)
|
|
|
|
.arg(client_id?client_id->toText():"(no client-id)")
|
|
|
|
.arg(hwaddr?hwaddr->toText():"(no hwaddr info)")
|
|
|
|
.arg(hint.toText());
|
|
|
|
|
2013-01-08 12:07:07 +01:00
|
|
|
answer->setType(DHCPNAK);
|
2012-12-31 16:49:37 +01:00
|
|
|
answer->setYiaddr(IOAddress("0.0.0.0"));
|
2013-09-09 10:24:54 +02:00
|
|
|
|
|
|
|
answer->delOption(DHO_FQDN);
|
|
|
|
answer->delOption(DHO_HOST_NAME);
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:18:58 +02:00
|
|
|
void
|
2014-01-03 16:54:16 +01:00
|
|
|
Dhcpv4Srv::adjustIfaceData(const Pkt4Ptr& query, const Pkt4Ptr& response) {
|
|
|
|
adjustRemoteAddr(query, response);
|
|
|
|
|
|
|
|
// For the non-relayed message, the destination port is the client's port.
|
|
|
|
// For the relayed message, the server/relay port is a destination.
|
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.
|
|
|
|
response->setRemotePort(query->isRelayed() ? DHCP4_SERVER_PORT :
|
|
|
|
DHCP4_CLIENT_PORT);
|
|
|
|
|
2014-01-03 16:54:16 +01:00
|
|
|
// 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.
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo Consider an optimization that we use local address from
|
|
|
|
/// the query if this address is not broadcast.
|
2014-01-03 16:54:16 +01:00
|
|
|
SocketInfo sock_info = IfaceMgr::instance().getSocket(*query);
|
|
|
|
// Set local adddress, port and interface.
|
|
|
|
response->setLocalAddr(sock_info.addr_);
|
|
|
|
response->setLocalPort(DHCP4_SERVER_PORT);
|
|
|
|
response->setIface(query->getIface());
|
|
|
|
response->setIndex(query->getIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Dhcpv4Srv::adjustRemoteAddr(const Pkt4Ptr& question, const Pkt4Ptr& response) {
|
2013-05-21 15:18:58 +02:00
|
|
|
// Let's create static objects representing zeroed and broadcast
|
|
|
|
// addresses. We will use them further in this function to test
|
|
|
|
// other addresses against them. Since they are static, they will
|
|
|
|
// be created only once.
|
|
|
|
static const IOAddress zero_addr("0.0.0.0");
|
|
|
|
static const IOAddress bcast_addr("255.255.255.255");
|
|
|
|
|
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.
|
|
|
|
if (question->getType() == DHCPINFORM) {
|
|
|
|
// If client adheres to RFC2131 it will set the ciaddr and in this
|
|
|
|
// case we always unicast our response to this address.
|
|
|
|
if (question->getCiaddr() != zero_addr) {
|
|
|
|
response->setRemoteAddr(question->getCiaddr());
|
|
|
|
|
|
|
|
// If we received DHCPINFOM via relay and the ciaddr is not set we
|
|
|
|
// 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.
|
|
|
|
} else if (question->isRelayed()) {
|
|
|
|
response->setRemoteAddr(question->getGiaddr());
|
|
|
|
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 {
|
|
|
|
response->setRemoteAddr(question->getRemoteAddr());
|
|
|
|
}
|
|
|
|
// Remote addres is now set so return.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:18:58 +02:00
|
|
|
// If received relayed message, server responds to the relay address.
|
2014-01-09 11:55:31 +01:00
|
|
|
if (question->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.
|
2014-07-15 12:06:40 +02:00
|
|
|
if ((question->getType() == DHCPINFORM) &&
|
|
|
|
(question->getCiaddr() == zero_addr)) {
|
|
|
|
response->setFlags(BOOTP_BROADCAST);
|
|
|
|
}
|
2014-01-03 16:54:16 +01:00
|
|
|
response->setRemoteAddr(question->getGiaddr());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
// If giaddr is 0 but client set ciaddr, server should unicast the
|
|
|
|
// response to ciaddr.
|
|
|
|
} else if (question->getCiaddr() != zero_addr) {
|
2014-01-03 16:54:16 +01:00
|
|
|
response->setRemoteAddr(question->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) {
|
|
|
|
response->setRemoteAddr(bcast_addr);
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
// If yiaddr is set it means that we have created a lease for a client.
|
2014-01-03 16:54:16 +01:00
|
|
|
} else if (response->getYiaddr() != zero_addr) {
|
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.
|
2013-05-22 17:50:33 +02:00
|
|
|
const bool bcast_flag = ((question->getFlags() & Pkt4::FLAG_BROADCAST_MASK) != 0);
|
2013-05-21 15:18:58 +02:00
|
|
|
if (!IfaceMgr::instance().isDirectResponseSupported() || bcast_flag) {
|
2014-01-03 16:54:16 +01:00
|
|
|
response->setRemoteAddr(bcast_addr);
|
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 {
|
2014-01-03 16:54:16 +01:00
|
|
|
response->setRemoteAddr(question->getRemoteAddr());
|
2013-05-21 15:18:58 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
OptionPtr
|
|
|
|
Dhcpv4Srv::getNetmaskOption(const Subnet4Ptr& subnet) {
|
2012-12-31 16:49:37 +01:00
|
|
|
uint32_t netmask = getNetmask4(subnet->get().second);
|
|
|
|
|
|
|
|
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) {
|
2013-07-16 13:58:01 +02:00
|
|
|
|
|
|
|
sanityCheck(discover, FORBIDDEN);
|
|
|
|
|
2012-02-03 16:54:41 +01:00
|
|
|
Pkt4Ptr offer = Pkt4Ptr
|
2011-12-23 17:26:24 +01:00
|
|
|
(new Pkt4(DHCPOFFER, discover->getTransid()));
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2011-12-23 17:26:24 +01:00
|
|
|
copyDefaultFields(discover, offer);
|
|
|
|
appendDefaultOptions(offer, DHCPOFFER);
|
|
|
|
|
2013-09-09 10:24:54 +02:00
|
|
|
// If DISCOVER message contains the FQDN or Hostname option, server
|
|
|
|
// may respond to the client with the appropriate FQDN or Hostname
|
|
|
|
// option to indicate that whether it will take responsibility for
|
|
|
|
// updating DNS when the client sends REQUEST message.
|
|
|
|
processClientName(discover, offer);
|
|
|
|
|
2012-12-31 16:49:37 +01:00
|
|
|
assignLease(discover, offer);
|
2011-12-20 19:59:14 +01:00
|
|
|
|
2013-10-21 14:41:54 +02:00
|
|
|
// Adding any other options makes sense only when we got the lease.
|
|
|
|
if (offer->getYiaddr() != IOAddress("0.0.0.0")) {
|
|
|
|
appendRequestedOptions(discover, offer);
|
2013-10-24 18:10:07 +02:00
|
|
|
appendRequestedVendorOptions(discover, offer);
|
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.
|
|
|
|
appendBasicOptions(discover, offer);
|
|
|
|
}
|
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.
|
|
|
|
adjustIfaceData(discover, offer);
|
|
|
|
|
|
|
|
appendServerID(offer);
|
|
|
|
|
2011-12-20 19:59:14 +01:00
|
|
|
return (offer);
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
Pkt4Ptr
|
|
|
|
Dhcpv4Srv::processRequest(Pkt4Ptr& request) {
|
2013-08-20 15:18:27 +02:00
|
|
|
|
2013-08-23 11:26:36 +02:00
|
|
|
/// @todo Uncomment this (see ticket #3116)
|
2014-01-20 10:32:32 -05:00
|
|
|
/// sanityCheck(request, MANDATORY);
|
2013-08-20 15:18:27 +02:00
|
|
|
|
2012-02-03 16:54:41 +01:00
|
|
|
Pkt4Ptr ack = Pkt4Ptr
|
2011-12-21 16:37:11 +01:00
|
|
|
(new Pkt4(DHCPACK, request->getTransid()));
|
|
|
|
|
|
|
|
copyDefaultFields(request, ack);
|
|
|
|
appendDefaultOptions(ack, DHCPACK);
|
|
|
|
|
2013-09-09 10:24:54 +02:00
|
|
|
// If REQUEST message contains the FQDN or Hostname option, server
|
|
|
|
// 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.
|
|
|
|
processClientName(request, ack);
|
|
|
|
|
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.
|
2012-12-31 16:49:37 +01:00
|
|
|
assignLease(request, ack);
|
2011-12-21 16:37:11 +01:00
|
|
|
|
2013-10-21 14:41:54 +02:00
|
|
|
// Adding any other options makes sense only when we got the lease.
|
|
|
|
if (ack->getYiaddr() != IOAddress("0.0.0.0")) {
|
|
|
|
appendRequestedOptions(request, ack);
|
2013-10-24 18:10:07 +02:00
|
|
|
appendRequestedVendorOptions(request, ack);
|
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.
|
|
|
|
appendBasicOptions(request, ack);
|
|
|
|
}
|
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.
|
|
|
|
adjustIfaceData(request, ack);
|
|
|
|
|
|
|
|
appendServerID(ack);
|
|
|
|
|
2011-12-21 16:37:11 +01:00
|
|
|
return (ack);
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::processRelease(Pkt4Ptr& release) {
|
2013-01-08 13:53:34 +01:00
|
|
|
|
2013-08-23 11:26:36 +02:00
|
|
|
/// @todo Uncomment this (see ticket #3116)
|
2014-01-20 10:32:32 -05:00
|
|
|
/// sanityCheck(release, MANDATORY);
|
2013-08-20 15:18:27 +02:00
|
|
|
|
2013-01-08 13:53:34 +01:00
|
|
|
// Try to find client-id
|
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
|
2014-04-04 09:31:04 -04:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_RELEASE_FAIL_NO_LEASE)
|
|
|
|
.arg(client_id ? client_id->toText() : "(no client-id)")
|
|
|
|
.arg(release->getHWAddr() ?
|
|
|
|
release->getHWAddr()->toText() : "(no hwaddr info)")
|
|
|
|
.arg(release->getCiaddr().toText());
|
2013-01-08 13:53:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-01-08 13:53:34 +01:00
|
|
|
// Does the hardware address match? We don't want one client releasing
|
|
|
|
// second client's leases.
|
|
|
|
if (lease->hwaddr_ != release->getHWAddr()->hwaddr_) {
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo: Print hwaddr from lease as part of ticket #2589
|
2013-01-08 13:53:34 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_WRONG_HWADDR)
|
2013-09-09 10:24:54 +02:00
|
|
|
.arg(release->getCiaddr().toText())
|
2013-01-08 13:53:34 +01:00
|
|
|
.arg(client_id ? client_id->toText() : "(no client-id)")
|
|
|
|
.arg(release->getHWAddr()->toText());
|
|
|
|
return;
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-01-08 13:53:34 +01:00
|
|
|
// Does the lease have client-id info? If it has, then check it with what
|
|
|
|
// the client sent us.
|
|
|
|
if (lease->client_id_ && client_id && *lease->client_id_ != *client_id) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_WRONG_CLIENT_ID)
|
2013-09-09 10:24:54 +02:00
|
|
|
.arg(release->getCiaddr().toText())
|
2013-01-08 13:53:34 +01:00
|
|
|
.arg(client_id->toText())
|
|
|
|
.arg(lease->client_id_->toText());
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Delete all previous arguments
|
|
|
|
callout_handle->deleteAllArguments();
|
2013-01-08 13:53:34 +01:00
|
|
|
|
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".
|
|
|
|
if (callout_handle->getSkip()) {
|
|
|
|
skip = true;
|
2013-08-23 11:26:36 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_LEASE4_RELEASE_SKIP);
|
2013-08-19 15:08:08 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 13:53:34 +01:00
|
|
|
|
2013-08-19 15:08:08 +02:00
|
|
|
// Ok, hw and client-id match - let's release the lease.
|
|
|
|
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) {
|
2013-09-09 10:24:54 +02:00
|
|
|
// Release successful
|
2013-08-19 15:08:08 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE)
|
|
|
|
.arg(lease->addr_.toText())
|
|
|
|
.arg(client_id ? client_id->toText() : "(no client-id)")
|
|
|
|
.arg(release->getHWAddr()->toText());
|
2013-09-09 10:24:54 +02:00
|
|
|
|
2014-01-29 09:51:59 -05:00
|
|
|
if (CfgMgr::instance().ddnsEnabled()) {
|
2014-01-20 10:32:32 -05:00
|
|
|
// Remove existing DNS entries for the lease, if any.
|
|
|
|
queueNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, lease);
|
|
|
|
}
|
2013-08-19 15:08:08 +02:00
|
|
|
} else {
|
|
|
|
// Release failed -
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_RELEASE_FAIL)
|
|
|
|
.arg(lease->addr_.toText())
|
2013-01-08 13:53:34 +01:00
|
|
|
.arg(client_id ? client_id->toText() : "(no client-id)")
|
2013-08-19 15:08:08 +02:00
|
|
|
.arg(release->getHWAddr()->toText());
|
|
|
|
}
|
2013-01-08 13:53:34 +01:00
|
|
|
}
|
|
|
|
} catch (const isc::Exception& ex) {
|
|
|
|
// Rethrow the exception with a bit more data.
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_RELEASE_EXCEPTION)
|
|
|
|
.arg(ex.what())
|
|
|
|
.arg(release->getYiaddr());
|
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
|
2013-01-31 16:03:37 +00:00
|
|
|
Dhcpv4Srv::processDecline(Pkt4Ptr& /* decline */) {
|
2013-08-23 11:26:36 +02:00
|
|
|
/// @todo Implement this (also see ticket #3116)
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
Pkt4Ptr
|
|
|
|
Dhcpv4Srv::processInform(Pkt4Ptr& inform) {
|
2014-07-18 18:32:59 +02:00
|
|
|
// DHCPINFORM MUST not include server identifier.
|
|
|
|
sanityCheck(inform, FORBIDDEN);
|
|
|
|
|
2014-07-15 12:06:40 +02:00
|
|
|
Pkt4Ptr ack = Pkt4Ptr(new Pkt4(DHCPACK, inform->getTransid()));
|
|
|
|
copyDefaultFields(inform, ack);
|
|
|
|
appendRequestedOptions(inform, ack);
|
|
|
|
appendRequestedVendorOptions(inform, ack);
|
2014-07-15 14:15:26 +02:00
|
|
|
appendBasicOptions(inform, ack);
|
2014-07-15 12:06:40 +02:00
|
|
|
adjustIfaceData(inform, ack);
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
ack->setHops(0);
|
|
|
|
ack->setGiaddr(IOAddress("0.0.0.0"));
|
|
|
|
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.
|
|
|
|
appendServerID(ack);
|
|
|
|
return (ack);
|
2011-11-27 19:35:47 +08:00
|
|
|
}
|
2012-09-06 12:58:17 +01:00
|
|
|
|
|
|
|
const char*
|
|
|
|
Dhcpv4Srv::serverReceivedPacketName(uint8_t type) {
|
|
|
|
static const char* DISCOVER = "DISCOVER";
|
|
|
|
static const char* REQUEST = "REQUEST";
|
|
|
|
static const char* RELEASE = "RELEASE";
|
|
|
|
static const char* DECLINE = "DECLINE";
|
|
|
|
static const char* INFORM = "INFORM";
|
|
|
|
static const char* UNKNOWN = "UNKNOWN";
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case DHCPDISCOVER:
|
|
|
|
return (DISCOVER);
|
|
|
|
|
|
|
|
case DHCPREQUEST:
|
|
|
|
return (REQUEST);
|
|
|
|
|
|
|
|
case DHCPRELEASE:
|
|
|
|
return (RELEASE);
|
|
|
|
|
|
|
|
case DHCPDECLINE:
|
|
|
|
return (DECLINE);
|
|
|
|
|
|
|
|
case DHCPINFORM:
|
|
|
|
return (INFORM);
|
|
|
|
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return (UNKNOWN);
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
Subnet4Ptr
|
2014-02-03 18:40:36 +01:00
|
|
|
Dhcpv4Srv::selectSubnet(const Pkt4Ptr& question) const {
|
2012-12-31 16:49:37 +01:00
|
|
|
|
2013-07-15 18:41:19 +02:00
|
|
|
Subnet4Ptr subnet;
|
2013-07-19 15:54:02 +02:00
|
|
|
static const IOAddress notset("0.0.0.0");
|
2014-02-03 18:40:36 +01:00
|
|
|
static const IOAddress bcast("255.255.255.255");
|
2013-01-08 12:07:07 +01:00
|
|
|
|
2014-02-03 18:40:36 +01:00
|
|
|
// If a message is relayed, use the relay (giaddr) address to select subnet
|
|
|
|
// for the client. Note that this may result in exception if the value
|
|
|
|
// of hops does not correspond with the Giaddr. Such message is considered
|
|
|
|
// to be malformed anyway and the message will be dropped by the higher
|
|
|
|
// level functions.
|
|
|
|
if (question->isRelayed()) {
|
2014-02-10 19:54:51 +01:00
|
|
|
subnet = CfgMgr::instance().getSubnet4(question->getGiaddr(),
|
2014-02-12 15:51:04 +01:00
|
|
|
question->classes_,
|
|
|
|
true);
|
2014-02-03 18:40:36 +01:00
|
|
|
|
|
|
|
// The message is not relayed so it is sent directly by a client. But
|
|
|
|
// the client may be renewing its lease and in such case it unicasts
|
|
|
|
// its message to the server. Note that the unicast Request bypasses
|
|
|
|
// relays and the client may be in a different network, so we can't
|
|
|
|
// use IP address on the local interface to get the subnet. Instead,
|
|
|
|
// we rely on the client's address to get the subnet.
|
|
|
|
} else if ((question->getLocalAddr() != bcast) &&
|
|
|
|
(question->getCiaddr() != notset)) {
|
2014-02-10 19:54:51 +01:00
|
|
|
subnet = CfgMgr::instance().getSubnet4(question->getCiaddr(),
|
2014-02-03 19:03:27 +01:00
|
|
|
question->classes_);
|
2014-02-03 18:40:36 +01:00
|
|
|
|
2014-07-15 17:37:07 +02:00
|
|
|
// Either renewing client or the client that sends DHCPINFORM
|
|
|
|
// must set the ciaddr. But apparently some clients don't do it,
|
|
|
|
// so if the client didn't include ciaddr we will use the source
|
|
|
|
// address.
|
|
|
|
} else if ((question->getLocalAddr() != bcast) &&
|
|
|
|
(question->getRemoteAddr() != notset)) {
|
|
|
|
subnet = CfgMgr::instance().getSubnet4(question->getRemoteAddr(),
|
|
|
|
question->classes_);
|
|
|
|
|
2014-02-03 18:40:36 +01:00
|
|
|
// The message has been received from a directly connected client
|
|
|
|
// and this client appears to have no address. The IPv4 address
|
|
|
|
// assigned to the interface on which this message has been received,
|
|
|
|
// will be used to determine the subnet suitable for the client.
|
2013-01-08 12:07:07 +01:00
|
|
|
} else {
|
2014-02-10 19:54:51 +01:00
|
|
|
subnet = CfgMgr::instance().getSubnet4(question->getIface(),
|
|
|
|
question->classes_);
|
2013-01-08 12:07:07 +01:00
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
2013-08-23 11:26:36 +02:00
|
|
|
// Let's execute all callouts registered for subnet4_select
|
2013-07-19 15:54:02 +02:00
|
|
|
if (HooksManager::calloutsPresent(hook_index_subnet4_select_)) {
|
2013-07-15 18:41:19 +02:00
|
|
|
CalloutHandlePtr callout_handle = getCalloutHandle(question);
|
|
|
|
|
|
|
|
// We're reusing callout_handle from previous calls
|
|
|
|
callout_handle->deleteAllArguments();
|
|
|
|
|
|
|
|
// Set new arguments
|
|
|
|
callout_handle->setArgument("query4", question);
|
|
|
|
callout_handle->setArgument("subnet4", subnet);
|
2013-08-23 11:26:36 +02:00
|
|
|
callout_handle->setArgument("subnet4collection",
|
|
|
|
CfgMgr::instance().getSubnets4());
|
2013-07-15 18:41:19 +02:00
|
|
|
|
|
|
|
// Call user (and server-side) callouts
|
2013-08-23 11:26:36 +02:00
|
|
|
HooksManager::callCallouts(hook_index_subnet4_select_,
|
|
|
|
*callout_handle);
|
2013-07-15 18:41:19 +02:00
|
|
|
|
|
|
|
// Callouts decided to skip this step. This means that no subnet will be
|
2013-08-23 11:26:36 +02:00
|
|
|
// selected. Packet processing will continue, but it will be severly
|
|
|
|
// limited (i.e. only global options will be assigned)
|
2013-07-15 18:41:19 +02:00
|
|
|
if (callout_handle->getSkip()) {
|
2013-08-23 11:26:36 +02:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_HOOKS,
|
|
|
|
DHCP4_HOOK_SUBNET4_SELECT_SKIP);
|
2013-07-15 18:41:19 +02:00
|
|
|
return (Subnet4Ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use whatever subnet was specified by the callout
|
|
|
|
callout_handle->getArgument("subnet4", subnet);
|
2013-01-08 12:07:07 +01:00
|
|
|
}
|
2013-07-15 18:41:19 +02:00
|
|
|
|
|
|
|
return (subnet);
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
|
|
|
|
2014-01-13 18:18:46 +01:00
|
|
|
bool
|
2014-02-03 18:40:36 +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)) {
|
|
|
|
LOG_INFO(dhcp4_logger, DHCP4_NO_SUBNET_FOR_DIRECT_CLIENT)
|
|
|
|
.arg(query->getTransid())
|
|
|
|
.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)) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_NOT_FOR_US)
|
|
|
|
.arg(query->getTransid())
|
|
|
|
.arg(query->getIface());
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Dhcpv4Srv::acceptDirectRequest(const Pkt4Ptr& pkt) const {
|
|
|
|
try {
|
|
|
|
if (pkt->isRelayed()) {
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
return (false);
|
|
|
|
}
|
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.
|
|
|
|
static const IOAddress zero_addr("0.0.0.0");
|
|
|
|
try {
|
|
|
|
if (pkt->getType() == DHCPINFORM) {
|
|
|
|
if ((pkt->getRemoteAddr() == zero_addr) &&
|
|
|
|
(pkt->getCiaddr() == zero_addr)) {
|
|
|
|
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);
|
|
|
|
}
|
2014-02-03 18:40:36 +01:00
|
|
|
static const IOAddress bcast("255.255.255.255");
|
2014-02-06 18:10:07 +01:00
|
|
|
return ((pkt->getLocalAddr() != bcast || selectSubnet(pkt)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (...) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DROP_NO_TYPE)
|
|
|
|
.arg(query->getIface());
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we receive a message with a non-existing type, we are logging it.
|
|
|
|
if (type > DHCPLEASEQUERYDONE) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_UNRECOGNIZED_RCVD_PACKET_TYPE)
|
|
|
|
.arg(type)
|
|
|
|
.arg(query->getTransid());
|
|
|
|
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.
|
|
|
|
if ((type != DHCPDISCOVER) && (type != DHCPREQUEST) &&
|
|
|
|
(type != DHCPRELEASE) && (type != DHCPDECLINE) &&
|
|
|
|
(type != DHCPINFORM)) {
|
2014-02-07 16:52:30 +01:00
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
|
|
|
|
DHCP4_UNSUPPORTED_RCVD_PACKET_TYPE)
|
|
|
|
.arg(type)
|
|
|
|
.arg(query->getTransid());
|
2014-02-06 18:10:07 +01:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
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
|
|
|
|
// we accept the message because it is targetted to all servers.
|
|
|
|
// 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.
|
2014-01-14 18:47:42 +01:00
|
|
|
return (IfaceMgr::instance().hasOpenSocket(server_id));
|
2014-01-13 18:18:46 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:07:54 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::sanityCheck(const Pkt4Ptr& pkt, RequirementLevel serverid) {
|
2012-12-31 16:49:37 +01:00
|
|
|
OptionPtr server_id = pkt->getOption(DHO_DHCP_SERVER_IDENTIFIER);
|
|
|
|
switch (serverid) {
|
|
|
|
case FORBIDDEN:
|
|
|
|
if (server_id) {
|
|
|
|
isc_throw(RFCViolation, "Server-id option was not expected, but "
|
|
|
|
<< "received in " << serverReceivedPacketName(pkt->getType()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MANDATORY:
|
|
|
|
if (!server_id) {
|
|
|
|
isc_throw(RFCViolation, "Server-id option was expected, but not "
|
|
|
|
" received in message "
|
|
|
|
<< serverReceivedPacketName(pkt->getType()));
|
|
|
|
}
|
|
|
|
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
|
2013-07-19 15:54:02 +02:00
|
|
|
if (pkt->getHWAddr() && !pkt->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)
|
|
|
|
OptionPtr client_id = pkt->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
isc_throw(RFCViolation, "Missing or useless client-id and no HW address "
|
|
|
|
" provided in message "
|
|
|
|
<< serverReceivedPacketName(pkt->getType()));
|
|
|
|
}
|
2012-12-31 16:49:37 +01:00
|
|
|
}
|
2013-01-31 12:16:22 +00:00
|
|
|
|
2013-07-19 12:45:00 +02:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::openActiveSockets(const uint16_t port,
|
|
|
|
const bool use_bcast) {
|
|
|
|
IfaceMgr::instance().closeSockets();
|
|
|
|
|
|
|
|
// Get the reference to the collection of interfaces. This reference should
|
|
|
|
// be valid as long as the program is run because IfaceMgr is a singleton.
|
|
|
|
// Therefore we can safely iterate over instances of all interfaces and
|
|
|
|
// modify their flags. Here we modify flags which indicate whether socket
|
|
|
|
// should be open for a particular interface or not.
|
|
|
|
const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
|
|
|
|
for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
|
|
|
|
iface != ifaces.end(); ++iface) {
|
|
|
|
Iface* iface_ptr = IfaceMgr::instance().getIface(iface->getName());
|
2013-07-19 16:35:36 +02:00
|
|
|
if (iface_ptr == NULL) {
|
|
|
|
isc_throw(isc::Unexpected, "Interface Manager returned NULL"
|
|
|
|
<< " instance of the interface when DHCPv4 server was"
|
|
|
|
<< " trying to reopen sockets after reconfiguration");
|
|
|
|
}
|
2013-07-19 12:45:00 +02:00
|
|
|
if (CfgMgr::instance().isActiveIface(iface->getName())) {
|
|
|
|
iface_ptr->inactive4_ = false;
|
|
|
|
LOG_INFO(dhcp4_logger, DHCP4_ACTIVATE_INTERFACE)
|
|
|
|
.arg(iface->getFullName());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// For deactivating interface, it should be sufficient to log it
|
|
|
|
// on the debug level because it is more useful to know what
|
|
|
|
// interface is activated which is logged on the info level.
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC,
|
|
|
|
DHCP4_DEACTIVATE_INTERFACE).arg(iface->getName());
|
|
|
|
iface_ptr->inactive4_ = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Let's reopen active sockets. openSockets4 will check internally whether
|
|
|
|
// sockets are marked active or inactive.
|
2014-01-20 10:32:32 -05:00
|
|
|
/// @todo Optimization: we should not reopen all sockets but rather select
|
|
|
|
/// those that have been affected by the new configuration.
|
2013-11-29 13:30:03 +01:00
|
|
|
isc::dhcp::IfaceMgrErrorMsgCallback error_handler =
|
|
|
|
boost::bind(&Dhcpv4Srv::ifaceMgrSocket4ErrorHandler, _1);
|
|
|
|
if (!IfaceMgr::instance().openSockets4(port, use_bcast, error_handler)) {
|
2013-07-19 12:45:00 +02:00
|
|
|
LOG_WARN(dhcp4_logger, DHCP4_NO_SOCKETS_OPEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 21:17:07 +02:00
|
|
|
size_t
|
|
|
|
Dhcpv4Srv::unpackOptions(const OptionBuffer& buf,
|
2014-02-03 18:40:36 +01:00
|
|
|
const std::string& option_space,
|
|
|
|
isc::dhcp::OptionCollection& options) {
|
2013-10-04 21:17:07 +02:00
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
OptionDefContainer option_defs;
|
2013-10-08 21:05:48 +02:00
|
|
|
if (option_space == "dhcp4") {
|
2013-10-04 21:17:07 +02:00
|
|
|
// Get the list of stdandard option definitions.
|
2013-10-08 21:05:48 +02:00
|
|
|
option_defs = LibDHCP::getOptionDefs(Option::V4);
|
2013-10-04 21:17:07 +02:00
|
|
|
} else if (!option_space.empty()) {
|
|
|
|
OptionDefContainerPtr option_defs_ptr =
|
|
|
|
CfgMgr::instance().getOptionDefs(option_space);
|
|
|
|
if (option_defs_ptr != NULL) {
|
|
|
|
option_defs = *option_defs_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get the search index #1. It allows to search for option definitions
|
|
|
|
// using option code.
|
|
|
|
const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
|
|
|
|
|
|
|
|
// The buffer being read comprises a set of options, each starting with
|
|
|
|
// a one-byte type code and a one-byte length field.
|
|
|
|
while (offset + 1 <= buf.size()) {
|
|
|
|
uint8_t opt_type = buf[offset++];
|
|
|
|
|
|
|
|
// DHO_END is a special, one octet long option
|
|
|
|
if (opt_type == DHO_END)
|
|
|
|
return (offset); // just return. Don't need to add DHO_END option
|
|
|
|
|
|
|
|
// DHO_PAD is just a padding after DHO_END. Let's continue parsing
|
|
|
|
// in case we receive a message without DHO_END.
|
|
|
|
if (opt_type == DHO_PAD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (offset + 1 >= buf.size()) {
|
|
|
|
// opt_type must be cast to integer so as it is not treated as
|
|
|
|
// unsigned char value (a number is presented in error message).
|
|
|
|
isc_throw(OutOfRange, "Attempt to parse truncated option "
|
|
|
|
<< static_cast<int>(opt_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t opt_len = buf[offset++];
|
|
|
|
if (offset + opt_len > buf.size()) {
|
|
|
|
isc_throw(OutOfRange, "Option parse failed. Tried to parse "
|
|
|
|
<< offset + opt_len << " bytes from " << buf.size()
|
|
|
|
<< "-byte long buffer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all definitions with the particular option code. Note that option code
|
|
|
|
// is non-unique within this container however at this point we expect
|
|
|
|
// to get one option definition with the particular code. If more are
|
|
|
|
// returned we report an error.
|
|
|
|
const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
|
|
|
|
// Get the number of returned option definitions for the option code.
|
|
|
|
size_t num_defs = distance(range.first, range.second);
|
|
|
|
|
|
|
|
OptionPtr opt;
|
|
|
|
if (num_defs > 1) {
|
|
|
|
// Multiple options of the same code are not supported right now!
|
|
|
|
isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
|
|
|
|
" for option type " << static_cast<int>(opt_type)
|
|
|
|
<< " returned. Currently it is not supported to initialize"
|
|
|
|
<< " multiple option definitions for the same option code."
|
|
|
|
<< " This will be supported once support for option spaces"
|
|
|
|
<< " is implemented");
|
|
|
|
} else if (num_defs == 0) {
|
|
|
|
opt = OptionPtr(new Option(Option::V4, opt_type,
|
|
|
|
buf.begin() + offset,
|
|
|
|
buf.begin() + offset + opt_len));
|
|
|
|
opt->setEncapsulatedSpace("dhcp4");
|
|
|
|
} else {
|
|
|
|
// The option definition has been found. Use it to create
|
|
|
|
// the option instance from the provided buffer chunk.
|
|
|
|
const OptionDefinitionPtr& def = *(range.first);
|
|
|
|
assert(def);
|
|
|
|
opt = def->optionFactory(Option::V4, opt_type,
|
|
|
|
buf.begin() + offset,
|
|
|
|
buf.begin() + offset + opt_len,
|
|
|
|
boost::bind(&Dhcpv4Srv::unpackOptions,
|
|
|
|
this, _1, _2, _3));
|
|
|
|
}
|
|
|
|
|
|
|
|
options.insert(std::make_pair(opt_type, opt));
|
|
|
|
offset += opt_len;
|
|
|
|
}
|
|
|
|
return (offset);
|
|
|
|
}
|
|
|
|
|
2013-11-29 13:30:03 +01:00
|
|
|
void
|
|
|
|
Dhcpv4Srv::ifaceMgrSocket4ErrorHandler(const std::string& errmsg) {
|
|
|
|
// Log the reason for socket opening failure and return.
|
|
|
|
LOG_WARN(dhcp4_logger, DHCP4_OPEN_SOCKET_FAIL).arg(errmsg);
|
|
|
|
}
|
2013-07-19 12:45:00 +02:00
|
|
|
|
2013-10-18 11:10:54 +02:00
|
|
|
void Dhcpv4Srv::classifyPacket(const Pkt4Ptr& pkt) {
|
|
|
|
boost::shared_ptr<OptionString> vendor_class =
|
|
|
|
boost::dynamic_pointer_cast<OptionString>(pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER));
|
|
|
|
|
|
|
|
string classes = "";
|
|
|
|
|
|
|
|
if (!vendor_class) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DOCSIS specific section
|
2014-01-17 20:51:34 +01:00
|
|
|
|
|
|
|
// Let's keep this as a series of checks. So far we're supporting only
|
|
|
|
// docsis3.0, but there are also docsis2.0, docsis1.1 and docsis1.0. We
|
|
|
|
// may come up with adding several classes, e.g. for docsis2.0 we would
|
|
|
|
// add classes docsis2.0, docsis1.1 and docsis1.0.
|
|
|
|
|
|
|
|
// Also we are using find, because we have at least one traffic capture
|
|
|
|
// where the user class was followed by a space ("docsis3.0 ").
|
|
|
|
|
|
|
|
// For now, the code is very simple, but it is expected to get much more
|
|
|
|
// complex soon. One specific case is that the vendor class is an option
|
|
|
|
// sent by the client, so we should not trust it. To confirm that the device
|
|
|
|
// is indeed a modem, John B. suggested to check whether chaddr field
|
|
|
|
// quals subscriber-id option that was inserted by the relay (CMTS).
|
|
|
|
// This kind of logic will appear here soon.
|
|
|
|
if (vendor_class->getValue().find(DOCSIS3_CLASS_MODEM) != std::string::npos) {
|
2014-02-18 13:49:50 +01:00
|
|
|
pkt->addClass(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_MODEM);
|
|
|
|
classes += string(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_MODEM) + " ";
|
2013-12-31 15:10:43 +01:00
|
|
|
} else
|
2014-01-17 20:51:34 +01:00
|
|
|
if (vendor_class->getValue().find(DOCSIS3_CLASS_EROUTER) != std::string::npos) {
|
2014-02-18 13:49:50 +01:00
|
|
|
pkt->addClass(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_EROUTER);
|
|
|
|
classes += string(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_EROUTER) + " ";
|
2014-01-17 20:51:34 +01:00
|
|
|
} else {
|
2014-02-18 13:49:50 +01:00
|
|
|
classes += VENDOR_CLASS_PREFIX + vendor_class->getValue();
|
|
|
|
pkt->addClass(VENDOR_CLASS_PREFIX + vendor_class->getValue());
|
2013-10-18 11:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!classes.empty()) {
|
|
|
|
LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_CLASS_ASSIGNED)
|
|
|
|
.arg(classes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Dhcpv4Srv::classSpecificProcessing(const Pkt4Ptr& query, const Pkt4Ptr& rsp) {
|
|
|
|
|
|
|
|
Subnet4Ptr subnet = selectSubnet(query);
|
|
|
|
if (!subnet) {
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2014-02-18 17:35:58 +01:00
|
|
|
if (query->inClass(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_MODEM)) {
|
2013-10-18 11:10:54 +02:00
|
|
|
|
2013-12-31 15:10:43 +01:00
|
|
|
// Set next-server. This is TFTP server address. Cable modems will
|
|
|
|
// download their configuration from that server.
|
|
|
|
rsp->setSiaddr(subnet->getSiaddr());
|
2013-10-18 11:10:54 +02:00
|
|
|
|
2013-12-31 15:10:43 +01:00
|
|
|
// Now try to set up file field in DHCPv4 packet. We will just copy
|
|
|
|
// content of the boot-file option, which contains the same information.
|
2013-10-18 11:10:54 +02:00
|
|
|
Subnet::OptionDescriptor desc =
|
|
|
|
subnet->getOptionDescriptor("dhcp4", DHO_BOOT_FILE_NAME);
|
|
|
|
|
|
|
|
if (desc.option) {
|
|
|
|
boost::shared_ptr<OptionString> boot =
|
|
|
|
boost::dynamic_pointer_cast<OptionString>(desc.option);
|
|
|
|
if (boot) {
|
|
|
|
std::string filename = boot->getValue();
|
|
|
|
rsp->setFile((const uint8_t*)filename.c_str(), filename.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 12:09:08 +01:00
|
|
|
if (query->inClass(VENDOR_CLASS_PREFIX + DOCSIS3_CLASS_EROUTER)) {
|
2013-10-18 11:10:54 +02:00
|
|
|
|
2013-12-31 15:10:43 +01:00
|
|
|
// Do not set TFTP server address for eRouter devices.
|
|
|
|
rsp->setSiaddr(IOAddress("0.0.0.0"));
|
2013-10-18 11:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Dhcpv4Srv::d2ClientErrorHandler(const
|
|
|
|
dhcp_ddns::NameChangeSender::Result result,
|
|
|
|
dhcp_ddns::NameChangeRequestPtr& ncr) {
|
|
|
|
LOG_ERROR(dhcp4_logger, DHCP4_DDNS_REQUEST_SEND_FAILED).
|
|
|
|
arg(result).arg((ncr ? ncr->toText() : " NULL "));
|
2014-07-02 09:00:48 +02:00
|
|
|
// We cannot communicate with kea-dhcp-ddns, suspend futher updates.
|
2014-02-13 16:08:43 -05:00
|
|
|
/// @todo We may wish to revisit this, but for now we will simpy turn
|
|
|
|
/// them off.
|
|
|
|
CfgMgr::instance().getD2ClientMgr().suspendUpdates();
|
|
|
|
}
|
|
|
|
|
2013-01-31 12:16:22 +00:00
|
|
|
} // namespace dhcp
|
|
|
|
} // namespace isc
|