Kea  2.1.7-git
dhcp4_srv.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
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>
18 #include <dhcpsrv/alloc_engine.h>
20 #include <dhcpsrv/cb_ctl_dhcp4.h>
21 #include <dhcpsrv/cfg_option.h>
22 #include <dhcpsrv/d2_client_mgr.h>
23 #include <dhcpsrv/network_state.h>
24 #include <dhcpsrv/subnet.h>
25 #include <hooks/callout_handle.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 
40 namespace isc {
41 namespace dhcp {
42 
63 public:
78  Dhcpv4Exchange(const AllocEnginePtr& alloc_engine, const Pkt4Ptr& query,
80  const Subnet4Ptr& 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 
108  void deleteResponse() {
109  resp_.reset();
110  }
111 
114  return (context_);
115  }
116 
119  return (cfg_option_list_);
120  }
121 
124  return (cfg_option_list_);
125  }
126 
130 
140 
147  static void removeDependentEvaluatedClasses(const Pkt4Ptr& query);
148 
153 
162 
173  static void classifyPacket(const Pkt4Ptr& pkt);
174 
185  static void evaluateClasses(const Pkt4Ptr& pkt, bool depend_on_known);
186 
187 private:
188 
195  static void classifyByVendor(const Pkt4Ptr& pkt);
196 
206  void copyDefaultFields();
207 
217  void copyDefaultOptions();
218 
220  AllocEnginePtr alloc_engine_;
221 
223  Pkt4Ptr query_;
224 
226  Pkt4Ptr resp_;
227 
230 
234  CfgOptionList cfg_option_list_;
235 };
236 
238 typedef boost::shared_ptr<Dhcpv4Exchange> Dhcpv4ExchangePtr;
239 
240 
253 class Dhcpv4Srv : public process::Daemon {
254 private:
255 
257  asiolink::IOServicePtr io_service_;
258 
259 public:
260 
262  typedef enum {
265  OPTIONAL
267 
287  Dhcpv4Srv(uint16_t server_port = DHCP4_SERVER_PORT,
288  uint16_t client_port = 0,
289  const bool use_bcast = true,
290  const bool direct_response_desired = true);
291 
293  virtual ~Dhcpv4Srv();
294 
299  bool inTestMode() const {
300  return (server_port_ == 0);
301  }
302 
305  return (io_service_);
306  }
307 
310  return (network_state_);
311  }
312 
319  return (cb_control_);
320  }
321 
324  static std::string getVersion(bool extended);
325 
332  int run();
333 
339  void run_one();
340 
347  void processPacketAndSendResponse(Pkt4Ptr& query);
348 
355  void processPacketAndSendResponseNoThrow(Pkt4Ptr& query);
356 
362  void sendResponseNoThrow(hooks::CalloutHandlePtr& callout_handle,
363  Pkt4Ptr& query, Pkt4Ptr& rsp);
364 
373  void processPacket(Pkt4Ptr& query, Pkt4Ptr& rsp,
374  bool allow_packet_park = true);
375 
383  void processDhcp4Query(Pkt4Ptr& query, Pkt4Ptr& rsp,
384  bool allow_packet_park);
385 
394  void processDhcp4QueryAndSendResponse(Pkt4Ptr& query, Pkt4Ptr& rsp,
395  bool allow_packet_park);
396 
398  void shutdown() override;
399 
403 
404  uint16_t getServerPort() const {
412  return (server_port_);
413  }
414 
419  bool useBroadcast() const {
420  return (use_bcast_);
421  }
423 
429  void startD2();
430 
436  void stopD2();
437 
452  virtual void d2ClientErrorHandler(const dhcp_ddns::
453  NameChangeSender::Result result,
455 
459  void discardPackets();
460 
465  return (test_send_responses_to_source_);
466  }
467 
475  bool earlyGHRLookup(const Pkt4Ptr& query,
477 
478 protected:
479 
486 
487  bool accept(const Pkt4Ptr& query) const;
508 
536  bool acceptDirectRequest(const Pkt4Ptr& query) const;
537 
556  bool acceptMessageType(const Pkt4Ptr& query) const;
557 
571  bool acceptServerId(const Pkt4Ptr& pkt) const;
573 
582  static void sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid);
583 
594  Pkt4Ptr processDiscover(Pkt4Ptr& discover, AllocEngine::ClientContext4Ptr& context);
595 
610  Pkt4Ptr processRequest(Pkt4Ptr& request, AllocEngine::ClientContext4Ptr& context);
611 
620  void processRelease(Pkt4Ptr& release, AllocEngine::ClientContext4Ptr& context);
621 
631  void processDecline(Pkt4Ptr& decline, AllocEngine::ClientContext4Ptr& context);
632 
639  Pkt4Ptr processInform(Pkt4Ptr& inform, AllocEngine::ClientContext4Ptr& context);
640 
647  void buildCfgOptionList(Dhcpv4Exchange& ex);
648 
656  void appendRequestedOptions(Dhcpv4Exchange& ex);
657 
668  void appendRequestedVendorOptions(Dhcpv4Exchange& ex);
669 
684  void assignLease(Dhcpv4Exchange& ex);
685 
703  void postAllocateNameUpdate(const AllocEngine::ClientContext4Ptr& ctx,
704  const Lease4Ptr& lease, const Pkt4Ptr& query,
705  const Pkt4Ptr& resp, bool client_name_changed);
706 
732  void setTeeTimes(const Lease4Ptr& lease, const Subnet4Ptr& subnet, Pkt4Ptr resp);
733 
745  void appendBasicOptions(Dhcpv4Exchange& ex);
746 
758  void setFixedFields(Dhcpv4Exchange& ex);
759 
797  void processClientName(Dhcpv4Exchange& ex);
798 
805  void setPacketStatisticsDefaults();
806 
810  void setSendResponsesToSource(bool value) {
811  test_send_responses_to_source_ = value;
812  }
813 
814 public:
815 
822  static const std::string VENDOR_CLASS_PREFIX;
823 
824 private:
835  void processClientFqdnOption(Dhcpv4Exchange& ex);
836 
868  void processHostnameOption(Dhcpv4Exchange& ex);
869 
883  void declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline,
885 
886 protected:
887 
903  void createNameChangeRequests(const Lease4Ptr& lease,
904  const Lease4Ptr& old_lease,
905  const DdnsParams& ddns_params);
906 
915  void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
916 
935  static void appendServerID(Dhcpv4Exchange& ex);
936 
941  static uint16_t checkRelayPort(const Dhcpv4Exchange& ex);
942 
977  void adjustIfaceData(Dhcpv4Exchange& ex);
978 
994  void adjustRemoteAddr(Dhcpv4Exchange& ex);
995 
1002  static std::string srvidToString(const OptionPtr& opt);
1003 
1015  isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& query,
1016  bool& drop,
1017  bool sanity_only = false) const;
1018 
1030  isc::dhcp::Subnet4Ptr selectSubnet4o6(const Pkt4Ptr& query,
1031  bool& drop,
1032  bool sanity_only = false) const;
1033 
1038  virtual Pkt4Ptr receivePacket(int timeout);
1039 
1044  virtual void sendPacket(const Pkt4Ptr& pkt);
1045 
1056  void classifyPacket(const Pkt4Ptr& pkt);
1057 
1058 protected:
1059 
1070  void requiredClassify(Dhcpv4Exchange& ex);
1071 
1081  void deferredUnpack(Pkt4Ptr& query);
1082 
1088  void processPacketPktSend(hooks::CalloutHandlePtr& callout_handle,
1089  Pkt4Ptr& query, Pkt4Ptr& rsp);
1090 
1095  void processPacketBufferSend(hooks::CalloutHandlePtr& callout_handle,
1096  Pkt4Ptr& rsp);
1097 
1098 private:
1099 
1106  void classifyByVendor(const Pkt4Ptr& pkt);
1107 
1113  static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
1114 
1115 protected:
1116 
1118  uint16_t server_port_;
1119 
1121  uint16_t client_port_;
1122 
1125  volatile bool shutdown_;
1126 
1131  boost::shared_ptr<AllocEngine> alloc_engine_;
1132 
1135 
1139 
1142 
1143 private:
1144 
1147  bool test_send_responses_to_source_;
1148 
1149 public:
1150 
1152 
1155  static void processStatsReceived(const Pkt4Ptr& query);
1156 
1159  static void processStatsSent(const Pkt4Ptr& response);
1160 
1163  static int getHookIndexBuffer4Receive();
1164 
1167  static int getHookIndexPkt4Receive();
1168 
1171  static int getHookIndexSubnet4Select();
1172 
1175  static int getHookIndexLease4Release();
1176 
1179  static int getHookIndexPkt4Send();
1180 
1183  static int getHookIndexBuffer4Send();
1184 
1187  static int getHookIndexLease4Decline();
1188 
1194  std::list<std::list<std::string>> jsonPathsToRedact() const final override;
1195 };
1196 
1197 } // namespace dhcp
1198 } // namespace isc
1199 
1200 #endif // DHCP4_SRV_H
DHCPv4 server service.
Definition: dhcp4_srv.h:253
bool inTestMode() const
Checks if the server is running in unit test mode.
Definition: dhcp4_srv.h:299
CBControlDHCPv4Ptr cb_control_
Controls access to the configuration backends.
Definition: dhcp4_srv.h:1141
uint16_t server_port_
UDP port number on which server listens.
Definition: dhcp4_srv.h:1118
static void setHostIdentifiers(AllocEngine::ClientContext4Ptr context)
Set host identifiers within a context.
Definition: dhcp4_srv.cc:390
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:524
void initResponse()
Initializes the instance of the response message.
Definition: dhcp4_srv.cc:270
static void classifyPacket(const Pkt4Ptr &pkt)
Assigns incoming packet to zero or more classes.
Definition: dhcp4_srv.cc:565
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:212
boost::shared_ptr< AllocEngine > AllocEnginePtr
A pointer to the AllocEngine object.
asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service used by the server.
Definition: dhcp4_srv.h:304
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:712
volatile bool shutdown_
Indicates if shutdown is in progress.
Definition: dhcp4_srv.h:1125
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:278
boost::shared_ptr< ClientContext4 > ClientContext4Ptr
Pointer to the ClientContext4.
boost::shared_ptr< Dhcpv4Exchange > Dhcpv4ExchangePtr
Type representing the pointer to the Dhcpv4Exchange.
Definition: dhcp4_srv.h:238
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
static void classifyByVendor(const Pkt4Ptr &pkt)
Assign class using vendor-class-identifier option.
Definition: dhcp4_srv.cc:553
static void removeDependentEvaluatedClasses(const Pkt4Ptr &query)
Removed evaluated client classes.
Definition: dhcp4_srv.cc:491
Convenience container for conveying DDNS behavioral parameters It is intended to be created per Packe...
Definition: srv_config.h:47
boost::shared_ptr< AllocEngine > alloc_engine_
Allocation Engine.
Definition: dhcp4_srv.h:1131
Defines the logger used by the top-level component of kea-lfc.
NetworkStatePtr network_state_
Holds information about disabled DHCP service and/or disabled subnet/network scopes.
Definition: dhcp4_srv.h:1138
bool getSendResponsesToSource() const
Returns value of the test_send_responses_to_source_ flag.
Definition: dhcp4_srv.h:464
RequirementLevel
defines if certain option may, must or must not appear
Definition: dhcp4_srv.h:262
bool useBroadcast() const
Return bool value indicating that broadcast flags should be set on sockets.
Definition: dhcp4_srv.h:419
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Pkt4Ptr getQuery() const
Returns the pointer to the query from the client.
Definition: dhcp4_srv.h:96
static const std::string VENDOR_CLASS_PREFIX
this is a prefix added to the content of vendor-class option
Definition: dhcp4_srv.h:822
void initResponse4o6()
Initializes the DHCPv6 part of the response message.
Definition: dhcp4_srv.cc:296
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
bool use_bcast_
Should broadcast be enabled on sockets (if true).
Definition: dhcp4_srv.h:1134
void deleteResponse()
Removes the response message by resetting the pointer to NULL.
Definition: dhcp4_srv.h:108
Base class for all services.
Definition: daemon.h:48
CfgOptionList & getCfgOptionList()
Returns the configured option list (non-const version)
Definition: dhcp4_srv.h:118
void conditionallySetReservedClientClasses()
Assigns classes retrieved from host reservation database if they haven&#39;t been yet set...
Definition: dhcp4_srv.cc:517
boost::shared_ptr< CBControlDHCPv4 > CBControlDHCPv4Ptr
Definition: cb_ctl_dhcp4.h:45
void setSendResponsesToSource(bool value)
Sets value of the test_send_responses_to_source_ flag.
Definition: dhcp4_srv.h:810
void setReservedMessageFields()
Sets reserved values of siaddr, sname and file in the server&#39;s response.
Definition: dhcp4_srv.cc:531
DHCPv4 message exchange.
Definition: dhcp4_srv.h:62
CBControlDHCPv4Ptr getCBControl() const
Returns an object which controls access to the configuration backends.
Definition: dhcp4_srv.h:318
static void evaluateClasses(const Pkt4Ptr &pkt, bool depend_on_known)
Evaluate classes.
Definition: dhcp4_srv.cc:576
static void setReservedClientClasses(AllocEngine::ClientContext4Ptr context)
Assigns classes retrieved from host reservation database.
Definition: dhcp4_srv.cc:506
uint16_t client_port_
UDP port number to which server sends all responses.
Definition: dhcp4_srv.h:1121
NetworkStatePtr & getNetworkState()
Returns pointer to the network state used by the server.
Definition: dhcp4_srv.h:309
This file provides the classes needed to embody, compose, and decompose DNS update requests that are ...
Defines the D2ClientMgr class.
Pkt4Ptr getResponse() const
Returns the pointer to the server&#39;s response.
Definition: dhcp4_srv.h:103
Dhcpv4Exchange(const AllocEnginePtr &alloc_engine, const Pkt4Ptr &query, AllocEngine::ClientContext4Ptr &context, const Subnet4Ptr &subnet, bool &drop)
Constructor.
Definition: dhcp4_srv.cc:151
AllocEngine::ClientContext4Ptr getContext() const
Returns the copy of the context for the Allocation engine.
Definition: dhcp4_srv.h:113
const CfgOptionList & getCfgOptionList() const
Returns the configured option list (const version)
Definition: dhcp4_srv.h:123