dhcp4_srv.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // Copyright (C) 2011-2016 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. #ifndef DHCPV4_SRV_H
  7. #define DHCPV4_SRV_H
  8. #include <dhcp/dhcp4.h>
  9. #include <dhcp/pkt4.h>
  10. #include <dhcp/pkt4o6.h>
  11. #include <dhcp/pkt6.h>
  12. #include <dhcp/option.h>
  13. #include <dhcp/option_string.h>
  14. #include <dhcp/option4_client_fqdn.h>
  15. #include <dhcp/option_custom.h>
  16. #include <dhcp_ddns/ncr_msg.h>
  17. #include <dhcpsrv/d2_client_mgr.h>
  18. #include <dhcpsrv/subnet.h>
  19. #include <dhcpsrv/alloc_engine.h>
  20. #include <dhcpsrv/cfg_option.h>
  21. #include <hooks/callout_handle.h>
  22. #include <dhcpsrv/daemon.h>
  23. #include <boost/noncopyable.hpp>
  24. #include <iostream>
  25. #include <queue>
  26. // Undefine the macro OPTIONAL which is defined in some operating
  27. // systems but conflicts with a member of the RequirementLevel enum in
  28. // the server class.
  29. #ifdef OPTIONAL
  30. #undef OPTIONAL
  31. #endif
  32. namespace isc {
  33. namespace dhcp {
  34. /// @brief DHCPv4 message exchange.
  35. ///
  36. /// This class represents the DHCPv4 message exchange. The message exchange
  37. /// consists of the single client message, server response to this message
  38. /// and the mechanisms to generate the server's response. The server creates
  39. /// the instance of the @c Dhcpv4Exchange for each inbound message that it
  40. /// accepts for processing.
  41. ///
  42. /// The use of the @c Dhcpv4Exchange object as a central repository of
  43. /// information about the message exchange simplifies the API of the
  44. /// @c Dhcpv4Srv class.
  45. ///
  46. /// Another benefit of using this class is that different methods of the
  47. /// @c Dhcpv4Srv may share information. For example, the constructor of this
  48. /// class selects the subnet and multiple methods of @c Dhcpv4Srv use this
  49. /// subnet, without the need to select it again.
  50. ///
  51. /// @todo This is the initial version of this class. In the future a lot of
  52. /// code from the @c Dhcpv4Srv class will be migrated here.
  53. class Dhcpv4Exchange {
  54. public:
  55. /// @brief Constructor.
  56. ///
  57. /// The constructor selects the subnet for the query and checks for the
  58. /// static host reservations for the client which has sent the message.
  59. /// The information about the reservations is stored in the
  60. /// @c AllocEngine::ClientContext4 object, which can be obtained by
  61. /// calling the @c getContext.
  62. ///
  63. /// @param alloc_engine Pointer to the instance of the Allocation Engine
  64. /// used by the server.
  65. /// @param query Pointer to the client message.
  66. /// @param subnet Pointer to the subnet to which the client belongs.
  67. Dhcpv4Exchange(const AllocEnginePtr& alloc_engine, const Pkt4Ptr& query,
  68. const Subnet4Ptr& subnet);
  69. /// @brief Initializes the instance of the response message.
  70. ///
  71. /// The type of the response depends on the type of the query message.
  72. /// For the DHCPDISCOVER the DHCPOFFER is created. For the DHCPREQUEST
  73. /// and DHCPINFORM the DHCPACK is created. For the DHCPRELEASE the
  74. /// response is not initialized.
  75. void initResponse();
  76. /// @brief Initializes the DHCPv6 part of the response message
  77. ///
  78. /// Called by initResponse() when the query is a DHCP4o6 message
  79. void initResponse4o6();
  80. /// @brief Returns the pointer to the query from the client.
  81. Pkt4Ptr getQuery() const {
  82. return (query_);
  83. }
  84. /// @brief Returns the pointer to the server's response.
  85. ///
  86. /// The returned pointer is NULL if the query type is DHCPRELEASE or DHCPDECLINE.
  87. Pkt4Ptr getResponse() const {
  88. return (resp_);
  89. }
  90. /// @brief Removes the response message by resetting the pointer to NULL.
  91. void deleteResponse() {
  92. resp_.reset();
  93. }
  94. /// @brief Returns the copy of the context for the Allocation engine.
  95. AllocEngine::ClientContext4Ptr getContext() const {
  96. return (context_);
  97. }
  98. /// @brief Returns the configured option list (non-const version)
  99. CfgOptionList& getCfgOptionList() {
  100. return (cfg_option_list_);
  101. }
  102. /// @brief Returns the configured option list (const version)
  103. const CfgOptionList& getCfgOptionList() const {
  104. return (cfg_option_list_);
  105. }
  106. private:
  107. /// @brief Copies default parameters from client's to server's message
  108. ///
  109. /// Some fields are copied from client's message into server's response,
  110. /// e.g. client HW address, number of hops, transaction-id etc.
  111. ///
  112. /// @warning This message is called internally by @c initResponse and
  113. /// thus it doesn't check if the resp_ value has been initialized. The
  114. /// calling method is responsible for making sure that @c resp_ is
  115. /// not NULL.
  116. void copyDefaultFields();
  117. /// @brief Copies default options from client's to server's message
  118. ///
  119. /// Some options are copied from client's message into server's response,
  120. /// e.g. Relay Agent Info option, Subnet Selection option etc.
  121. ///
  122. /// @warning This message is called internally by @c initResponse and
  123. /// thus it doesn't check if the resp_ value has been initialized. The
  124. /// calling method is responsible for making sure that @c resp_ is
  125. /// not NULL.
  126. void copyDefaultOptions();
  127. /// @brief Set host identifiers within a context.
  128. ///
  129. /// This method sets an ordered list of host identifier types and
  130. /// values which the server should use to find host reservations.
  131. /// The order of the set is determined by the configuration parameter,
  132. /// host-reservation-identifiers
  133. void setHostIdentifiers();
  134. /// @brief Pointer to the allocation engine used by the server.
  135. AllocEnginePtr alloc_engine_;
  136. /// @brief Pointer to the DHCPv4 message sent by the client.
  137. Pkt4Ptr query_;
  138. /// @brief Pointer to the DHCPv4 message to be sent to the client.
  139. Pkt4Ptr resp_;
  140. /// @brief Context for use with allocation engine.
  141. AllocEngine::ClientContext4Ptr context_;
  142. /// @brief Configured option list.
  143. /// @note The configured option list is an *ordered* list of
  144. /// @c CfgOption objects used to append options to the response.
  145. CfgOptionList cfg_option_list_;
  146. };
  147. /// @brief Type representing the pointer to the @c Dhcpv4Exchange.
  148. typedef boost::shared_ptr<Dhcpv4Exchange> Dhcpv4ExchangePtr;
  149. /// @brief DHCPv4 server service.
  150. ///
  151. /// This singleton class represents DHCPv4 server. It contains all
  152. /// top-level methods and routines necessary for server operation.
  153. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  154. /// that is going to be used as server-identifier, receives incoming
  155. /// packets, processes them, manages leases assignment and generates
  156. /// appropriate responses.
  157. ///
  158. /// This class does not support any controlling mechanisms directly.
  159. /// See the derived \ref ControlledDhcpv4Srv class for support for
  160. /// command and configuration updates over msgq.
  161. class Dhcpv4Srv : public Daemon {
  162. public:
  163. /// @brief defines if certain option may, must or must not appear
  164. typedef enum {
  165. FORBIDDEN,
  166. MANDATORY,
  167. OPTIONAL
  168. } RequirementLevel;
  169. /// @brief Default constructor.
  170. ///
  171. /// Instantiates necessary services, required to run DHCPv4 server.
  172. /// In particular, creates IfaceMgr that will be responsible for
  173. /// network interaction. Will instantiate lease manager, and load
  174. /// old or create new DUID. It is possible to specify alternate
  175. /// port on which DHCPv4 server will listen on. That is mostly useful
  176. /// for testing purposes. The Last two arguments of the constructor
  177. /// should be left at default values for normal server operation.
  178. /// They should be set to 'false' when creating an instance of this
  179. /// class for unit testing because features they enable require
  180. /// root privileges.
  181. ///
  182. /// @param port specifies port number to listen on
  183. /// @param use_bcast configure sockets to support broadcast messages.
  184. /// @param direct_response_desired specifies if it is desired to
  185. /// use direct V4 traffic.
  186. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
  187. const bool use_bcast = true,
  188. const bool direct_response_desired = true);
  189. /// @brief Destructor. Used during DHCPv4 service shutdown.
  190. virtual ~Dhcpv4Srv();
  191. /// @brief returns Kea version on stdout and exit.
  192. /// redeclaration/redefinition. @ref Daemon::getVersion()
  193. static std::string getVersion(bool extended);
  194. /// @brief Main server processing loop.
  195. ///
  196. /// Main server processing loop. Call the processing step routine
  197. /// until shut down.
  198. ///
  199. /// @return true, if being shut down gracefully, never fail.
  200. bool run();
  201. /// @brief Main server processing step.
  202. ///
  203. /// Main server processing step. Receives one incoming packet, calls
  204. /// the processing packet routing and (if necessary) transmits
  205. /// a response.
  206. void run_one();
  207. /// @brief Process a single incoming DHCPv4 packet.
  208. ///
  209. /// It verifies correctness of the passed packet, call per-type processXXX
  210. /// methods, generates appropriate answer.
  211. ///
  212. /// @param query A pointer to the packet to be processed.
  213. /// @param rsp A pointer to the response
  214. void processPacket(Pkt4Ptr& query, Pkt4Ptr& rsp);
  215. /// @brief Instructs the server to shut down.
  216. void shutdown();
  217. ///
  218. /// @name Public accessors returning values required to (re)open sockets.
  219. ///
  220. //@{
  221. ///
  222. /// @brief Get UDP port on which server should listen.
  223. ///
  224. /// Typically, server listens on UDP port number 67. Other ports are used
  225. /// for testing purposes only.
  226. ///
  227. /// @return UDP port on which server should listen.
  228. uint16_t getPort() const {
  229. return (port_);
  230. }
  231. /// @brief Return bool value indicating that broadcast flags should be set
  232. /// on sockets.
  233. ///
  234. /// @return A bool value indicating that broadcast should be used (if true).
  235. bool useBroadcast() const {
  236. return (use_bcast_);
  237. }
  238. //@}
  239. /// @brief Starts DHCP_DDNS client IO if DDNS updates are enabled.
  240. ///
  241. /// If updates are enabled, it instructs the D2ClientMgr singleton to
  242. /// enter send mode. If D2ClientMgr encounters errors it may throw
  243. /// D2ClientError. This method does not catch exceptions.
  244. void startD2();
  245. /// @brief Stops DHCP_DDNS client IO if DDNS updates are enabled.
  246. ///
  247. /// If updates are enabled, it instructs the D2ClientMgr singleton to
  248. /// leave send mode. If D2ClientMgr encounters errors it may throw
  249. /// D2ClientError. This method does not catch exceptions.
  250. void stopD2();
  251. /// @brief Implements the error handler for DHCP_DDNS IO errors
  252. ///
  253. /// Invoked when a NameChangeRequest send to kea-dhcp-ddns completes with
  254. /// a failed status. These are communications errors, not data related
  255. /// failures.
  256. ///
  257. /// This method logs the failure and then suspends all further updates.
  258. /// Updating can only be restored by reconfiguration or restarting the
  259. /// server. There is currently no retry logic so the first IO error that
  260. /// occurs will suspend updates.
  261. /// @todo We may wish to make this more robust or sophisticated.
  262. ///
  263. /// @param result Result code of the send operation.
  264. /// @param ncr NameChangeRequest which failed to send.
  265. virtual void d2ClientErrorHandler(const dhcp_ddns::
  266. NameChangeSender::Result result,
  267. dhcp_ddns::NameChangeRequestPtr& ncr);
  268. protected:
  269. /// @name Functions filtering and sanity-checking received messages.
  270. ///
  271. /// @todo These functions are supposed to be moved to a new class which
  272. /// will manage different rules for accepting and rejecting messages.
  273. /// Perhaps ticket #3116 is a good opportunity to do it.
  274. ///
  275. //@{
  276. /// @brief Checks whether received message should be processed or discarded.
  277. ///
  278. /// This function checks whether received message should be processed or
  279. /// discarded. It should be called on the beginning of message processing
  280. /// (just after the message has been decoded). This message calls a number
  281. /// of other functions which check whether message should be processed,
  282. /// using different criteria.
  283. ///
  284. /// This function should be extended when new criteria for accepting
  285. /// received message have to be implemented. This function is meant to
  286. /// aggregate all early filtering checks on the received message. By having
  287. /// a single function like this, we are avoiding bloat of the server's main
  288. /// loop.
  289. ///
  290. /// @warning This function should remain exception safe.
  291. ///
  292. /// @param query Received message.
  293. ///
  294. /// @return true if the message should be further processed, or false if
  295. /// the message should be discarded.
  296. bool accept(const Pkt4Ptr& query) const;
  297. /// @brief Check if a message sent by directly connected client should be
  298. /// accepted or discarded.
  299. ///
  300. /// This function checks if the received message is from directly connected
  301. /// client. If it is, it checks that it should be processed or discarded.
  302. ///
  303. /// Note that this function doesn't validate all addresses being carried in
  304. /// the message. The primary purpose of this function is to filter out
  305. /// direct messages in the local network for which there is no suitable
  306. /// subnet configured. For example, this function accepts unicast messages
  307. /// because unicasts may be used by clients located in remote networks to
  308. /// to renew existing leases. If their notion of address is wrong, the
  309. /// server will have to sent a NAK, instead of dropping the message.
  310. /// Detailed validation of such messages is performed at later stage of
  311. /// processing.
  312. ///
  313. /// This function accepts the following messages:
  314. /// - all valid relayed messages,
  315. /// - all unicast messages,
  316. /// - all broadcast messages except DHCPINFORM received on the interface
  317. /// for which the suitable subnet exists (is configured).
  318. /// - all DHCPINFORM messages with source address or ciaddr set.
  319. ///
  320. /// @param query Message sent by a client.
  321. ///
  322. /// @return true if message is accepted for further processing, false
  323. /// otherwise.
  324. bool acceptDirectRequest(const Pkt4Ptr& query) const;
  325. /// @brief Check if received message type is valid for the server to
  326. /// process.
  327. ///
  328. /// This function checks that the received message type belongs to
  329. /// the range of types recognized by the server and that the
  330. /// message of this type should be processed by the server.
  331. ///
  332. /// The messages types accepted for processing are:
  333. /// - Discover
  334. /// - Request
  335. /// - Release
  336. /// - Decline
  337. /// - Inform
  338. ///
  339. /// @param query Message sent by a client.
  340. ///
  341. /// @return true if message is accepted for further processing, false
  342. /// otherwise.
  343. bool acceptMessageType(const Pkt4Ptr& query) const;
  344. /// @brief Verifies if the server id belongs to our server.
  345. ///
  346. /// This function checks if the server identifier carried in the specified
  347. /// DHCPv4 message belongs to this server. If the server identifier option
  348. /// is absent or the value carried by this option is equal to one of the
  349. /// server identifiers used by the server, the true is returned. If the
  350. /// server identifier option is present, but it doesn't match any server
  351. /// identifier used by this server, the false value is returned.
  352. ///
  353. /// @param pkt DHCPv4 message which server identifier is to be checked.
  354. ///
  355. /// @return true, if the server identifier is absent or matches one of the
  356. /// server identifiers that the server is using; false otherwise.
  357. bool acceptServerId(const Pkt4Ptr& pkt) const;
  358. //@}
  359. /// @brief Verifies if specified packet meets RFC requirements
  360. ///
  361. /// Checks if mandatory option is really there, that forbidden option
  362. /// is not there, and that client-id or server-id appears only once.
  363. ///
  364. /// @param query Pointer to the client's message.
  365. /// @param serverid expectation regarding server-id option
  366. /// @throw RFCViolation if any issues are detected
  367. static void sanityCheck(const Pkt4Ptr& query, RequirementLevel serverid);
  368. /// @brief Processes incoming DISCOVER and returns response.
  369. ///
  370. /// Processes received DISCOVER message and verifies that its sender
  371. /// should be served. In particular, a lease is selected and sent
  372. /// as an offer to a client if it should be served.
  373. ///
  374. /// @param discover DISCOVER message received from client
  375. ///
  376. /// @return OFFER message or NULL
  377. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  378. /// @brief Processes incoming REQUEST and returns REPLY response.
  379. ///
  380. /// Processes incoming REQUEST message and verifies that its sender
  381. /// should be served. In particular, verifies that requested lease
  382. /// is valid, not expired, not reserved, not used by other client and
  383. /// that requesting client is allowed to use it.
  384. ///
  385. /// Returns ACK message, NAK message, or NULL
  386. ///
  387. /// @param request a message received from client
  388. ///
  389. /// @return ACK or NAK message
  390. Pkt4Ptr processRequest(Pkt4Ptr& request);
  391. /// @brief Processes incoming DHCPRELEASE messages.
  392. ///
  393. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  394. /// this function does not return anything.
  395. ///
  396. /// @param release message received from client
  397. void processRelease(Pkt4Ptr& release);
  398. /// @brief Process incoming DHCPDECLINE messages.
  399. ///
  400. /// This method processes incoming DHCPDECLINE. In particular, it extracts
  401. /// Requested IP Address option, checks that the address really belongs to
  402. /// the client and if it does, calls @ref declineLease.
  403. ///
  404. /// @param decline message received from client
  405. void processDecline(Pkt4Ptr& decline);
  406. /// @brief Processes incoming DHCPINFORM messages.
  407. ///
  408. /// @param inform message received from client
  409. ///
  410. /// @return DHCPACK to be sent to the client.
  411. Pkt4Ptr processInform(Pkt4Ptr& inform);
  412. /// @brief Build the configured option list
  413. ///
  414. /// @note The configured option list is an *ordered* list of
  415. /// @c CfgOption objects used to append options to the response.
  416. ///
  417. /// @param ex The exchange where the configured option list is cached
  418. void buildCfgOptionList(Dhcpv4Exchange& ex);
  419. /// @brief Appends options requested by client.
  420. ///
  421. /// This method assigns options that were requested by client
  422. /// (sent in PRL) or are enforced by server.
  423. ///
  424. /// @param ex The exchange holding both the client's message and the
  425. /// server's response.
  426. void appendRequestedOptions(Dhcpv4Exchange& ex);
  427. /// @brief Appends requested vendor options as requested by client.
  428. ///
  429. /// This method is similar to \ref appendRequestedOptions(), but uses
  430. /// vendor options. The major difference is that vendor-options use
  431. /// its own option spaces (there may be more than one distinct set of vendor
  432. /// options, each with unique vendor-id). Vendor options are requested
  433. /// using separate options within their respective vendor-option spaces.
  434. ///
  435. /// @param ex The exchange holding both the client's message and the
  436. /// server's response.
  437. void appendRequestedVendorOptions(Dhcpv4Exchange& ex);
  438. /// @brief Assigns a lease and appends corresponding options
  439. ///
  440. /// This method chooses the most appropriate lease for requesting
  441. /// client and assigning it. Options corresponding to the lease
  442. /// are added to specific message.
  443. ///
  444. /// This method may reset the pointer to the response in the @c ex object
  445. /// to indicate that the response should not be sent to the client.
  446. /// The caller must check if the response is is null after calling
  447. /// this method.
  448. ///
  449. /// The response type in the @c ex object may be set to DHCPACK or DHCPNAK.
  450. ///
  451. /// @param ex DHCPv4 exchange holding the client's message to be checked.
  452. void assignLease(Dhcpv4Exchange& ex);
  453. /// @brief Append basic options if they are not present.
  454. ///
  455. /// This function adds the following basic options if they
  456. /// are not yet added to the response message:
  457. /// - Subnet Mask,
  458. /// - Router,
  459. /// - Name Server,
  460. /// - Domain Name.
  461. ///
  462. /// @param ex DHCPv4 exchange holding the client's message to be checked.
  463. void appendBasicOptions(Dhcpv4Exchange& ex);
  464. /// @brief Processes Client FQDN and Hostname Options sent by a client.
  465. ///
  466. /// Client may send Client FQDN or Hostname option to communicate its name
  467. /// to the server. Server may use this name to perform DNS update for the
  468. /// lease being assigned to a client. If server takes responsibility for
  469. /// updating DNS for a client it may communicate it by sending the Client
  470. /// FQDN or Hostname %Option back to the client. Server select a different
  471. /// name than requested by a client to update DNS. In such case, the server
  472. /// stores this different name in its response.
  473. ///
  474. /// Client should not send both Client FQDN and Hostname options. However,
  475. /// if client sends both options, server should prefer Client FQDN option
  476. /// and ignore the Hostname option. If Client FQDN option is not present,
  477. /// the Hostname option is processed.
  478. ///
  479. /// The Client FQDN %Option is processed by this function as described in
  480. /// RFC4702.
  481. ///
  482. /// In response to a Hostname %Option sent by a client, the server may send
  483. /// Hostname option with the same or different hostname. If different
  484. /// hostname is sent, it is an indication to the client that server has
  485. /// overridden the client's preferred name and will rather use this
  486. /// different name to update DNS. However, since Hostname option doesn't
  487. /// carry an information whether DNS update will be carried by the server
  488. /// or not, the client is responsible for checking whether DNS update
  489. /// has been performed.
  490. ///
  491. /// After successful processing options stored in the first parameter,
  492. /// this function may add Client FQDN or Hostname option to the response
  493. /// message. In some cases, server may cease to add any options to the
  494. /// response, i.e. when server doesn't support DNS updates.
  495. ///
  496. /// This function does not throw. It simply logs the debug message if the
  497. /// processing of the FQDN or Hostname failed.
  498. ///
  499. /// @param ex The exchange holding both the client's message and the
  500. /// server's response.
  501. void processClientName(Dhcpv4Exchange& ex);
  502. /// @brief this is a prefix added to the contend of vendor-class option
  503. ///
  504. /// If incoming packet has a vendor class option, its content is
  505. /// prepended with this prefix and then interpreted as a class.
  506. /// For example, a packet that sends vendor class with value of "FOO"
  507. /// will cause the packet to be assigned to class VENDOR_CLASS_FOO.
  508. static const std::string VENDOR_CLASS_PREFIX;
  509. private:
  510. /// @brief Process Client FQDN %Option sent by a client.
  511. ///
  512. /// This function is called by the @c Dhcpv4Srv::processClientName when
  513. /// the client has sent the FQDN option in its message to the server.
  514. /// It comprises the actual logic to parse the FQDN option and prepare
  515. /// the FQDN option to be sent back to the client in the server's
  516. /// response.
  517. ///
  518. /// @param ex The exchange holding both the client's message and the
  519. /// server's response.
  520. void processClientFqdnOption(Dhcpv4Exchange& ex);
  521. /// @brief Process Hostname %Option sent by a client.
  522. ///
  523. /// This function is called by the @c Dhcpv4Srv::processClientName when
  524. /// the client has sent the Hostname option in its message to the server.
  525. /// It comprises the actual logic to parse the Hostname option and
  526. /// prepare the Hostname option to be sent back to the client in the
  527. /// server's response.
  528. ///
  529. /// @param ex The exchange holding both the client's message and the
  530. /// server's response.
  531. void processHostnameOption(Dhcpv4Exchange& ex);
  532. /// @public
  533. /// @brief Marks lease as declined.
  534. ///
  535. /// This method moves a lease to declined state with all the steps involved:
  536. /// - trigger DNS removal (if necessary)
  537. /// - disassociate the client information
  538. /// - update lease in the database (switch to DECLINED state)
  539. /// - increase necessary statistics
  540. /// - call lease4_decline hook
  541. ///
  542. /// @param lease lease to be declined
  543. /// @param decline client's message
  544. void declineLease(const Lease4Ptr& lease, const Pkt4Ptr& decline);
  545. protected:
  546. /// @brief Creates NameChangeRequests which correspond to the lease
  547. /// which has been acquired.
  548. ///
  549. /// If this function is called when an existing lease is renewed, it
  550. /// may generate NameChangeRequest to remove existing DNS entries which
  551. /// correspond to the old lease instance. This function may cease to
  552. /// generate NameChangeRequests if the notion of the client's FQDN hasn't
  553. /// changed between an old and new lease.
  554. ///
  555. /// @param lease A pointer to the new lease which has been acquired.
  556. /// @param old_lease A pointer to the instance of the old lease which has
  557. /// been replaced by the new lease passed in the first argument. The NULL
  558. /// value indicates that the new lease has been allocated, rather than
  559. /// lease being renewed.
  560. void createNameChangeRequests(const Lease4Ptr& lease,
  561. const Lease4Ptr& old_lease);
  562. /// @brief Attempts to renew received addresses
  563. ///
  564. /// Attempts to renew existing lease. This typically includes finding a lease that
  565. /// corresponds to the received address. If no such lease is found, a status code
  566. /// response is generated.
  567. ///
  568. /// @param renew client's message asking for renew
  569. /// @param reply server's response (ACK or NAK)
  570. void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
  571. /// @brief Adds server identifier option to the server's response.
  572. ///
  573. /// This method adds a server identifier to the DHCPv4 message. It expects
  574. /// that the local (source) address is set for this message. If address is
  575. /// not set, it will throw an exception. This method also expects that the
  576. /// server identifier option is not present in the specified message.
  577. /// Otherwise, it will throw an exception on attempt to add a duplicate
  578. /// server identifier option.
  579. ///
  580. /// @note This method doesn't throw exceptions by itself but the underlying
  581. /// classes being used my throw. The reason for this method to not sanity
  582. /// check the specified message is that it is meant to be called internally
  583. /// by the @c Dhcpv4Srv class.
  584. ///
  585. /// @note This method is static because it is not dependent on the class
  586. /// state.
  587. ///
  588. /// @param ex The exchange holding both the client's message and the
  589. /// server's response.
  590. static void appendServerID(Dhcpv4Exchange& ex);
  591. /// @brief Set IP/UDP and interface parameters for the DHCPv4 response.
  592. ///
  593. /// This method sets the following parameters for the DHCPv4 message being
  594. /// sent to a client:
  595. /// - client unicast or a broadcast address,
  596. /// - client or relay port,
  597. /// - server address,
  598. /// - server port,
  599. /// - name and index of the interface which is to be used to send the
  600. /// message.
  601. ///
  602. /// Internally it calls the @c Dhcpv4Srv::adjustRemoteAddr to figure
  603. /// out the destination address (client unicast address or broadcast
  604. /// address).
  605. ///
  606. /// The destination port is always DHCPv4 client (68) or relay (67) port,
  607. /// depending if the response will be sent directly to a client.
  608. ///
  609. /// The source port is always set to DHCPv4 server port (67).
  610. ///
  611. /// The interface selected for the response is always the same as the
  612. /// one through which the query has been received.
  613. ///
  614. /// The source address for the response is the IPv4 address assigned to
  615. /// the interface being used to send the response. This function uses
  616. /// @c IfaceMgr to get the socket bound to the IPv4 address on the
  617. /// particular interface.
  618. ///
  619. /// @note This method is static because it is not dependent on the class
  620. /// state.
  621. ///
  622. /// @param ex The exchange holding both the client's message and the
  623. /// server's response.
  624. static void adjustIfaceData(Dhcpv4Exchange& ex);
  625. /// @brief Sets remote addresses for outgoing packet.
  626. ///
  627. /// This method sets the local and remote addresses on outgoing packet.
  628. /// The addresses being set depend on the following conditions:
  629. /// - has incoming packet been relayed,
  630. /// - is direct response to a client without address supported,
  631. /// - type of the outgoing packet,
  632. /// - broadcast flag set in the incoming packet.
  633. ///
  634. /// @warning This method does not check whether provided packet pointers
  635. /// are valid. Make sure that pointers are correct before calling this
  636. /// function.
  637. ///
  638. /// @note This method is static because it is not dependent on the class
  639. /// state.
  640. ///
  641. /// @param ex The exchange holding both the client's message and the
  642. /// server's response.
  643. static void adjustRemoteAddr(Dhcpv4Exchange& ex);
  644. /// @brief converts server-id to text
  645. /// Converts content of server-id option to a text representation, e.g.
  646. /// "192.0.2.1"
  647. ///
  648. /// @param opt option that contains server-id
  649. /// @return string representation
  650. static std::string srvidToString(const OptionPtr& opt);
  651. /// @brief Selects a subnet for a given client's packet.
  652. ///
  653. /// @param query client's message
  654. /// @return selected subnet (or NULL if no suitable subnet was found)
  655. isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& query) const;
  656. /// @brief Selects a subnet for a given client's DHCP4o6 packet.
  657. ///
  658. /// @param query client's message
  659. /// @return selected subnet (or NULL if no suitable subnet was found)
  660. isc::dhcp::Subnet4Ptr selectSubnet4o6(const Pkt4Ptr& query) const;
  661. /// indicates if shutdown is in progress. Setting it to true will
  662. /// initiate server shutdown procedure.
  663. volatile bool shutdown_;
  664. /// @brief dummy wrapper around IfaceMgr::receive4
  665. ///
  666. /// This method is useful for testing purposes, where its replacement
  667. /// simulates reception of a packet. For that purpose it is protected.
  668. virtual Pkt4Ptr receivePacket(int timeout);
  669. /// @brief dummy wrapper around IfaceMgr::send()
  670. ///
  671. /// This method is useful for testing purposes, where its replacement
  672. /// simulates transmission of a packet. For that purpose it is protected.
  673. virtual void sendPacket(const Pkt4Ptr& pkt);
  674. /// @brief Assigns incoming packet to zero or more classes.
  675. ///
  676. /// @note This is done in two phases: first the content of the
  677. /// vendor-class-identifier option is used as a class, by
  678. /// calling @ref classifyByVendor(). Second classification match
  679. /// expressions are evaluated. The resulting classes will be stored
  680. /// in the packet (see @ref isc::dhcp::Pkt4::classes_ and
  681. /// @ref isc::dhcp::Pkt4::inClass).
  682. ///
  683. /// @param pkt packet to be classified
  684. void classifyPacket(const Pkt4Ptr& pkt);
  685. /// @brief Performs packet processing specific to a vendor class
  686. ///
  687. /// If the selected subnet, query or response in the @c ex object is NULL
  688. /// this method returns immediately and returns true.
  689. ///
  690. /// @note This processing is a likely candidate to be pushed into hooks.
  691. ///
  692. /// @param ex The exchange holding both the client's message and the
  693. /// server's response.
  694. /// @return true if successful, false otherwise (will prevent sending response)
  695. bool vendorClassSpecificProcessing(const Dhcpv4Exchange& ex);
  696. /// @brief Allocation Engine.
  697. /// Pointer to the allocation engine that we are currently using
  698. /// It must be a pointer, because we will support changing engines
  699. /// during normal operation (e.g. to use different allocators)
  700. boost::shared_ptr<AllocEngine> alloc_engine_;
  701. private:
  702. /// @public
  703. /// @brief Assign class using vendor-class-identifier option
  704. ///
  705. /// @note This is the first part of @ref classifyPacket
  706. ///
  707. /// @param pkt packet to be classified
  708. /// @param classes a reference to added class names for logging
  709. void classifyByVendor(const Pkt4Ptr& pkt, std::string& classes);
  710. /// @private
  711. /// @brief Constructs netmask option based on subnet4
  712. /// @param subnet subnet for which the netmask will be calculated
  713. ///
  714. /// @return Option that contains netmask information
  715. static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
  716. uint16_t port_; ///< UDP port number on which server listens.
  717. bool use_bcast_; ///< Should broadcast be enabled on sockets (if true).
  718. public:
  719. /// Class methods and variables for DHCPv4-over-DHCPv6 handler
  720. /// @brief Updates statistics for received packets
  721. /// @param query packet received
  722. static void processStatsReceived(const Pkt4Ptr& query);
  723. /// @brief Updates statistics for transmitted packets
  724. /// @param query packet transmitted
  725. static void processStatsSent(const Pkt4Ptr& response);
  726. /// Indexes for registered hook points
  727. /// @brief index for "buffer4_receive" hook point
  728. static int hook_index_buffer4_receive_;
  729. /// @brief index for "pkt4_receive" hook point
  730. static int hook_index_pkt4_receive_;
  731. /// @brief index for "subnet4_select" hook point
  732. static int hook_index_subnet4_select_;
  733. /// @brief index for "lease4_release" hook point
  734. static int hook_index_lease4_release_;
  735. /// @brief index for "pkt4_send" hook point
  736. static int hook_index_pkt4_send_;
  737. /// @brief index for "buffer4_send" hook point
  738. static int hook_index_buffer4_send_;
  739. /// @brief index for "lease4_decline" hook point
  740. static int hook_index_lease4_decline_;
  741. };
  742. }; // namespace isc::dhcp
  743. }; // namespace isc
  744. #endif // DHCP4_SRV_H