dhcp6_srv.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. // Copyright (C) 2011-2017 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-identifier 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. ///
  433. /// @param co_list configured option list
  434. void appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer,
  435. const CfgOptionList& co_list);
  436. /// @brief Appends requested vendor options to server's answer.
  437. ///
  438. /// This is mostly useful for Cable Labs options for now, but the method
  439. /// is easily extensible to other vendors.
  440. ///
  441. /// @param question client's message
  442. /// @param answer server's message (vendor options will be added here)
  443. /// @param ctx client context (contains subnet, duid and other parameters)
  444. /// @param co_list configured option list
  445. void appendRequestedVendorOptions(const Pkt6Ptr& question, Pkt6Ptr& answer,
  446. AllocEngine::ClientContext6& ctx,
  447. const CfgOptionList& co_list);
  448. /// @brief Assigns leases.
  449. ///
  450. /// It supports non-temporary addresses (IA_NA) and prefixes (IA_PD). It
  451. /// does NOT support temporary addresses (IA_TA).
  452. ///
  453. /// @param question client's message (with requested IA options)
  454. /// @param answer server's message (IA options will be added here).
  455. /// This message should contain Client FQDN option being sent by the server
  456. /// to the client (if the client sent this option to the server).
  457. /// @param ctx client context (contains subnet, duid and other parameters)
  458. void assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer,
  459. AllocEngine::ClientContext6& ctx);
  460. /// @brief Processes Client FQDN Option.
  461. ///
  462. /// This function retrieves DHCPv6 Client FQDN %Option (if any) from the
  463. /// packet sent by a client and takes necessary actions upon this option.
  464. /// Received option comprises flags field which controls what DNS updates
  465. /// server should do. Server may override client's preference based on
  466. /// the current configuration. Server indicates that it has overridden
  467. /// the preference by storing DHCPv6 Client FQDN option with the
  468. /// appropriate flags in the response to a client. This option is also
  469. /// used to communicate the client's domain-name which should be sent
  470. /// to the DNS in the update. Again, server may act upon the received
  471. /// domain-name, i.e. if the provided domain-name is partial it should
  472. /// generate the fully qualified domain-name.
  473. ///
  474. /// This function takes into account the host reservation if one is matched
  475. /// to this client when forming the FQDN to be used with DNS as well as the
  476. /// lease name to be stored with the lease. In the following the term
  477. /// "reserved hostname" means a host reservation which includes a
  478. /// non-blank hostname.
  479. ///
  480. /// - If there is no Client FQDN and no reserved hostname then there
  481. /// will no be DNS updates and the lease hostname will be empty.
  482. ///
  483. /// - If there is no Client FQDN but there is reserved hostname then
  484. /// there will be no DNS updates and the lease hostname will be equal
  485. /// to reserved hostname.
  486. ///
  487. /// - If there is a Client FQDN and a reserved hostname, then both the
  488. /// FQDN and lease hostname will be equal to reserved hostname with
  489. /// the qualifying suffix appended.
  490. ///
  491. /// - If there is a Client FQDN but no reserved hostname then both the
  492. /// FQDN and lease hostname will be equal to the name provided in the
  493. /// client FQDN adjusted according the the DhcpDdns configuration
  494. /// parameters (e.g.replace-client-name, qualifying suffix...).
  495. ///
  496. /// All the logic required to form appropriate answer to the client is
  497. /// held in this function.
  498. ///
  499. /// @param question Client's message.
  500. /// @param answer Server's response to a client. If server generated
  501. /// Client FQDN option for the client, this option is stored in this
  502. /// object.
  503. /// @param ctx client context (includes subnet, client-id, hw-addr etc.)
  504. void processClientFqdn(const Pkt6Ptr& question, const Pkt6Ptr& answer,
  505. AllocEngine::ClientContext6& ctx);
  506. /// @brief Creates a number of @c isc::dhcp_ddns::NameChangeRequest objects
  507. /// based on the DHCPv6 Client FQDN %Option.
  508. ///
  509. /// The @c isc::dhcp_ddns::NameChangeRequest class encapsulates the request
  510. /// from the DHCPv6 server to the DHCP-DDNS module to perform DNS Update.
  511. /// The FQDN option carries response to the client about DNS updates that
  512. /// server intends to perform for the DNS client. Based on this, the
  513. /// function will create zero or more @c isc::dhcp_ddns::NameChangeRequest
  514. /// objects and store them in the internal queue. Requests created by this
  515. /// function are only for adding or updating DNS records. If DNS updates are
  516. /// disabled, this method returns immediately.
  517. ///
  518. /// @todo Add support for multiple IAADDR options in the IA_NA.
  519. ///
  520. /// @param answer A message begins sent to the Client. If it holds the
  521. /// @param ctx client context (contains subnet, duid and other parameters)
  522. /// Client FQDN option, this option is used to create NameChangeRequests.
  523. void createNameChangeRequests(const Pkt6Ptr& answer,
  524. AllocEngine::ClientContext6& ctx);
  525. /// @brief Attempts to extend the lifetime of IAs.
  526. ///
  527. /// This function is called when a client sends Renew or Rebind message.
  528. /// It iterates through received IA options and attempts to extend
  529. /// corresponding lease lifetimes. Internally, it calls
  530. /// @c Dhcpv6Srv::extendIA_NA and @c Dhcpv6Srv::extendIA_PD to extend
  531. /// the lifetime of IA_NA and IA_PD leases accordingly.
  532. ///
  533. /// @param query client's Renew or Rebind message
  534. /// @param reply server's response
  535. /// @param ctx client context (contains subnet, duid and other parameters)
  536. void extendLeases(const Pkt6Ptr& query, Pkt6Ptr& reply,
  537. AllocEngine::ClientContext6& ctx);
  538. /// @brief Attempts to release received addresses
  539. ///
  540. /// It iterates through received IA_NA options and attempts to release
  541. /// received addresses. If no such leases are found, or the lease fails
  542. /// proper checks (e.g. belongs to someone else), a proper status
  543. /// code is added to reply message. Released addresses are not added
  544. /// to REPLY packet, just its IA_NA containers.
  545. /// @param release client's message asking to release
  546. /// @param reply server's response
  547. /// @param ctx client context (includes subnet, client-id, hw-addr etc.)
  548. void releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply,
  549. AllocEngine::ClientContext6& ctx);
  550. /// @brief converts DUID to text
  551. /// Converts content of DUID option to a text representation, e.g.
  552. /// 01:ff:02:03:06:80:90:ab:cd:ef
  553. ///
  554. /// @param opt option that contains DUID
  555. /// @return string representation
  556. static std::string duidToString(const OptionPtr& opt);
  557. /// @brief dummy wrapper around IfaceMgr::receive6
  558. ///
  559. /// This method is useful for testing purposes, where its replacement
  560. /// simulates reception of a packet. For that purpose it is protected.
  561. virtual Pkt6Ptr receivePacket(int timeout);
  562. /// @brief dummy wrapper around IfaceMgr::send()
  563. ///
  564. /// This method is useful for testing purposes, where its replacement
  565. /// simulates transmission of a packet. For that purpose it is protected.
  566. virtual void sendPacket(const Pkt6Ptr& pkt);
  567. /// @brief Assigns incoming packet to zero or more classes.
  568. ///
  569. /// @note This is done in two phases: first the content of the
  570. /// vendor-class-identifier option is used as a class, by
  571. /// calling @ref classifyByVendor(). Second classification match
  572. /// expressions are evaluated. The resulting classes will be stored
  573. /// in the packet (see @ref isc::dhcp::Pkt6::classes_ and
  574. /// @ref isc::dhcp::Pkt6::inClass).
  575. ///
  576. /// @param pkt packet to be classified
  577. void classifyPacket(const Pkt6Ptr& pkt);
  578. /// @brief Assigns classes retrieved from host reservation database.
  579. ///
  580. /// @param pkt Pointer to the packet to which classes will be assigned.
  581. /// @param ctx Reference to the client context.
  582. void setReservedClientClasses(const Pkt6Ptr& pkt,
  583. const AllocEngine::ClientContext6& ctx);
  584. /// @brief Attempts to get a MAC/hardware address using configred sources
  585. ///
  586. /// Tries to extract MAC/hardware address information from the packet
  587. /// using MAC sources configured in 'mac-sources' configuration parameter.
  588. ///
  589. /// @param pkt will try to exact MAC address from this packet
  590. /// @return HWaddr pointer (or NULL if configured methods fail)
  591. static HWAddrPtr getMAC(const Pkt6Ptr& pkt);
  592. /// @brief Processes Relay-supplied options, if present
  593. ///
  594. /// This method implements RFC6422. It checks if there are any RSOO options
  595. /// inserted by the relay agents in the query message. If there are, they
  596. /// are copied over to the response if they meet the following criteria:
  597. /// - the option is marked as RSOO-enabled (see relay-supplied-options
  598. /// configuration parameter)
  599. /// - there is no such option provided by the server)
  600. void processRSOO(const Pkt6Ptr& query, const Pkt6Ptr& rsp);
  601. /// @brief Initializes client context for specified packet
  602. ///
  603. /// This method:
  604. /// - Performs the subnet selection and stores the result in context
  605. /// - Extracts the duid from the packet and saves it to the context
  606. /// - Extracts the hardware address from the packet and saves it to
  607. /// the context
  608. /// - Performs host reservation lookup and stores the result in the
  609. /// context
  610. ///
  611. /// Even though the incoming packet type is known to this method, it
  612. /// doesn't set the @c fake_allocation flag, because of a possibility
  613. /// that the Rapid Commit option is in use. The @c fake_allocation
  614. /// flag is set appropriately after it has been determined whether
  615. /// the Rapid Commit option was included and that the server respects
  616. /// it.
  617. ///
  618. /// @param pkt pointer to a packet for which context will be created.
  619. /// @param [out] ctx reference to context object to be initialized.
  620. void initContext(const Pkt6Ptr& pkt, AllocEngine::ClientContext6& ctx);
  621. /// @brief this is a prefix added to the contend of vendor-class option
  622. ///
  623. /// If incoming packet has a vendor class option, its content is
  624. /// prepended with this prefix and then interpreted as a class.
  625. /// For example, a packet that sends vendor class with value of "FOO"
  626. /// will cause the packet to be assigned to class VENDOR_CLASS_FOO.
  627. static const std::string VENDOR_CLASS_PREFIX;
  628. /// @brief Attempts to decline all leases in specified Decline message.
  629. ///
  630. /// This method iterates over all IA_NA options and calls @ref declineIA on
  631. /// each of them.
  632. ///
  633. /// @param decline Decline message sent by a client
  634. /// @param reply Server's response (IA_NA with status will be added here)
  635. /// @param ctx context
  636. /// @return true when expected to continue, false when hooks told us to drop
  637. /// the packet
  638. bool declineLeases(const Pkt6Ptr& decline, Pkt6Ptr& reply,
  639. AllocEngine::ClientContext6& ctx);
  640. /// @brief Declines leases in a single IA_NA option
  641. ///
  642. /// This method iterates over all addresses in this IA_NA, verifies
  643. /// whether they belong to the client and calls @ref declineLease. If there's
  644. /// an error, general_status (a status put in the top level scope), will be
  645. /// updated.
  646. ///
  647. /// @param decline client's Decline message
  648. /// @param duid client's duid (used to verify if the client owns the lease)
  649. /// @param general_status [out] status in top-level message (may be updated)
  650. /// @param ia specific IA_NA option to process.
  651. /// @return IA_NA option with response (to be included in Reply message)
  652. OptionPtr
  653. declineIA(const Pkt6Ptr& decline, const DuidPtr& duid, int& general_status,
  654. boost::shared_ptr<Option6IA> ia);
  655. /// @brief Declines specific IPv6 lease.
  656. ///
  657. /// This method performs the actual decline and all necessary operations:
  658. /// - cleans up DNS, if necessary
  659. /// - updates subnet[X].declined-addresses (per subnet stat)
  660. /// - updates declined-addresses (global stat)
  661. /// - disassociates client information from the lease
  662. /// - moves the lease to DECLINED state
  663. /// - sets lease expiration time to decline-probation-period
  664. /// - adds status-code success
  665. ///
  666. /// @param decline used for generating removal Name Change Request.
  667. /// @param lease lease to be declined
  668. /// @param ia_rsp response IA_NA.
  669. /// @return true when expected to continue, false when hooks told us to drop
  670. /// the packet
  671. bool declineLease(const Pkt6Ptr& decline, const Lease6Ptr lease,
  672. boost::shared_ptr<Option6IA> ia_rsp);
  673. /// @brief A simple utility method that sets the status code
  674. ///
  675. /// Removes old status code and sets a new one.
  676. /// @param container status code will be added here
  677. /// @param status status code option
  678. void setStatusCode(boost::shared_ptr<Option6IA>& container,
  679. const OptionPtr& status);
  680. private:
  681. /// @public
  682. /// @brief Assign class using vendor-class-identifier option
  683. ///
  684. /// @note This is the first part of @ref classifyPacket
  685. ///
  686. /// @param pkt packet to be classified
  687. /// @param classes a reference to added class names for logging
  688. void classifyByVendor(const Pkt6Ptr& pkt, std::string& classes);
  689. /// @private
  690. /// @brief Generate FQDN to be sent to a client if none exists.
  691. ///
  692. /// This function is meant to be called by the functions which process
  693. /// client's messages. The function should be called after a function
  694. /// which creates FQDN option for the client. This option must exist
  695. /// in the answer message specified as an argument. It must also be
  696. /// called after functions which assign leases for a client. The
  697. /// IA options being a result of lease acquisition must be appended
  698. /// to the message specified as a parameter.
  699. ///
  700. /// If the Client FQDN option being present in the message carries empty
  701. /// hostname, this function will attempt to generate hostname from the
  702. /// IPv6 address being acquired by the client. The IPv6 address is retrieved
  703. /// from the IA_NA option carried in the specified message. If multiple
  704. /// addresses are present in the particular IA_NA option or multiple IA_NA
  705. /// options exist, the first address found is selected.
  706. ///
  707. /// The IPv6 address is converted to the hostname using the following
  708. /// pattern:
  709. /// @code
  710. /// prefix-converted-ip-address.domain-name-suffix.
  711. /// @endcode
  712. /// where:
  713. /// - prefix is a configurable prefix string appended to all auto-generated
  714. /// hostnames.
  715. /// - converted-ip-address is created by replacing all colons from the IPv6
  716. /// address with hyphens.
  717. /// - domain-name-suffix is a suffix for a domain name that, together with
  718. /// the other parts, constitute the fully qualified domain name.
  719. ///
  720. /// When hostname is successfully generated, it is either used to update
  721. /// FQDN-related fields in a lease database or to update the Client FQDN
  722. /// option being sent back to the client. The lease database update is
  723. /// NOT performed if Advertise message is being processed.
  724. ///
  725. /// @param answer Message being sent to a client, which may hold IA_NA
  726. /// and Client FQDN options to be used to generate name for a client.
  727. ///
  728. /// @throw isc::Unexpected if specified message is NULL. This is treated
  729. /// as a programmatic error.
  730. void generateFqdn(const Pkt6Ptr& answer);
  731. /// @brief Updates statistics for received packets
  732. /// @param query packet received
  733. static void processStatsReceived(const Pkt6Ptr& query);
  734. /// @brief Checks if the specified option code has been requested using
  735. /// the Option Request option.
  736. ///
  737. /// @param query Pointer to the client's query.
  738. /// @parma code Option code.
  739. ///
  740. /// @return true if option has been requested in the ORO.
  741. bool requestedInORO(const Pkt6Ptr& query, const uint16_t code) const;
  742. /// UDP port number on which server listens.
  743. uint16_t port_;
  744. public:
  745. /// @note used by DHCPv4-over-DHCPv6 so must be public and static
  746. /// @brief Updates statistics for transmitted packets
  747. /// @param response packet transmitted
  748. static void processStatsSent(const Pkt6Ptr& response);
  749. /// @brief Returns the index of the buffer6_send hook
  750. /// @return the index of the buffer6_send hook
  751. static int getHookIndexBuffer6Send();
  752. protected:
  753. /// Server DUID (to be sent in server-identifier option)
  754. OptionPtr serverid_;
  755. /// Indicates if shutdown is in progress. Setting it to true will
  756. /// initiate server shutdown procedure.
  757. volatile bool shutdown_;
  758. /// @brief Allocation Engine.
  759. /// Pointer to the allocation engine that we are currently using
  760. /// It must be a pointer, because we will support changing engines
  761. /// during normal operation (e.g. to use different allocators)
  762. boost::shared_ptr<AllocEngine> alloc_engine_;
  763. /// Holds a list of @c isc::dhcp_ddns::NameChangeRequest objects, which
  764. /// are waiting for sending to kea-dhcp-ddns module.
  765. std::queue<isc::dhcp_ddns::NameChangeRequest> name_change_reqs_;
  766. };
  767. }; // namespace isc::dhcp
  768. }; // namespace isc
  769. #endif // DHCP6_SRV_H