Kea 3.1.1
dhcp4_srv.h
Go to the documentation of this file.
1// Copyright (C) 2011-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef DHCPV4_SRV_H
8#define DHCPV4_SRV_H
9
10#include <asiolink/io_service.h>
11#include <dhcp/dhcp4.h>
12#include <dhcp/option.h>
13#include <dhcp/option_string.h>
15#include <dhcp/option_custom.h>
16#include <dhcp/pkt4.h>
17#include <dhcp_ddns/ncr_msg.h>
21#include <dhcpsrv/cfg_option.h>
24#include <dhcpsrv/subnet.h>
26#include <process/daemon.h>
27
28#include <functional>
29#include <iostream>
30#include <queue>
31
32// Undefine the macro OPTIONAL which is defined in some operating
33// systems but conflicts with a member of the RequirementLevel enum in
34// the server class.
35
36#ifdef OPTIONAL
37#undef OPTIONAL
38#endif
39
40namespace isc {
41namespace dhcp {
42
63public:
78 Dhcpv4Exchange(const AllocEnginePtr& alloc_engine, const Pkt4Ptr& query,
80 const ConstSubnet4Ptr& subnet, bool& drop);
81
88 void initResponse();
89
93 void initResponse4o6();
94
96 Pkt4Ptr getQuery() const {
97 return (query_);
98 }
99
104 return (resp_);
105 }
106
109 resp_.reset();
110 }
111
114 return (context_);
115 }
116
119 return (cfg_option_list_);
120 }
121
124 return (cfg_option_list_);
125 }
126
128 bool getIPv6OnlyPreferred() const {
129 return (ipv6_only_preferred_);
130 }
131
135 void setIPv6OnlyPreferred(bool ipv6_only_preferred) {
136 ipv6_only_preferred_ = ipv6_only_preferred;
137 }
138
142
152
159 static void removeDependentEvaluatedClasses(const Pkt4Ptr& query);
160
165
174
185 static void classifyPacket(const Pkt4Ptr& pkt);
186
197 static void evaluateClasses(const Pkt4Ptr& pkt, bool depend_on_known);
198
199private:
200
207 static void classifyByVendor(const Pkt4Ptr& pkt);
208
218 void copyDefaultFields();
219
229 void copyDefaultOptions();
230
232 AllocEnginePtr alloc_engine_;
233
235 Pkt4Ptr query_;
236
238 Pkt4Ptr resp_;
239
242
246 CfgOptionList cfg_option_list_;
247
249 bool ipv6_only_preferred_;
250};
251
253typedef boost::shared_ptr<Dhcpv4Exchange> Dhcpv4ExchangePtr;
254
255
269private:
270
272 asiolink::IOServicePtr io_service_;
273
274public:
275
282
302 Dhcpv4Srv(uint16_t server_port = DHCP4_SERVER_PORT,
303 uint16_t client_port = 0,
304 const bool use_bcast = true,
305 const bool direct_response_desired = true);
306
308 virtual ~Dhcpv4Srv();
309
314 bool inTestMode() const {
315 return (server_port_ == 0);
316 }
317
320 return (io_service_);
321 }
322
327
334 return (cb_control_);
335 }
336
339 static std::string getVersion(bool extended);
340
347 int run();
348
354 void runOne();
355
363
371
378 void sendResponseNoThrow(hooks::CalloutHandlePtr& callout_handle,
379 Pkt4Ptr& query, Pkt4Ptr& rsp,
380 ConstSubnet4Ptr& subnet);
381
390 Pkt4Ptr processPacket(Pkt4Ptr query, bool allow_answer_park = true);
391
400 Pkt4Ptr processDhcp4Query(Pkt4Ptr query, bool allow_answer_park);
401
410 bool allow_answer_park);
411
420 bool allow_answer_park);
421
432 bool allow_answer_park);
433
442 bool allow_answer_park);
443
445 void shutdown() override;
446
450
451
458 uint16_t getServerPort() const;
459
464 bool useBroadcast() const {
465 return (use_bcast_);
466 }
467
468
474 void startD2();
475
481 void stopD2();
482
497 virtual void d2ClientErrorHandler(const dhcp_ddns::
498 NameChangeSender::Result result,
500
504 void discardPackets();
505
510 return (test_send_responses_to_source_);
511 }
512
517 void initContext0(const Pkt4Ptr& query,
519
527 bool earlyGHRLookup(const Pkt4Ptr& query,
529
530protected:
531
538
539
559 bool accept(const Pkt4Ptr& query);
560
588 bool acceptDirectRequest(const Pkt4Ptr& query);
589
608 bool acceptMessageType(const Pkt4Ptr& query) const;
609
623 bool acceptServerId(const Pkt4Ptr& pkt) const;
625
635 static void sanityCheck(const Pkt4Ptr& query);
636
645 static void sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid);
646
658
674
684
695
703
711
720
732
746 bool assignZero(ConstSubnet4Ptr& subnet,
747 const ClientClasses& client_classes);
748
763 void assignLease(Dhcpv4Exchange& ex);
764
783 const Lease4Ptr& lease, const Pkt4Ptr& query,
784 const Pkt4Ptr& resp, bool client_name_changed);
785
811 void setTeeTimes(const Lease4Ptr& lease,
812 const ConstSubnet4Ptr& subnet,
813 Pkt4Ptr resp);
814
827
840
879
887
891 void setSendResponsesToSource(bool value) {
892 test_send_responses_to_source_ = value;
893 }
894
909 void serverDecline(hooks::CalloutHandlePtr& callout_handle, Pkt4Ptr& query,
910 Lease4Ptr lease, bool lease_exists);
911
922 void serverDeclineNoThrow(hooks::CalloutHandlePtr& callout_handle, Pkt4Ptr& query,
923 Lease4Ptr lease, bool lease_exists);
924
925public:
926
933 static const std::string VENDOR_CLASS_PREFIX;
934
935private:
946 void processClientFqdnOption(Dhcpv4Exchange& ex);
947
979 void processHostnameOption(Dhcpv4Exchange& ex);
980
994 void declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline,
996
997protected:
998
1014 void createNameChangeRequests(const Lease4Ptr& lease,
1015 const Lease4Ptr& old_lease,
1016 const DdnsParams& ddns_params);
1017
1026 void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
1027
1046 static void appendServerID(Dhcpv4Exchange& ex);
1047
1052 static uint16_t checkRelayPort(const Dhcpv4Exchange& ex);
1053
1089
1106
1113 static std::string srvidToString(const OptionPtr& opt);
1114
1122 bool& drop,
1123 bool allow_answer_park = true);
1124
1132 bool& drop,
1133 bool allow_answer_park = true);
1134
1139 virtual Pkt4Ptr receivePacket(int timeout);
1140
1145 virtual void sendPacket(const Pkt4Ptr& pkt);
1146
1157 void classifyPacket(const Pkt4Ptr& pkt);
1158
1176 void recoverStashedAgentOption(const Pkt4Ptr& query);
1177
1178protected:
1179
1197
1207 void deferredUnpack(Pkt4Ptr& query);
1208
1215 void processPacketPktSend(hooks::CalloutHandlePtr& callout_handle,
1216 Pkt4Ptr& query, Pkt4Ptr& rsp,
1217 ConstSubnet4Ptr& subnet);
1218
1224 Pkt4Ptr& rsp);
1225
1226private:
1227
1233 void classifyByVendor(const Pkt4Ptr& pkt);
1234
1239 static OptionPtr getNetmaskOption(const ConstSubnet4Ptr& subnet);
1240
1247 static std::tuple<bool, uint32_t> parkingLimitExceeded(std::string const& hook_label);
1248
1249protected:
1250
1253
1256
1259 volatile bool shutdown_;
1260
1265 boost::shared_ptr<AllocEngine> alloc_engine_;
1266
1269
1273
1276
1277private:
1278
1281 bool test_send_responses_to_source_;
1282
1283public:
1284
1286
1289 static void processStatsReceived(const Pkt4Ptr& query);
1290
1293 static void processStatsSent(const Pkt4Ptr& response);
1294
1297 static int getHookIndexBuffer4Receive();
1298
1301 static int getHookIndexPkt4Receive();
1302
1305 static int getHookIndexSubnet4Select();
1306
1309 static int getHookIndexLease4Release();
1310
1313 static int getHookIndexPkt4Send();
1314
1317 static int getHookIndexBuffer4Send();
1318
1321 static int getHookIndexLease4Decline();
1322
1328 std::list<std::list<std::string>> jsonPathsToRedact() const final override;
1329};
1330
1331} // namespace dhcp
1332} // namespace isc
1333
1334#endif // DHCP4_SRV_H
boost::shared_ptr< ClientContext4 > ClientContext4Ptr
Pointer to the ClientContext4.
Container for storing client class names.
Definition classify.h:110
Convenience container for conveying DDNS behavioral parameters It is intended to be created per Packe...
Definition ddns_params.h:23
DHCPv4 message exchange.
Definition dhcp4_srv.h:62
AllocEngine::ClientContext4Ptr getContext() const
Returns the copy of the context for the Allocation engine.
Definition dhcp4_srv.h:113
void deleteResponse()
Removes the response message by resetting the pointer to null.
Definition dhcp4_srv.h:108
const CfgOptionList & getCfgOptionList() const
Returns the configured option list (const version)
Definition dhcp4_srv.h:123
Pkt4Ptr getQuery() const
Returns the pointer to the query from the client.
Definition dhcp4_srv.h:96
static void setHostIdentifiers(AllocEngine::ClientContext4Ptr context)
Set host identifiers within a context.
Definition dhcp4_srv.cc:452
static void classifyByVendor(const Pkt4Ptr &pkt)
Assign class using vendor-class-identifier option.
Definition dhcp4_srv.cc:614
void initResponse()
Initializes the instance of the response message.
Definition dhcp4_srv.cc:324
void setReservedMessageFields()
Sets reserved values of siaddr, sname and file in the server's response.
Definition dhcp4_srv.cc:592
CfgOptionList & getCfgOptionList()
Returns the configured option list (non-const version)
Definition dhcp4_srv.h:118
Pkt4Ptr getResponse() const
Returns the pointer to the server's response.
Definition dhcp4_srv.h:103
static void setReservedClientClasses(AllocEngine::ClientContext4Ptr context)
Assigns classes retrieved from host reservation database.
Definition dhcp4_srv.cc:568
void initResponse4o6()
Initializes the DHCPv6 part of the response message.
Definition dhcp4_srv.cc:350
static void evaluateClasses(const Pkt4Ptr &pkt, bool depend_on_known)
Evaluate classes.
Definition dhcp4_srv.cc:637
void setIPv6OnlyPreferred(bool ipv6_only_preferred)
Set the IPv6-Only Preferred flag.
Definition dhcp4_srv.h:135
Dhcpv4Exchange(const AllocEnginePtr &alloc_engine, const Pkt4Ptr &query, AllocEngine::ClientContext4Ptr &context, const ConstSubnet4Ptr &subnet, bool &drop)
Constructor.
Definition dhcp4_srv.cc:206
static void classifyPacket(const Pkt4Ptr &pkt)
Assigns incoming packet to zero or more classes.
Definition dhcp4_srv.cc:626
static void removeDependentEvaluatedClasses(const Pkt4Ptr &query)
Removed evaluated client classes.
Definition dhcp4_srv.cc:553
bool getIPv6OnlyPreferred() const
Returns the IPv6-Only Preferred flag.
Definition dhcp4_srv.h:128
void conditionallySetReservedClientClasses()
Assigns classes retrieved from host reservation database if they haven't been yet set.
Definition dhcp4_srv.cc:578
void initContext0(const Pkt4Ptr &query, AllocEngine::ClientContext4Ptr ctx)
Initialize client context (first part).
void setSendResponsesToSource(bool value)
Sets value of the test_send_responses_to_source_ flag.
Definition dhcp4_srv.h:891
int run()
Main server processing loop.
void declineLease(const Lease4Ptr &lease, const Pkt4Ptr &decline, AllocEngine::ClientContext4Ptr &context)
Marks lease as declined.
void processPacketAndSendResponse(Pkt4Ptr query)
Process a single incoming DHCPv4 packet and sends the response.
void classifyPacket(const Pkt4Ptr &pkt)
Assigns incoming packet to zero or more classes.
void appendRequestedVendorOptions(Dhcpv4Exchange &ex)
Appends requested vendor options as requested by client.
void adjustIfaceData(Dhcpv4Exchange &ex)
Set IP/UDP and interface parameters for the DHCPv4 response.
static uint16_t checkRelayPort(const Dhcpv4Exchange &ex)
Check if the relay port RAI sub-option was set in the query.
virtual ~Dhcpv4Srv()
Destructor. Used during DHCPv4 service shutdown.
Definition dhcp4_srv.cc:725
virtual Pkt4Ptr receivePacket(int timeout)
dummy wrapper around IfaceMgr::receive4
bool accept(const Pkt4Ptr &query)
Checks whether received message should be processed or discarded.
void setTeeTimes(const Lease4Ptr &lease, const ConstSubnet4Ptr &subnet, Pkt4Ptr resp)
Adds the T1 and T2 timers to the outbound response as appropriate.
static void appendServerID(Dhcpv4Exchange &ex)
Adds server identifier option to the server's response.
void postAllocateNameUpdate(const AllocEngine::ClientContext4Ptr &ctx, const Lease4Ptr &lease, const Pkt4Ptr &query, const Pkt4Ptr &resp, bool client_name_changed)
Update client name and DNS flags in the lease and response.
bool use_bcast_
Should broadcast be enabled on sockets (if true).
Definition dhcp4_srv.h:1268
void runOne()
Main server processing step.
void startD2()
Starts DHCP_DDNS client IO if DDNS updates are enabled.
static int getHookIndexBuffer4Receive()
Returns the index for "buffer4_receive" hook point.
Pkt4Ptr processRequest(Pkt4Ptr &request, AllocEngine::ClientContext4Ptr &context)
Processes incoming REQUEST and returns REPLY response.
static void processStatsReceived(const Pkt4Ptr &query)
Class methods for DHCPv4-over-DHCPv6 handler.
static int getHookIndexPkt4Send()
Returns the index for "pkt4_send" hook point.
void processDecline(Pkt4Ptr &decline, AllocEngine::ClientContext4Ptr &context)
Process incoming DHCPDECLINE messages.
Dhcpv4Srv(uint16_t server_port=DHCP4_SERVER_PORT, uint16_t client_port=0, const bool use_bcast=true, const bool direct_response_desired=true)
Default constructor.
Definition dhcp4_srv.cc:663
static int getHookIndexSubnet4Select()
Returns the index for "subnet4_select" hook point.
isc::dhcp::ConstSubnet4Ptr selectSubnet(const Pkt4Ptr &query, bool &drop, bool allow_answer_park=true)
Selects a subnet for a given client's packet.
Definition dhcp4_srv.cc:776
static void processStatsSent(const Pkt4Ptr &response)
Updates statistics for transmitted packets.
void shutdown() override
Instructs the server to shut down.
Definition dhcp4_srv.cc:770
static int getHookIndexLease4Release()
Returns the index for "lease4_release" hook point.
void adjustRemoteAddr(Dhcpv4Exchange &ex)
Sets remote addresses for outgoing packet.
static int getHookIndexPkt4Receive()
Returns the index for "pkt4_receive" hook point.
void assignLease(Dhcpv4Exchange &ex)
Assigns a lease and appends corresponding options.
void evaluateAdditionalClasses(Dhcpv4Exchange &ex)
Evaluates classes in the additional classes lists.
Pkt4Ptr processDhcp4Query(Pkt4Ptr query, bool allow_answer_park)
Process a single incoming DHCPv4 query.
asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service used by the server.
Definition dhcp4_srv.h:319
void setFixedFields(Dhcpv4Exchange &ex)
Sets fixed fields of the outgoing packet.
void appendBasicOptions(Dhcpv4Exchange &ex)
Append basic options if they are not present.
void recoverStashedAgentOption(const Pkt4Ptr &query)
Recover stashed agent options from client address lease.
void processClientName(Dhcpv4Exchange &ex)
Processes Client FQDN and Hostname Options sent by a client.
boost::shared_ptr< AllocEngine > alloc_engine_
Allocation Engine.
Definition dhcp4_srv.h:1265
void serverDecline(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &query, Lease4Ptr lease, bool lease_exists)
Renders a lease declined after the server has detected, via ping-check or other means,...
Pkt4Ptr processInform(Pkt4Ptr &inform, AllocEngine::ClientContext4Ptr &context)
Processes incoming DHCPINFORM messages.
uint16_t client_port_
UDP port number to which server sends all responses.
Definition dhcp4_srv.h:1255
void serverDeclineNoThrow(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &query, Lease4Ptr lease, bool lease_exists)
Exception safe wrapper around serverDecline()
void processPacketPktSend(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &query, Pkt4Ptr &rsp, ConstSubnet4Ptr &subnet)
Executes pkt4_send callout.
void processPacketAndSendResponseNoThrow(Pkt4Ptr query)
Process a single incoming DHCPv4 packet and sends the response.
std::list< std::list< std::string > > jsonPathsToRedact() const final override
Return a list of all paths that contain passwords or secrets for kea-dhcp4.
static std::string srvidToString(const OptionPtr &opt)
converts server-id to text Converts content of server-id option to a text representation,...
bool acceptServerId(const Pkt4Ptr &pkt) const
Verifies if the server id belongs to our server.
static const std::string VENDOR_CLASS_PREFIX
this is a prefix added to the content of vendor-class option
Definition dhcp4_srv.h:933
bool inTestMode() const
Checks if the server is running in unit test mode.
Definition dhcp4_srv.h:314
void createNameChangeRequests(const Lease4Ptr &lease, const Lease4Ptr &old_lease, const DdnsParams &ddns_params)
Creates NameChangeRequests which correspond to the lease which has been acquired.
void appendRequestedOptions(Dhcpv4Exchange &ex)
Appends options requested by client.
void setPacketStatisticsDefaults()
This function sets statistics related to DHCPv4 packets processing to their initial values.
Definition dhcp4_srv.cc:715
void processLocalizedQuery4AndSendResponse(Pkt4Ptr query, AllocEngine::ClientContext4Ptr &ctx, bool allow_answer_park)
Process a localized incoming DHCPv4 query.
NetworkStatePtr & getNetworkState()
Returns pointer to the network state used by the server.
Definition dhcp4_srv.h:324
static std::string getVersion(bool extended)
returns Kea version on stdout and exit.
void buildCfgOptionList(Dhcpv4Exchange &ex)
Build the configured option list.
volatile bool shutdown_
Indicates if shutdown is in progress.
Definition dhcp4_srv.h:1259
bool useBroadcast() const
Return bool value indicating that broadcast flags should be set on sockets.
Definition dhcp4_srv.h:464
uint16_t server_port_
UDP port number on which server listens.
Definition dhcp4_srv.h:1252
void sendResponseNoThrow(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &query, Pkt4Ptr &rsp, ConstSubnet4Ptr &subnet)
Process an unparked DHCPv4 packet and sends the response.
bool earlyGHRLookup(const Pkt4Ptr &query, AllocEngine::ClientContext4Ptr ctx)
Initialize client context and perform early global reservations lookup.
NetworkStatePtr network_state_
Holds information about disabled DHCP service and/or disabled subnet/network scopes.
Definition dhcp4_srv.h:1272
void renewLease(const Pkt4Ptr &renew, Pkt4Ptr &reply)
Attempts to renew received addresses.
void processDhcp4QueryAndSendResponse(Pkt4Ptr query, bool allow_answer_park)
Process a single incoming DHCPv4 query.
bool getSendResponsesToSource() const
Returns value of the test_send_responses_to_source_ flag.
Definition dhcp4_srv.h:509
Pkt4Ptr processDiscover(Pkt4Ptr &discover, AllocEngine::ClientContext4Ptr &context)
Processes incoming DISCOVER and returns response.
virtual void d2ClientErrorHandler(const dhcp_ddns::NameChangeSender::Result result, dhcp_ddns::NameChangeRequestPtr &ncr)
Implements the error handler for DHCP_DDNS IO errors.
uint16_t getServerPort() const
Get UDP port on which server should listen.
virtual void sendPacket(const Pkt4Ptr &pkt)
dummy wrapper around IfaceMgr::send()
static int getHookIndexBuffer4Send()
Returns the index for "buffer4_send" hook point.
void stopD2()
Stops DHCP_DDNS client IO if DDNS updates are enabled.
CBControlDHCPv4Ptr getCBControl() const
Returns an object which controls access to the configuration backends.
Definition dhcp4_srv.h:333
static void sanityCheck(const Pkt4Ptr &query)
Verifies if specified packet meets RFC requirements.
bool acceptMessageType(const Pkt4Ptr &query) const
Check if received message type is valid for the server to process.
void discardPackets()
Discards parked packets Clears the packet parking lots of all packets.
isc::dhcp::ConstSubnet4Ptr selectSubnet4o6(const Pkt4Ptr &query, bool &drop, bool allow_answer_park=true)
Selects a subnet for a given client's DHCP4o6 packet.
Definition dhcp4_srv.cc:907
static int getHookIndexLease4Decline()
Returns the index for "lease4_decline" hook point.
void processRelease(Pkt4Ptr &release, AllocEngine::ClientContext4Ptr &context)
Processes incoming DHCPRELEASE messages.
bool acceptDirectRequest(const Pkt4Ptr &query)
Check if a message sent by directly connected client should be accepted or discarded.
CBControlDHCPv4Ptr cb_control_
Controls access to the configuration backends.
Definition dhcp4_srv.h:1275
RequirementLevel
defines if certain option may, must or must not appear
Definition dhcp4_srv.h:277
Pkt4Ptr processPacket(Pkt4Ptr query, bool allow_answer_park=true)
Process a single incoming DHCPv4 packet.
void processPacketBufferSend(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &rsp)
Executes buffer4_send callout and sends the response.
bool assignZero(ConstSubnet4Ptr &subnet, const ClientClasses &client_classes)
Assign the 0.0.0.0 address to an IPv6-Only client.
void deferredUnpack(Pkt4Ptr &query)
Perform deferred option unpacking.
Pkt4Ptr processLocalizedQuery4(AllocEngine::ClientContext4Ptr &ctx, bool allow_answer_park)
Process a localized incoming DHCPv4 query.
Base class for all services.
Definition daemon.h:48
Defines the D2ClientMgr class.
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition ncr_msg.h:241
boost::shared_ptr< Dhcpv4Exchange > Dhcpv4ExchangePtr
Type representing the pointer to the Dhcpv4Exchange.
Definition dhcp4_srv.h:253
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
Definition subnet.h:458
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
boost::shared_ptr< AllocEngine > AllocEnginePtr
A pointer to the AllocEngine object.
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
boost::shared_ptr< CBControlDHCPv4 > CBControlDHCPv4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition cfg_option.h:979
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Defines the logger used by the top-level component of kea-lfc.
This file provides the classes needed to embody, compose, and decompose DNS update requests that are ...