dhcp6_srv.h 37 KB

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