dhcp6_srv.h 39 KB

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