dhcp6_srv.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 DHCPV6_SRV_H
  15. #define DHCPV6_SRV_H
  16. #include <dhcp_ddns/ncr_msg.h>
  17. #include <dhcp/dhcp6.h>
  18. #include <dhcp/duid.h>
  19. #include <dhcp/option.h>
  20. #include <dhcp/option6_client_fqdn.h>
  21. #include <dhcp/option6_ia.h>
  22. #include <dhcp/option_definition.h>
  23. #include <dhcp/pkt6.h>
  24. #include <dhcpsrv/alloc_engine.h>
  25. #include <dhcpsrv/d2_client_mgr.h>
  26. #include <dhcpsrv/subnet.h>
  27. #include <hooks/callout_handle.h>
  28. #include <dhcpsrv/daemon.h>
  29. #include <iostream>
  30. #include <queue>
  31. namespace isc {
  32. namespace dhcp {
  33. /// @brief This exception is thrown when DHCP server hits the error which should
  34. /// result in discarding the message being processed.
  35. class DHCPv6DiscardMessageError : public Exception {
  36. public:
  37. DHCPv6DiscardMessageError(const char* file, size_t line, const char* what) :
  38. isc::Exception(file, line, what) { };
  39. };
  40. /// @brief DHCPv6 server service.
  41. ///
  42. /// This class represents DHCPv6 server. It contains all
  43. /// top-level methods and routines necessary for server operation.
  44. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  45. /// that is going to be used as server-identifier, receives incoming
  46. /// packets, processes them, manages leases assignment and generates
  47. /// appropriate responses.
  48. class Dhcpv6Srv : public Daemon {
  49. public:
  50. /// @brief defines if certain option may, must or must not appear
  51. typedef enum {
  52. FORBIDDEN,
  53. MANDATORY,
  54. OPTIONAL
  55. } RequirementLevel;
  56. /// @brief Minimum length of a MAC address to be used in DUID generation.
  57. static const size_t MIN_MAC_LEN = 6;
  58. /// @brief Default constructor.
  59. ///
  60. /// Instantiates necessary services, required to run DHCPv6 server.
  61. /// In particular, creates IfaceMgr that will be responsible for
  62. /// network interaction. Will instantiate lease manager, and load
  63. /// old or create new DUID.
  64. ///
  65. /// @param port port on will all sockets will listen
  66. Dhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT);
  67. /// @brief Destructor. Used during DHCPv6 service shutdown.
  68. virtual ~Dhcpv6Srv();
  69. /// @brief returns Kea version on stdout and exit.
  70. /// redeclaration/redefinition. @ref Daemon::getVersion())
  71. static std::string getVersion(bool extended);
  72. /// @brief Returns server-indentifier option.
  73. ///
  74. /// @return server-id option
  75. OptionPtr getServerID() { return serverid_; }
  76. /// @brief Main server processing loop.
  77. ///
  78. /// Main server processing loop. Receives incoming packets, verifies
  79. /// their correctness, generates appropriate answer (if needed) and
  80. /// transmits responses.
  81. ///
  82. /// @return true, if being shut down gracefully, fail if experienced
  83. /// critical error.
  84. bool run();
  85. /// @brief Instructs the server to shut down.
  86. void shutdown();
  87. /// @brief Get UDP port on which server should listen.
  88. ///
  89. /// Typically, server listens on UDP port 547. Other ports are only
  90. /// used for testing purposes.
  91. ///
  92. /// @return UDP port on which server should listen.
  93. uint16_t getPort() const {
  94. return (port_);
  95. }
  96. /// @brief Starts DHCP_DDNS client IO if DDNS updates are enabled.
  97. ///
  98. /// If updates are enabled, it Instructs the D2ClientMgr singleton to
  99. /// enter send mode. If D2ClientMgr encounters errors it may throw
  100. /// D2ClientErrors. This method does not catch exceptions.
  101. void startD2();
  102. /// @brief Implements the error handler for DHCP_DDNS IO errors
  103. ///
  104. /// Invoked when a NameChangeRequest send to kea-dhcp-ddns completes with
  105. /// a failed status. These are communications errors, not data related
  106. /// failures.
  107. ///
  108. /// This method logs the failure and then suspends all further updates.
  109. /// Updating can only be restored by reconfiguration or restarting the
  110. /// server. There is currently no retry logic so the first IO error that
  111. /// occurs will suspend updates.
  112. /// @todo We may wish to make this more robust or sophisticated.
  113. ///
  114. /// @param result Result code of the send operation.
  115. /// @param ncr NameChangeRequest which failed to send.
  116. virtual void d2ClientErrorHandler(const dhcp_ddns::
  117. NameChangeSender::Result result,
  118. dhcp_ddns::NameChangeRequestPtr& ncr);
  119. protected:
  120. /// @brief Compare received server id with our server id
  121. ///
  122. /// Checks if the server id carried in a query from a client matches
  123. /// server identifier being used by the server.
  124. ///
  125. /// @param pkt DHCPv6 packet carrying server identifier to be checked.
  126. /// @return true if server id carried in the query matches server id
  127. /// used by the server; false otherwise.
  128. bool testServerID(const Pkt6Ptr& pkt);
  129. /// @brief Check if the message can be sent to unicast.
  130. ///
  131. /// This function checks if the received message conforms to the section 15
  132. /// of RFC3315 which says that: "A server MUST discard any Solicit, Confirm,
  133. /// Rebind or Information-request messages it receives with a unicast
  134. /// destination address.
  135. ///
  136. /// @param pkt DHCPv6 message to be checked.
  137. /// @return false if the message has been sent to unicast address but it is
  138. /// not allowed according to RFC3315, section 15; true otherwise.
  139. bool testUnicast(const Pkt6Ptr& pkt) const;
  140. /// @brief verifies if specified packet meets RFC requirements
  141. ///
  142. /// Checks if mandatory option is really there, that forbidden option
  143. /// is not there, and that client-id or server-id appears only once.
  144. ///
  145. /// @param pkt packet to be checked
  146. /// @param clientid expectation regarding client-id option
  147. /// @param serverid expectation regarding server-id option
  148. /// @throw RFCViolation if any issues are detected
  149. void sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  150. RequirementLevel serverid);
  151. /// @brief Processes incoming Solicit and returns response.
  152. ///
  153. /// Processes received Solicit message and verifies that its sender
  154. /// should be served. In particular IA, TA and PD options are populated
  155. /// with to-be assigned addresses, temporary addresses and delegated
  156. /// prefixes, respectively. In the usual 4 message exchange, server is
  157. /// expected to respond with Advertise message. However, if client
  158. /// requests rapid-commit and server supports it, Reply will be sent
  159. /// instead of Advertise and requested leases will be assigned
  160. /// immediately.
  161. ///
  162. /// @param solicit Solicit message received from client
  163. ///
  164. /// @return Advertise, Reply message or NULL.
  165. Pkt6Ptr processSolicit(const Pkt6Ptr& solicit);
  166. /// @brief Processes incoming Request and returns Reply response.
  167. ///
  168. /// Processes incoming Request message and verifies that its sender
  169. /// should be served. In particular IA, TA and PD options are populated
  170. /// with assigned addresses, temporary addresses and delegated
  171. /// prefixes, respectively. Uses LeaseMgr to allocate or update existing
  172. /// leases.
  173. ///
  174. /// @param request a message received from client
  175. ///
  176. /// @return REPLY message or NULL
  177. Pkt6Ptr processRequest(const Pkt6Ptr& request);
  178. /// @brief Processes incoming Renew message.
  179. ///
  180. /// @param renew message received from the client
  181. /// @return Reply message to be sent to the client.
  182. Pkt6Ptr processRenew(const Pkt6Ptr& renew);
  183. /// @brief Processes incoming Rebind message.
  184. ///
  185. /// @todo There are cases when the Rebind message should be discarded
  186. /// by the DHCP server. One of those is when the server doesn't have a
  187. /// record of the client and it is unable to determine whether the
  188. /// client is on the appropriate link or not. We don't seem to do it
  189. /// now.
  190. ///
  191. /// @param rebind message received from the client.
  192. /// @return Reply message to be sent to the client.
  193. Pkt6Ptr processRebind(const Pkt6Ptr& rebind);
  194. /// @brief Processes incoming Confirm message and returns Reply.
  195. ///
  196. /// This function processes Confirm message from the client according
  197. /// to section 18.2.2. of RFC3315. It discards the Confirm message if
  198. /// the message sent by the client contains no addresses, i.e. it has
  199. /// no IA_NA options or all IA_NA options contain no IAAddr options.
  200. ///
  201. /// If the Confirm message contains addresses this function will perform
  202. /// the following checks:
  203. /// - check if there is appropriate subnet configured for the client
  204. /// (e.g. subnet from which addresses are assigned for requests
  205. /// received on the particular interface).
  206. /// - check if all addresses sent in the Confirm message belong to the
  207. /// selected subnet.
  208. ///
  209. /// If any of the checks above fails, the Reply message with the status
  210. /// code NotOnLink is returned. Otherwise, the Reply message with the
  211. /// status code Success is returned.
  212. ///
  213. /// @param confirm Confirm message sent by a client.
  214. ///
  215. /// @return Reply message from the server or NULL pointer if Confirm
  216. /// message should be discarded by the server.
  217. Pkt6Ptr processConfirm(const Pkt6Ptr& confirm);
  218. /// @brief Process incoming Release message.
  219. ///
  220. /// @param release message received from client
  221. /// @return Reply message to be sent to the client.
  222. Pkt6Ptr processRelease(const Pkt6Ptr& release);
  223. /// @brief Stub function that will handle incoming Decline.
  224. ///
  225. /// @param decline message received from client
  226. Pkt6Ptr processDecline(const Pkt6Ptr& decline);
  227. /// @brief Processes incoming Information-request message.
  228. ///
  229. /// @param inf_request message received from client
  230. /// @return Reply message to be sent to the client.
  231. Pkt6Ptr processInfRequest(const Pkt6Ptr& inf_request);
  232. /// @brief Creates status-code option.
  233. ///
  234. /// @param code status code value (see RFC3315)
  235. /// @param text textual explanation (will be sent in status code option)
  236. /// @return status-code option
  237. OptionPtr createStatusCode(uint16_t code, const std::string& text);
  238. /// @brief Selects a subnet for a given client's packet.
  239. ///
  240. /// @param question client's message
  241. /// @return selected subnet (or NULL if no suitable subnet was found)
  242. isc::dhcp::Subnet6Ptr selectSubnet(const Pkt6Ptr& question);
  243. /// @brief Processes IA_NA option (and assigns addresses if necessary).
  244. ///
  245. /// Generates response to IA_NA. This typically includes selecting (and
  246. /// allocating a lease in case of REQUEST) an address lease and creating
  247. /// IAADDR option. In case of allocation failure, it may contain
  248. /// status code option with non-zero status, denoting cause of the
  249. /// allocation failure.
  250. ///
  251. /// @param query client's message (typically SOLICIT or REQUEST)
  252. /// @param answer server's response to the client's message. This
  253. /// message should contain Client FQDN option being sent by the server
  254. /// to the client (if the client sent this option to the server).
  255. /// @param orig_ctx client context (contains subnet, duid and other parameters)
  256. /// @param ia pointer to client's IA_NA option (client's request)
  257. ///
  258. /// @return IA_NA option (server's response)
  259. OptionPtr assignIA_NA(const isc::dhcp::Pkt6Ptr& query,
  260. const isc::dhcp::Pkt6Ptr& answer,
  261. AllocEngine::ClientContext6& orig_ctx,
  262. Option6IAPtr ia);
  263. /// @brief Processes IA_PD option (and assigns prefixes if necessary).
  264. ///
  265. /// Generates response to IA_PD. This typically includes selecting (and
  266. /// allocating in the case of REQUEST) a prefix lease and creating an
  267. /// IAPREFIX option. In case of an allocation failure, it may contain a
  268. /// status code option with non-zero status denoting the cause of the
  269. /// allocation failure.
  270. ///
  271. /// @param query client's message (typically SOLICIT or REQUEST)
  272. /// @param orig_ctx client context (contains subnet, duid and other parameters)
  273. /// @param ia pointer to client's IA_PD option (client's request)
  274. /// @return IA_PD option (server's response)
  275. OptionPtr assignIA_PD(const Pkt6Ptr& query,
  276. AllocEngine::ClientContext6& orig_ctx,
  277. boost::shared_ptr<Option6IA> ia);
  278. /// @brief Extends lifetime of the specific IA_NA option.
  279. ///
  280. /// Generates response to IA_NA in Renew or Rebind. This typically includes
  281. /// finding a lease that corresponds to the received address. If no such
  282. /// lease is found, an IA_NA response is generated with an appropriate
  283. /// status code.
  284. ///
  285. /// @todo The behavior of this function will need to be extended to support
  286. /// draft-ietf-dhc-dhcpv6-stateful-issues. This draft modifies the behavior
  287. /// described in RFC3315 with respect to Renew and Rebind processing. Key
  288. /// changes are (version -05):
  289. /// - Renewing and Rebinding client MAY request additional bindings by
  290. /// putting an IA for all bindings it desires but has been unable to obtain.
  291. /// Server MAY allocate addresses if it finds that they are appropriate for
  292. /// the link that client is attached to.
  293. /// - When receiving Rebind, if the server determines that the addresses are
  294. /// not appropriate for the link the client is attached to, the server MAY
  295. /// send the IA with address lifetimes set to 0 or discard the message.
  296. ///
  297. /// @param subnet subnet the sender belongs to
  298. /// @param duid client's duid
  299. /// @param query client's message (Renew or Rebind)
  300. /// @param answer server's response to the client's message. This
  301. /// message should contain Client FQDN option being sent by the server
  302. /// to the client (if the client sent this option to the server).
  303. /// @param orig_ctx client context (contains subnet, duid and other parameters)
  304. /// @param ia IA_NA option which carries adress for which lease lifetime
  305. /// will be extended.
  306. /// @return IA_NA option (server's response)
  307. OptionPtr extendIA_NA(const Pkt6Ptr& query, const Pkt6Ptr& answer,
  308. AllocEngine::ClientContext6& orig_ctx,
  309. Option6IAPtr ia);
  310. /// @brief Extends lifetime of the prefix.
  311. ///
  312. /// This function is called by the logic which processes Renew and Rebind
  313. /// messages to extend the lifetime of the existing prefix.
  314. ///
  315. /// The behavior of this function is different in that when there is no
  316. /// binding found in the lease database for the particular client the
  317. /// NoBinding status code is returned when processing Renew, the exception
  318. /// is thrown when there is no binding and the Rebind message is processed
  319. /// (see RFC3633, section 12.2. for details).
  320. ///
  321. /// @param query client's message
  322. /// @param orig_ctx client context (contains subnet, duid and other parameters)
  323. /// @param ia IA_PD option that is being renewed
  324. /// @return IA_PD option (server's response)
  325. /// @throw DHCPv6DiscardMessageError when the message being processed should
  326. /// be discarded by the server, i.e. there is no binding for the client doing
  327. /// Rebind.
  328. OptionPtr extendIA_PD(const Pkt6Ptr& query,
  329. AllocEngine::ClientContext6& orig_ctx,
  330. Option6IAPtr ia);
  331. /// @brief Releases specific IA_NA option
  332. ///
  333. /// Generates response to IA_NA in Release message. This covers finding and
  334. /// removal of a lease that corresponds to the received address. If no such
  335. /// lease is found, an IA_NA response is generated with an appropriate
  336. /// status code.
  337. ///
  338. /// As RFC 3315 requires that a single status code be sent for the whole message,
  339. /// this method may update the passed general_status: it is set to SUCCESS when
  340. /// message processing begins, but may be updated to some error code if the
  341. /// release process fails.
  342. ///
  343. /// @param duid client's duid
  344. /// @param query client's message
  345. /// @param general_status a global status (it may be updated in case of errors)
  346. /// @param ia IA_NA option that is being released
  347. /// @return IA_NA option (server's response)
  348. OptionPtr releaseIA_NA(const DuidPtr& duid, const Pkt6Ptr& query,
  349. int& general_status,
  350. boost::shared_ptr<Option6IA> ia);
  351. /// @brief Releases specific IA_PD option
  352. ///
  353. /// Generates response to IA_PD in Release message. This covers finding and
  354. /// removal of a lease that corresponds to the received prefix(es). If no such
  355. /// lease is found, an IA_PD response is generated with an appropriate
  356. /// status code.
  357. ///
  358. /// @param duid client's duid
  359. /// @param query client's message
  360. /// @param general_status a global status (it may be updated in case of errors)
  361. /// @param ia IA_PD option that is being released
  362. /// @return IA_PD option (server's response)
  363. OptionPtr releaseIA_PD(const DuidPtr& duid, const Pkt6Ptr& query,
  364. int& general_status,
  365. boost::shared_ptr<Option6IA> ia);
  366. /// @brief Copies required options from client message to server answer.
  367. ///
  368. /// Copies options that must appear in any server response (ADVERTISE, REPLY)
  369. /// to client's messages (SOLICIT, REQUEST, RENEW, REBIND, DECLINE, RELEASE).
  370. /// One notable example is client-id. Other options may be copied as required.
  371. /// Relay information details are also copied here.
  372. ///
  373. /// @param question client's message (options will be copied from here)
  374. /// @param answer server's message (options will be copied here)
  375. void copyClientOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  376. /// @brief Appends default options to server's answer.
  377. ///
  378. /// Adds required options to server's answer. In particular, server-id
  379. /// is added. Possibly other mandatory options will be added, depending
  380. /// on type (or content) of client message.
  381. ///
  382. /// @param question client's message
  383. /// @param answer server's message (options will be added here)
  384. void appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  385. /// @brief Appends requested options to server's answer.
  386. ///
  387. /// Appends options requested by client to the server's answer.
  388. ///
  389. /// @param question client's message
  390. /// @param answer server's message (options will be added here)
  391. /// @param ctx client context (contains subnet, duid and other parameters)
  392. void appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer,
  393. AllocEngine::ClientContext6& ctx);
  394. /// @brief Appends requested vendor options to server's answer.
  395. ///
  396. /// This is mostly useful for Cable Labs options for now, but the method
  397. /// is easily extensible to other vendors.
  398. ///
  399. /// @param question client's message
  400. /// @param answer server's message (vendor options will be added here)
  401. /// @param ctx client context (contains subnet, duid and other parameters)
  402. void appendRequestedVendorOptions(const Pkt6Ptr& question, Pkt6Ptr& answer,
  403. AllocEngine::ClientContext6& ctx);
  404. /// @brief Assigns leases.
  405. ///
  406. /// It supports non-temporary addresses (IA_NA) and prefixes (IA_PD). It
  407. /// does NOT support temporary addresses (IA_TA).
  408. ///
  409. /// @param question client's message (with requested IA options)
  410. /// @param answer server's message (IA options will be added here).
  411. /// This message should contain Client FQDN option being sent by the server
  412. /// to the client (if the client sent this option to the server).
  413. /// @param ctx client context (contains subnet, duid and other parameters)
  414. void assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer,
  415. AllocEngine::ClientContext6& ctx);
  416. /// @brief Processes Client FQDN Option.
  417. ///
  418. /// This function retrieves DHCPv6 Client FQDN %Option (if any) from the
  419. /// packet sent by a client and takes necessary actions upon this option.
  420. /// Received option comprises flags field which controls what DNS updates
  421. /// server should do. Server may override client's preference based on
  422. /// the current configuration. Server indicates that it has overridden
  423. /// the preference by storing DHCPv6 Client FQDN option with the
  424. /// appropriate flags in the response to a client. This option is also
  425. /// used to communicate the client's domain-name which should be sent
  426. /// to the DNS in the update. Again, server may act upon the received
  427. /// domain-name, i.e. if the provided domain-name is partial it should
  428. /// generate the fully qualified domain-name.
  429. ///
  430. /// This function takes into account the host reservation if one is matched
  431. /// to this client when forming the FQDN to be used with DNS as well as the
  432. /// lease name to be stored with the lease. In the following the term
  433. /// "reserved hostname" means a host reservation which includes a
  434. /// non-blank hostname.
  435. ///
  436. /// - If there is no Client FQDN and no reserved hostname then there
  437. /// will no be DNS updates and the lease hostname will be empty.
  438. ///
  439. /// - If there is no Client FQDN but there is reserverd hostname then
  440. /// there will be no DNS updates and the lease hostname will be equal
  441. /// to reserved hostname.
  442. ///
  443. /// - If there is a Client FQDN and a reserved hostname, then both the
  444. /// FQDN and lease hostname will be equal to reserved hostname with
  445. /// the qualifying suffix appended.
  446. ///
  447. /// - If there is a Client FQDN but no reserverd hostname then both the
  448. /// FQDN and lease hostname will be equal to the name provided in the
  449. /// client FQDN adjusted according the the DhcpDdns configuration
  450. /// parameters (e.g.replace-client-name, qualifying suffix...).
  451. ///
  452. /// All the logic required to form appropriate answer to the client is
  453. /// held in this function.
  454. ///
  455. /// @param question Client's message.
  456. /// @param answer Server's response to a client. If server generated
  457. /// Client FQDN option for the client, this option is stored in this
  458. /// object.
  459. /// @param ctx client context (includes subnet, client-id, hw-addr etc.)
  460. void processClientFqdn(const Pkt6Ptr& question, const Pkt6Ptr& answer,
  461. AllocEngine::ClientContext6& ctx);
  462. /// @brief Creates a number of @c isc::dhcp_ddns::NameChangeRequest objects
  463. /// based on the DHCPv6 Client FQDN %Option.
  464. ///
  465. /// The @c isc::dhcp_ddns::NameChangeRequest class encapsulates the request
  466. /// from the DHCPv6 server to the DHCP-DDNS module to perform DNS Update.
  467. /// The FQDN option carries response to the client about DNS updates that
  468. /// server intents to perform for the DNS client. Based on this, the
  469. /// function will create zero or more @c isc::dhcp_ddns::NameChangeRequest
  470. /// objects and store them in the internal queue. Requests created by this
  471. /// function are only adding or updating DNS records. In order to generate
  472. /// requests for DNS records removal, use @c createRemovalNameChangeRequest.
  473. /// If ddns updates are disabled, this method returns immediately.
  474. ///
  475. /// @todo Add support for multiple IAADDR options in the IA_NA.
  476. ///
  477. /// @param answer A message beging sent to the Client. If it holds the
  478. /// Client FQDN option, this option is used to create NameChangeRequests.
  479. void createNameChangeRequests(const Pkt6Ptr& answer);
  480. /// @brief Creates a @c isc::dhcp_ddns::NameChangeRequest which requests
  481. /// removal of DNS entries for a particular lease.
  482. ///
  483. /// This function should be called upon removal of the lease from the lease
  484. /// database, i.e, when client sent Release or Decline message. It will
  485. /// create a single @c isc::dhcp_ddns::NameChangeRequest which removes the
  486. /// existing DNS records for the lease, which server is responsible for.
  487. /// Note that this function will not remove the entries which server hadn't
  488. /// added. This is the case, when client performs forward DNS update on its
  489. /// own.
  490. /// If ddns updates are disabled, this method returns immediately.
  491. ///
  492. /// @param lease A lease for which the the removal of corresponding DNS
  493. /// records will be performed.
  494. void createRemovalNameChangeRequest(const Lease6Ptr& lease);
  495. /// @brief Attempts to extend the lifetime of IAs.
  496. ///
  497. /// This function is called when a client sends Renew or Rebind message.
  498. /// It iterates through received IA options and attempts to extend
  499. /// corresponding lease lifetimes. Internally, it calls
  500. /// @c Dhcpv6Srv::extendIA_NA and @c Dhcpv6Srv::extendIA_PD to extend
  501. /// the lifetime of IA_NA and IA_PD leases accordingly.
  502. ///
  503. /// @param query client's Renew or Rebind message
  504. /// @param reply server's response
  505. /// @param ctx client context (contains subnet, duid and other parameters)
  506. void extendLeases(const Pkt6Ptr& query, Pkt6Ptr& reply,
  507. AllocEngine::ClientContext6& ctx);
  508. /// @brief Attempts to release received addresses
  509. ///
  510. /// It iterates through received IA_NA options and attempts to release
  511. /// received addresses. If no such leases are found, or the lease fails
  512. /// proper checks (e.g. belongs to someone else), a proper status
  513. /// code is added to reply message. Released addresses are not added
  514. /// to REPLY packet, just its IA_NA containers.
  515. /// @param release client's message asking to release
  516. /// @param reply server's response
  517. /// @param ctx client context (includes subnet, client-id, hw-addr etc.)
  518. void releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply,
  519. AllocEngine::ClientContext6& ctx);
  520. /// @brief Sets server-identifier.
  521. ///
  522. /// This method attempts to generate server-identifier DUID. It generates a
  523. /// new DUID using interface link-layer addresses (EUI-64) + timestamp (DUID
  524. /// type duid-llt, see RFC3315, section 9.2). If there are no suitable
  525. /// interfaces present, exception it thrown
  526. ///
  527. /// @throws isc::Unexpected Failed to read DUID file and no suitable
  528. /// interfaces for new DUID generation are detected.
  529. void generateServerID();
  530. /// @brief attempts to load DUID from a file
  531. ///
  532. /// Tries to load duid from a text file. If the load is successful,
  533. /// it creates server-id option and stores it in serverid_ (to be used
  534. /// later by getServerID()).
  535. ///
  536. /// @param file_name name of the DUID file to load
  537. /// @return true if load was successful, false otherwise
  538. bool loadServerID(const std::string& file_name);
  539. /// @brief attempts to write DUID to a file
  540. /// Tries to write duid content (stored in serverid_) to a text file.
  541. ///
  542. /// @param file_name name of the DUID file to write
  543. /// @return true if write was successful, false otherwise
  544. bool writeServerID(const std::string& file_name);
  545. /// @brief converts DUID to text
  546. /// Converts content of DUID option to a text representation, e.g.
  547. /// 01:ff:02:03:06:80:90:ab:cd:ef
  548. ///
  549. /// @param opt option that contains DUID
  550. /// @return string representation
  551. static std::string duidToString(const OptionPtr& opt);
  552. /// @brief dummy wrapper around IfaceMgr::receive6
  553. ///
  554. /// This method is useful for testing purposes, where its replacement
  555. /// simulates reception of a packet. For that purpose it is protected.
  556. virtual Pkt6Ptr receivePacket(int timeout);
  557. /// @brief dummy wrapper around IfaceMgr::send()
  558. ///
  559. /// This method is useful for testing purposes, where its replacement
  560. /// simulates transmission of a packet. For that purpose it is protected.
  561. virtual void sendPacket(const Pkt6Ptr& pkt);
  562. /// @brief Implements a callback function to parse options in the message.
  563. ///
  564. /// @param buf a A buffer holding options in on-wire format.
  565. /// @param option_space A name of the option space which holds definitions
  566. /// of to be used to parse options in the packets.
  567. /// @param [out] options A reference to the collection where parsed options
  568. /// will be stored.
  569. /// @param relay_msg_offset Reference to a size_t structure. If specified,
  570. /// offset to beginning of relay_msg option will be stored in it.
  571. /// @param relay_msg_len reference to a size_t structure. If specified,
  572. /// length of the relay_msg option will be stored in it.
  573. /// @return An offset to the first byte after last parsed option.
  574. size_t unpackOptions(const OptionBuffer& buf,
  575. const std::string& option_space,
  576. isc::dhcp::OptionCollection& options,
  577. size_t* relay_msg_offset,
  578. size_t* relay_msg_len);
  579. /// @brief Assigns incoming packet to zero or more classes.
  580. ///
  581. /// @note For now, the client classification is very simple. It just uses
  582. /// content of the vendor-class-identifier option as a class. The resulting
  583. /// class will be stored in packet (see @ref isc::dhcp::Pkt6::classes_ and
  584. /// @ref isc::dhcp::Pkt6::inClass).
  585. ///
  586. /// @param pkt packet to be classified
  587. void classifyPacket(const Pkt6Ptr& pkt);
  588. /// @brief Attempts to get a MAC/hardware address using configred sources
  589. ///
  590. /// Tries to extract MAC/hardware address information from the packet
  591. /// using MAC sources configured in 'mac-sources' configuration parameter.
  592. ///
  593. /// @param pkt will try to exact MAC address from this packet
  594. /// @return HWaddr pointer (or NULL if configured methods fail)
  595. static HWAddrPtr getMAC(const Pkt6Ptr& pkt);
  596. /// @brief Processes Relay-supplied options, if present
  597. ///
  598. /// This method implements RFC6422. It checks if there are any RSOO options
  599. /// inserted by the relay agents in the query message. If there are, they
  600. /// are copied over to the response if they meet the following criteria:
  601. /// - the option is marked as RSOO-enabled (see relay-supplied-options
  602. /// configuration parameter)
  603. /// - there is no such option provided by the server)
  604. void processRSOO(const Pkt6Ptr& query, const Pkt6Ptr& rsp);
  605. /// @brief Creates client context for specified packet
  606. ///
  607. /// Instantiates the ClientContext6 and then:
  608. /// - Performs the subnet selection and stores the result in context
  609. /// - Extracts the duid from the packet and saves it to the context
  610. /// - Extracts the hardware address from the packet and saves it to
  611. /// the context
  612. /// - Performs host reservation lookup and stores the result in the
  613. /// context
  614. ///
  615. /// @return client context
  616. AllocEngine::ClientContext6 createContext(const Pkt6Ptr& pkt);
  617. /// @brief this is a prefix added to the contend of vendor-class option
  618. ///
  619. /// If incoming packet has a vendor class option, its content is
  620. /// prepended with this prefix and then interpreted as a class.
  621. /// For example, a packet that sends vendor class with value of "FOO"
  622. /// will cause the packet to be assigned to class VENDOR_CLASS_FOO.
  623. static const std::string VENDOR_CLASS_PREFIX;
  624. private:
  625. /// @brief Generate FQDN to be sent to a client if none exists.
  626. ///
  627. /// This function is meant to be called by the functions which process
  628. /// client's messages. The function should be called after a function
  629. /// which creates FQDN option for the client. This option must exist
  630. /// in the answer message specified as an argument. It must also be
  631. /// called after functions which assign leases for a client. The
  632. /// IA options being a result of lease acquisition must be appended
  633. /// to the message specified as a parameter.
  634. ///
  635. /// If the Client FQDN option being present in the message carries empty
  636. /// hostname, this function will attempt to generate hostname from the
  637. /// IPv6 address being acquired by the client. The IPv6 address is retrieved
  638. /// from the IA_NA option carried in the specified message. If multiple
  639. /// addresses are present in the particular IA_NA option or multiple IA_NA
  640. /// options exist, the first address found is selected.
  641. ///
  642. /// The IPv6 address is converted to the hostname using the following
  643. /// pattern:
  644. /// @code
  645. /// prefix-converted-ip-address.domain-name-suffix.
  646. /// @endcode
  647. /// where:
  648. /// - prefix is a configurable prefix string appended to all auto-generated
  649. /// hostnames.
  650. /// - converted-ip-address is created by replacing all colons from the IPv6
  651. /// address with hyphens.
  652. /// - domain-name-suffix is a suffix for a domain name that, together with
  653. /// the other parts, constitute the fully qualified domain name.
  654. ///
  655. /// When hostname is successfully generated, it is either used to update
  656. /// FQDN-related fields in a lease database or to update the Client FQDN
  657. /// option being sent back to the client. The lease database update is
  658. /// NOT performed if Advertise message is being processed.
  659. ///
  660. /// @param answer Message being sent to a client, which may hold IA_NA
  661. /// and Client FQDN options to be used to generate name for a client.
  662. ///
  663. /// @throw isc::Unexpected if specified message is NULL. This is treated
  664. /// as a programmatic error.
  665. void generateFqdn(const Pkt6Ptr& answer);
  666. /// @brief Triggers removal Name Change Request if FQDN data changes in leases
  667. ///
  668. /// If there are any differences (different fwd or rev flags, or different
  669. /// hostname) a DNS update for removing entry will be generated.
  670. ///
  671. /// @param old_lease old version of the lease
  672. /// @param new_lease new version of the lease (may be NULL)
  673. /// @param hostname specifies hostname (for printing purposes)
  674. /// @param do_fwd specifies if reverse updates are enabled (for printing purposes)
  675. /// @param do_rev specifies if reverse updates are enabled (for printing purposes)
  676. void conditionalNCRRemoval(Lease6Ptr& old_lease, Lease6Ptr& new_lease,
  677. const std::string& hostname,
  678. bool do_fwd, bool do_rev);
  679. /// @brief Utility method that extracts DUID from client-id option
  680. ///
  681. /// @param pkt the message that contains client-id option
  682. /// @return extracted DUID (or NULL if client-id is missing)
  683. DuidPtr extractClientId(const Pkt6Ptr& pkt);
  684. /// @brief Allocation Engine.
  685. /// Pointer to the allocation engine that we are currently using
  686. /// It must be a pointer, because we will support changing engines
  687. /// during normal operation (e.g. to use different allocators)
  688. boost::shared_ptr<AllocEngine> alloc_engine_;
  689. /// Server DUID (to be sent in server-identifier option)
  690. OptionPtr serverid_;
  691. /// UDP port number on which server listens.
  692. uint16_t port_;
  693. protected:
  694. /// Indicates if shutdown is in progress. Setting it to true will
  695. /// initiate server shutdown procedure.
  696. volatile bool shutdown_;
  697. /// Holds a list of @c isc::dhcp_ddns::NameChangeRequest objects, which
  698. /// are waiting for sending to kea-dhcp-ddns module.
  699. std::queue<isc::dhcp_ddns::NameChangeRequest> name_change_reqs_;
  700. };
  701. }; // namespace isc::dhcp
  702. }; // namespace isc
  703. #endif // DHCP6_SRV_H