dhcp4_srv.h 32 KB

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