dhcp4_srv.h 32 KB

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