dhcp6_srv.h 38 KB

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