Kea  1.5.0
dhcp4_srv.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
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/pkt4.h>
13 #include <dhcp/option.h>
14 #include <dhcp/option_string.h>
16 #include <dhcp/option_custom.h>
17 #include <dhcp_ddns/ncr_msg.h>
18 #include <dhcpsrv/alloc_engine.h>
19 #include <dhcpsrv/cfg_option.h>
21 #include <dhcpsrv/d2_client_mgr.h>
22 #include <dhcpsrv/network_state.h>
23 #include <dhcpsrv/subnet.h>
24 #include <hooks/callout_handle.h>
25 #include <process/daemon.h>
26 
27 #include <boost/noncopyable.hpp>
28 
29 #include <functional>
30 #include <iostream>
31 #include <queue>
32 
33 // Undefine the macro OPTIONAL which is defined in some operating
34 // systems but conflicts with a member of the RequirementLevel enum in
35 // the server class.
36 
37 #ifdef OPTIONAL
38 #undef OPTIONAL
39 #endif
40 
41 namespace isc {
42 namespace dhcp {
43 
64 public:
77  Dhcpv4Exchange(const AllocEnginePtr& alloc_engine, const Pkt4Ptr& query,
78  const Subnet4Ptr& subnet);
79 
86  void initResponse();
87 
91  void initResponse4o6();
92 
94  Pkt4Ptr getQuery() const {
95  return (query_);
96  }
97 
102  return (resp_);
103  }
104 
106  void deleteResponse() {
107  resp_.reset();
108  }
109 
112  return (context_);
113  }
114 
117  return (cfg_option_list_);
118  }
119 
122  return (cfg_option_list_);
123  }
124 
128 
131 
132 private:
133 
143  void copyDefaultFields();
144 
154  void copyDefaultOptions();
155 
162  void setHostIdentifiers();
163 
165  AllocEnginePtr alloc_engine_;
167  Pkt4Ptr query_;
169  Pkt4Ptr resp_;
175  CfgOptionList cfg_option_list_;
176 };
177 
179 typedef boost::shared_ptr<Dhcpv4Exchange> Dhcpv4ExchangePtr;
180 
181 
194 class Dhcpv4Srv : public process::Daemon {
195 private:
196 
198  asiolink::IOServicePtr io_service_;
199 
200 public:
201 
203  typedef enum {
208 
226  Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
227  const bool use_bcast = true,
228  const bool direct_response_desired = true);
229 
231  virtual ~Dhcpv4Srv();
232 
235  return (io_service_);
236  }
237 
240  return (network_state_);
241  }
242 
245  static std::string getVersion(bool extended);
246 
253  bool run();
254 
260  void run_one();
261 
270  void processPacket(Pkt4Ptr& query, Pkt4Ptr& rsp,
271  bool allow_packet_park = true);
272 
273 
275  void shutdown();
276 
280 
281  uint16_t getPort() const {
289  return (port_);
290  }
291 
296  bool useBroadcast() const {
297  return (use_bcast_);
298  }
300 
306  void startD2();
307 
313  void stopD2();
314 
329  virtual void d2ClientErrorHandler(const dhcp_ddns::
330  NameChangeSender::Result result,
332 
334  void discardPackets();
335 
336 protected:
337 
344 
345  bool accept(const Pkt4Ptr& query) const;
366 
394  bool acceptDirectRequest(const Pkt4Ptr& query) const;
395 
414  bool acceptMessageType(const Pkt4Ptr& query) const;
415 
429  bool acceptServerId(const Pkt4Ptr& pkt) const;
431 
440  static void sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid);
441 
451  Pkt4Ptr processDiscover(Pkt4Ptr& discover);
452 
468 
477  void processRelease(Pkt4Ptr& release, AllocEngine::ClientContext4Ptr& context);
478 
488  void processDecline(Pkt4Ptr& decline, AllocEngine::ClientContext4Ptr& context);
489 
495  Pkt4Ptr processInform(Pkt4Ptr& inform);
496 
504 
513 
525 
540  void assignLease(Dhcpv4Exchange& ex);
541 
554 
566  void setFixedFields(Dhcpv4Exchange& ex);
567 
606 
613  static const std::string VENDOR_CLASS_PREFIX;
614 
615 private:
626  void processClientFqdnOption(Dhcpv4Exchange& ex);
627 
659  void processHostnameOption(Dhcpv4Exchange& ex);
660 
674  void declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline,
676 
677 protected:
678 
693  void createNameChangeRequests(const Lease4Ptr& lease,
694  const Lease4Ptr& old_lease);
695 
704  void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
705 
724  static void appendServerID(Dhcpv4Exchange& ex);
725 
730  static uint16_t checkRelayPort(const Dhcpv4Exchange& ex);
731 
765  static void adjustIfaceData(Dhcpv4Exchange& ex);
766 
785  static void adjustRemoteAddr(Dhcpv4Exchange& ex);
786 
793  static std::string srvidToString(const OptionPtr& opt);
794 
807  bool& drop,
808  bool sanity_only = false) const;
809 
822  bool& drop,
823  bool sanity_only = false) const;
824 
827  volatile bool shutdown_;
828 
833  virtual Pkt4Ptr receivePacket(int timeout);
834 
839  virtual void sendPacket(const Pkt4Ptr& pkt);
840 
851  void classifyPacket(const Pkt4Ptr& pkt);
852 
853 public:
854 
865  static void evaluateClasses(const Pkt4Ptr& pkt, bool depend_on_known);
866 
867 protected:
868 
880 
890  void deferredUnpack(Pkt4Ptr& query);
891 
897  void processPacketPktSend(hooks::CalloutHandlePtr& callout_handle,
898  Pkt4Ptr& query, Pkt4Ptr& rsp);
899 
905  Pkt4Ptr& rsp);
906 
911  boost::shared_ptr<AllocEngine> alloc_engine_;
912 
913 private:
914 
921  void classifyByVendor(const Pkt4Ptr& pkt);
922 
928  static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
929 
930  uint16_t port_;
931  bool use_bcast_;
932 
933 protected:
934 
938 
939 public:
941 
944  static void processStatsReceived(const Pkt4Ptr& query);
945 
948  static void processStatsSent(const Pkt4Ptr& response);
949 
952  static int getHookIndexBuffer4Receive();
953 
956  static int getHookIndexPkt4Receive();
957 
960  static int getHookIndexSubnet4Select();
961 
964  static int getHookIndexLease4Release();
965 
968  static int getHookIndexPkt4Send();
969 
972  static int getHookIndexBuffer4Send();
973 
976  static int getHookIndexLease4Decline();
977 };
978 
979 }; // namespace isc::dhcp
980 }; // namespace isc
981 
982 #endif // DHCP4_SRV_H
DHCPv4 server service.
Definition: dhcp4_srv.h:194
void setReservedClientClasses()
Assigns classes retrieved from host reservation database.
Definition: dhcp4_srv.cc:409
void run_one()
Main server processing step.
Definition: dhcp4_srv.cc:753
static int getHookIndexBuffer4Send()
Returns the index for "buffer4_send" hook point.
Definition: dhcp4_srv.cc:3623
uint16_t getPort() const
Get UDP port on which server should listen.
Definition: dhcp4_srv.h:288
void appendRequestedVendorOptions(Dhcpv4Exchange &ex)
Appends requested vendor options as requested by client.
Definition: dhcp4_srv.cc:1452
virtual ~Dhcpv4Srv()
Destructor. Used during DHCPv4 service shutdown.
Definition: dhcp4_srv.cc:482
void processPacketPktSend(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &query, Pkt4Ptr &rsp)
Executes pkt4_send callout.
Definition: dhcp4_srv.cc:1132
void processPacketBufferSend(hooks::CalloutHandlePtr &callout_handle, Pkt4Ptr &rsp)
Executes buffer4_send callout and sends the response.
Definition: dhcp4_srv.cc:1190
bool acceptMessageType(const Pkt4Ptr &query) const
Check if received message type is valid for the server to process.
Definition: dhcp4_srv.cc:3049
static int getHookIndexBuffer4Receive()
Returns the index for "buffer4_receive" hook point.
Definition: dhcp4_srv.cc:3603
bool acceptDirectRequest(const Pkt4Ptr &query) const
Check if a message sent by directly connected client should be accepted or discarded.
Definition: dhcp4_srv.cc:3011
void buildCfgOptionList(Dhcpv4Exchange &ex)
Build the configured option list.
Definition: dhcp4_srv.cc:1308
void shutdown()
Instructs the server to shut down.
Definition: dhcp4_srv.cc:511
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:464
void initResponse()
Initializes the instance of the response message.
Definition: dhcp4_srv.cc:193
static void adjustRemoteAddr(Dhcpv4Exchange &ex)
Sets remote addresses for outgoing packet.
Definition: dhcp4_srv.cc:2348
void classifyPacket(const Pkt4Ptr &pkt)
Assigns incoming packet to zero or more classes.
Definition: dhcp4_srv.cc:3255
virtual void sendPacket(const Pkt4Ptr &pkt)
dummy wrapper around IfaceMgr::send()
Definition: dhcp4_srv.cc:726
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:212
void discardPackets()
Discard all in-progress packets.
Definition: dhcp4_srv.cc:3631
boost::shared_ptr< AllocEngine > AllocEnginePtr
A pointer to the AllocEngine object.
static int getHookIndexLease4Release()
Returns the index for "lease4_release" hook point.
Definition: dhcp4_srv.cc:3615
void processRelease(Pkt4Ptr &release, AllocEngine::ClientContext4Ptr &context)
Processes incoming DHCPRELEASE messages.
Definition: dhcp4_srv.cc:2669
void setFixedFields(Dhcpv4Exchange &ex)
Sets fixed fields of the outgoing packet.
Definition: dhcp4_srv.cc:2445
Dhcpv4Srv(uint16_t port=DHCP4_SERVER_PORT, const bool use_bcast=true, const bool direct_response_desired=true)
Default constructor.
Definition: dhcp4_srv.cc:444
asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service used by the server.
Definition: dhcp4_srv.h:234
virtual void d2ClientErrorHandler(const dhcp_ddns::NameChangeSender::Result result, dhcp_ddns::NameChangeRequestPtr &ncr)
Implements the error handler for DHCP_DDNS IO errors.
Definition: dhcp4_srv.cc:3486
void processDecline(Pkt4Ptr &decline, AllocEngine::ClientContext4Ptr &context)
Process incoming DHCPDECLINE messages.
Definition: dhcp4_srv.cc:2784
bool acceptServerId(const Pkt4Ptr &pkt) const
Verifies if the server id belongs to our server.
Definition: dhcp4_srv.cc:3105
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:503
volatile bool shutdown_
indicates if shutdown is in progress.
Definition: dhcp4_srv.h:827
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:245
static void sanityCheck(const Pkt4Ptr &query, RequirementLevel serverid)
Verifies if specified packet meets RFC requirements.
Definition: dhcp4_srv.cc:3202
static void appendServerID(Dhcpv4Exchange &ex)
Adds server identifier option to the server's response.
Definition: dhcp4_srv.cc:1283
Pkt4Ptr processRequest(Pkt4Ptr &request, AllocEngine::ClientContext4Ptr &context)
Processes incoming REQUEST and returns REPLY response.
Definition: dhcp4_srv.cc:2607
void processPacket(Pkt4Ptr &query, Pkt4Ptr &rsp, bool allow_packet_park=true)
Process a single incoming DHCPv4 packet.
Definition: dhcp4_srv.cc:844
void classifyByVendor(const Pkt4Ptr &pkt)
Assign class using vendor-class-identifier option.
Definition: dhcp4_srv.cc:3243
void appendRequestedOptions(Dhcpv4Exchange &ex)
Appends options requested by client.
Definition: dhcp4_srv.cc:1381
boost::shared_ptr< ClientContext4 > ClientContext4Ptr
Pointer to the ClientContext4.
static int getHookIndexLease4Decline()
Returns the index for "lease4_decline" hook point.
Definition: dhcp4_srv.cc:3627
static int getHookIndexSubnet4Select()
Returns the index for "subnet4_select" hook point.
Definition: dhcp4_srv.cc:3611
static int getHookIndexPkt4Send()
Returns the index for "pkt4_send" hook point.
Definition: dhcp4_srv.cc:3619
bool run()
Main server processing loop.
Definition: dhcp4_srv.cc:731
Pkt4Ptr processInform(Pkt4Ptr &inform)
Processes incoming DHCPINFORM messages.
Definition: dhcp4_srv.cc:2934
void stopD2()
Stops DHCP_DDNS client IO if DDNS updates are enabled.
Definition: dhcp4_srv.cc:3477
boost::shared_ptr< Dhcpv4Exchange > Dhcpv4ExchangePtr
Type representing the pointer to the Dhcpv4Exchange.
Definition: dhcp4_srv.h:179
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
virtual Pkt4Ptr receivePacket(int timeout)
dummy wrapper around IfaceMgr::receive4
Definition: dhcp4_srv.cc:721
static void processStatsSent(const Pkt4Ptr &response)
Updates statistics for transmitted packets.
Definition: dhcp4_srv.cc:3577
boost::shared_ptr< AllocEngine > alloc_engine_
Allocation Engine.
Definition: dhcp4_srv.h:911
Defines the logger used by the top-level component of kea-dhcp-ddns.
NetworkStatePtr network_state_
Holds information about disabled DHCP service and/or disabled subnet/network scopes.
Definition: dhcp4_srv.h:937
void startD2()
Starts DHCP_DDNS client IO if DDNS updates are enabled.
Definition: dhcp4_srv.cc:3465
void declineLease(const Lease4Ptr &lease, const Pkt4Ptr &decline, AllocEngine::ClientContext4Ptr &context)
Marks lease as declined.
Definition: dhcp4_srv.cc:2858
void createNameChangeRequests(const Lease4Ptr &lease, const Lease4Ptr &old_lease)
Creates NameChangeRequests which correspond to the lease which has been acquired.
Definition: dhcp4_srv.cc:1844
RequirementLevel
defines if certain option may, must or must not appear
Definition: dhcp4_srv.h:203
isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr &query, bool &drop, bool sanity_only=false) const
Selects a subnet for a given client's packet.
Definition: dhcp4_srv.cc:517
bool useBroadcast() const
Return bool value indicating that broadcast flags should be set on sockets.
Definition: dhcp4_srv.h:296
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
static std::string srvidToString(const OptionPtr &opt)
converts server-id to text Converts content of server-id option to a text representation,...
Definition: dhcp4_srv.cc:1263
Dhcpv4Exchange(const AllocEnginePtr &alloc_engine, const Pkt4Ptr &query, const Subnet4Ptr &subnet)
Constructor.
Definition: dhcp4_srv.cc:123
void requiredClassify(Dhcpv4Exchange &ex)
Assigns incoming packet to zero or more classes (required pass).
Definition: dhcp4_srv.cc:3314
Pkt4Ptr getQuery() const
Returns the pointer to the query from the client.
Definition: dhcp4_srv.h:94
void assignLease(Dhcpv4Exchange &ex)
Assigns a lease and appends corresponding options.
Definition: dhcp4_srv.cc:1864
static const std::string VENDOR_CLASS_PREFIX
this is a prefix added to the content of vendor-class option
Definition: dhcp4_srv.h:613
isc::dhcp::Subnet4Ptr selectSubnet4o6(const Pkt4Ptr &query, bool &drop, bool sanity_only=false) const
Selects a subnet for a given client's DHCP4o6 packet.
Definition: dhcp4_srv.cc:603
bool accept(const Pkt4Ptr &query) const
Checks whether received message should be processed or discarded.
Definition: dhcp4_srv.cc:2983
void initResponse4o6()
Initializes the DHCPv6 part of the response message.
Definition: dhcp4_srv.cc:219
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
void deleteResponse()
Removes the response message by resetting the pointer to NULL.
Definition: dhcp4_srv.h:106
Base class for all services.
Definition: daemon.h:49
void appendBasicOptions(Dhcpv4Exchange &ex)
Append basic options if they are not present.
Definition: dhcp4_srv.cc:1540
void processClientName(Dhcpv4Exchange &ex)
Processes Client FQDN and Hostname Options sent by a client.
Definition: dhcp4_srv.cc:1586
CfgOptionList & getCfgOptionList()
Returns the configured option list (non-const version)
Definition: dhcp4_srv.h:116
void renewLease(const Pkt4Ptr &renew, Pkt4Ptr &reply)
Attempts to renew received addresses.
static uint16_t checkRelayPort(const Dhcpv4Exchange &ex)
Check if the relay port RAI sub-option was set in the query.
Definition: dhcp4_srv.cc:2254
void setReservedMessageFields()
Sets reserved values of siaddr, sname and file in the server's response.
Definition: dhcp4_srv.cc:420
DHCPv4 message exchange.
Definition: dhcp4_srv.h:63
void deferredUnpack(Pkt4Ptr &query)
Perform deferred option unpacking.
Definition: dhcp4_srv.cc:3406
static std::string getVersion(bool extended)
returns Kea version on stdout and exit.
Definition: dhcp4_srv.cc:3501
NetworkStatePtr & getNetworkState()
Returns pointer to the network state used by the server.
Definition: dhcp4_srv.h:239
static void adjustIfaceData(Dhcpv4Exchange &ex)
Set IP/UDP and interface parameters for the DHCPv4 response.
Definition: dhcp4_srv.cc:2267
static void processStatsReceived(const Pkt4Ptr &query)
Class methods for DHCPv4-over-DHCPv6 handler.
Definition: dhcp4_srv.cc:3528
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's response.
Definition: dhcp4_srv.h:101
Pkt4Ptr processDiscover(Pkt4Ptr &discover)
Processes incoming DISCOVER and returns response.
Definition: dhcp4_srv.cc:2548
static void evaluateClasses(const Pkt4Ptr &pkt, bool depend_on_known)
Evaluate classes.
Definition: dhcp4_srv.cc:3266
static int getHookIndexPkt4Receive()
Returns the index for "pkt4_receive" hook point.
Definition: dhcp4_srv.cc:3607
AllocEngine::ClientContext4Ptr getContext() const
Returns the copy of the context for the Allocation engine.
Definition: dhcp4_srv.h:111
const CfgOptionList & getCfgOptionList() const
Returns the configured option list (const version)
Definition: dhcp4_srv.h:121