dhcp4_srv.h 33 KB

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