dhcp6_srv.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright (C) 2011-2013 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/subnet.h>
  26. #include <hooks/callout_handle.h>
  27. #include <boost/noncopyable.hpp>
  28. #include <iostream>
  29. #include <queue>
  30. namespace isc {
  31. namespace dhcp {
  32. /// @brief DHCPv6 server service.
  33. ///
  34. /// This class represents DHCPv6 server. It contains all
  35. /// top-level methods and routines necessary for server operation.
  36. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  37. /// that is going to be used as server-identifier, receives incoming
  38. /// packets, processes them, manages leases assignment and generates
  39. /// appropriate responses.
  40. ///
  41. /// @note Only one instance of this class is instantiated as it encompasses
  42. /// the whole operation of the server. Nothing, however, enforces the
  43. /// singleton status of the object.
  44. class Dhcpv6Srv : public boost::noncopyable {
  45. public:
  46. /// @brief defines if certain option may, must or must not appear
  47. typedef enum {
  48. FORBIDDEN,
  49. MANDATORY,
  50. OPTIONAL
  51. } RequirementLevel;
  52. /// @brief Minimum length of a MAC address to be used in DUID generation.
  53. static const size_t MIN_MAC_LEN = 6;
  54. /// @brief Default constructor.
  55. ///
  56. /// Instantiates necessary services, required to run DHCPv6 server.
  57. /// In particular, creates IfaceMgr that will be responsible for
  58. /// network interaction. Will instantiate lease manager, and load
  59. /// old or create new DUID.
  60. ///
  61. /// @param port port on will all sockets will listen
  62. Dhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT);
  63. /// @brief Destructor. Used during DHCPv6 service shutdown.
  64. virtual ~Dhcpv6Srv();
  65. /// @brief Returns server-indentifier option.
  66. ///
  67. /// @return server-id option
  68. OptionPtr getServerID() { return serverid_; }
  69. /// @brief Main server processing loop.
  70. ///
  71. /// Main server processing loop. Receives incoming packets, verifies
  72. /// their correctness, generates appropriate answer (if needed) and
  73. /// transmits responses.
  74. ///
  75. /// @return true, if being shut down gracefully, fail if experienced
  76. /// critical error.
  77. bool run();
  78. /// @brief Instructs the server to shut down.
  79. void shutdown();
  80. /// @brief Get UDP port on which server should listen.
  81. ///
  82. /// Typically, server listens on UDP port 547. Other ports are only
  83. /// used for testing purposes.
  84. ///
  85. /// This accessor must be public because sockets are reopened from the
  86. /// static configuration callback handler. This callback handler invokes
  87. /// @c ControlledDhcpv4Srv::openActiveSockets which requires port parameter
  88. /// which has to be retrieved from the @c ControlledDhcpv4Srv object.
  89. /// They are retrieved using this public function.
  90. ///
  91. /// @return UDP port on which server should listen.
  92. uint16_t getPort() const {
  93. return (port_);
  94. }
  95. /// @brief Open sockets which are marked as active in @c CfgMgr.
  96. ///
  97. /// This function reopens sockets according to the current settings in the
  98. /// Configuration Manager. It holds the list of the interfaces which server
  99. /// should listen on. This function will open sockets on these interfaces
  100. /// only. This function is not exception safe.
  101. ///
  102. /// @param port UDP port on which server should listen.
  103. static void openActiveSockets(const uint16_t port);
  104. protected:
  105. /// @brief verifies if specified packet meets RFC requirements
  106. ///
  107. /// Checks if mandatory option is really there, that forbidden option
  108. /// is not there, and that client-id or server-id appears only once.
  109. ///
  110. /// @param pkt packet to be checked
  111. /// @param clientid expectation regarding client-id option
  112. /// @param serverid expectation regarding server-id option
  113. /// @throw RFCViolation if any issues are detected
  114. void sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  115. RequirementLevel serverid);
  116. /// @brief Processes incoming SOLICIT and returns response.
  117. ///
  118. /// Processes received SOLICIT message and verifies that its sender
  119. /// should be served. In particular IA, TA and PD options are populated
  120. /// with to-be assigned addresses, temporary addresses and delegated
  121. /// prefixes, respectively. In the usual 4 message exchange, server is
  122. /// expected to respond with ADVERTISE message. However, if client
  123. /// requests rapid-commit and server supports it, REPLY will be sent
  124. /// instead of ADVERTISE and requested leases will be assigned
  125. /// immediately.
  126. ///
  127. /// @param solicit SOLICIT message received from client
  128. ///
  129. /// @return ADVERTISE, REPLY message or NULL
  130. Pkt6Ptr processSolicit(const Pkt6Ptr& solicit);
  131. /// @brief Processes incoming REQUEST and returns REPLY response.
  132. ///
  133. /// Processes incoming REQUEST message and verifies that its sender
  134. /// should be served. In particular IA, TA and PD options are populated
  135. /// with assigned addresses, temporary addresses and delegated
  136. /// prefixes, respectively. Uses LeaseMgr to allocate or update existing
  137. /// leases.
  138. ///
  139. /// @param request a message received from client
  140. ///
  141. /// @return REPLY message or NULL
  142. Pkt6Ptr processRequest(const Pkt6Ptr& request);
  143. /// @brief Stub function that will handle incoming RENEW messages.
  144. ///
  145. /// @param renew message received from client
  146. Pkt6Ptr processRenew(const Pkt6Ptr& renew);
  147. /// @brief Stub function that will handle incoming REBIND messages.
  148. ///
  149. /// @param rebind message received from client
  150. Pkt6Ptr processRebind(const Pkt6Ptr& rebind);
  151. /// @brief Stub function that will handle incoming CONFIRM messages.
  152. ///
  153. /// @param confirm message received from client
  154. Pkt6Ptr processConfirm(const Pkt6Ptr& confirm);
  155. /// @brief Stub function that will handle incoming RELEASE messages.
  156. ///
  157. /// @param release message received from client
  158. Pkt6Ptr processRelease(const Pkt6Ptr& release);
  159. /// @brief Stub function that will handle incoming DECLINE messages.
  160. ///
  161. /// @param decline message received from client
  162. Pkt6Ptr processDecline(const Pkt6Ptr& decline);
  163. /// @brief Stub function that will handle incoming INF-REQUEST messages.
  164. ///
  165. /// @param infRequest message received from client
  166. Pkt6Ptr processInfRequest(const Pkt6Ptr& infRequest);
  167. /// @brief Creates status-code option.
  168. ///
  169. /// @param code status code value (see RFC3315)
  170. /// @param text textual explanation (will be sent in status code option)
  171. /// @return status-code option
  172. OptionPtr createStatusCode(uint16_t code, const std::string& text);
  173. /// @brief Selects a subnet for a given client's packet.
  174. ///
  175. /// @param question client's message
  176. /// @return selected subnet (or NULL if no suitable subnet was found)
  177. isc::dhcp::Subnet6Ptr selectSubnet(const Pkt6Ptr& question);
  178. /// @brief Processes IA_NA option (and assigns addresses if necessary).
  179. ///
  180. /// Generates response to IA_NA. This typically includes selecting (and
  181. /// allocating a lease in case of REQUEST) an address lease and creating
  182. /// IAADDR option. In case of allocation failure, it may contain
  183. /// status code option with non-zero status, denoting cause of the
  184. /// allocation failure.
  185. ///
  186. /// @param subnet subnet the client is connected to
  187. /// @param duid client's duid
  188. /// @param query client's message (typically SOLICIT or REQUEST)
  189. /// @param ia pointer to client's IA_NA option (client's request)
  190. /// @param fqdn A DHCPv6 Client FQDN %Option generated in a response to the
  191. /// FQDN option sent by a client.
  192. /// @return IA_NA option (server's response)
  193. OptionPtr assignIA_NA(const isc::dhcp::Subnet6Ptr& subnet,
  194. const isc::dhcp::DuidPtr& duid,
  195. const isc::dhcp::Pkt6Ptr& query,
  196. Option6IAPtr ia,
  197. const Option6ClientFqdnPtr& fqdn);
  198. /// @brief Processes IA_PD option (and assigns prefixes if necessary).
  199. ///
  200. /// Generates response to IA_PD. This typically includes selecting (and
  201. /// allocating a lease in case of REQUEST) a prefix lease and creating
  202. /// IAPREFIX option. In case of allocation failure, it may contain
  203. /// status code option with non-zero status, denoting cause of the
  204. /// allocation failure.
  205. ///
  206. /// @param subnet subnet the client is connected to
  207. /// @param duid client's duid
  208. /// @param query client's message (typically SOLICIT or REQUEST)
  209. /// @param ia pointer to client's IA_PD option (client's request)
  210. /// @return IA_PD option (server's response)
  211. OptionPtr assignIA_PD(const Subnet6Ptr& subnet, const DuidPtr& duid,
  212. const Pkt6Ptr& query,
  213. boost::shared_ptr<Option6IA> ia);
  214. /// @brief Renews specific IA_NA option
  215. ///
  216. /// Generates response to IA_NA in Renew. This typically includes finding a
  217. /// lease that corresponds to the received address. If no such lease is
  218. /// found, an IA_NA response is generated with an appropriate status code.
  219. ///
  220. /// @param subnet subnet the sender belongs to
  221. /// @param duid client's duid
  222. /// @param query client's message
  223. /// @param ia IA_NA option that is being renewed
  224. /// @param fqdn DHCPv6 Client FQDN Option included in the server's response
  225. /// @return IA_NA option (server's response)
  226. OptionPtr renewIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  227. const Pkt6Ptr& query, boost::shared_ptr<Option6IA> ia,
  228. const Option6ClientFqdnPtr& fqdn);
  229. /// @brief Releases specific IA_NA option
  230. ///
  231. /// Generates response to IA_NA in Release message. This covers finding and
  232. /// removal of a lease that corresponds to the received address. If no such
  233. /// lease is found, an IA_NA response is generated with an appropriate
  234. /// status code.
  235. ///
  236. /// As RFC 3315 requires that a single status code be sent for the whole message,
  237. /// this method may update the passed general_status: it is set to SUCCESS when
  238. /// message processing begins, but may be updated to some error code if the
  239. /// release process fails.
  240. ///
  241. /// @param duid client's duid
  242. /// @param query client's message
  243. /// @param general_status a global status (it may be updated in case of errors)
  244. /// @param ia IA_NA option that is being renewed
  245. /// @return IA_NA option (server's response)
  246. OptionPtr releaseIA_NA(const DuidPtr& duid, const Pkt6Ptr& query,
  247. int& general_status,
  248. boost::shared_ptr<Option6IA> ia);
  249. /// @brief Copies required options from client message to server answer.
  250. ///
  251. /// Copies options that must appear in any server response (ADVERTISE, REPLY)
  252. /// to client's messages (SOLICIT, REQUEST, RENEW, REBIND, DECLINE, RELEASE).
  253. /// One notable example is client-id. Other options may be copied as required.
  254. ///
  255. /// @param question client's message (options will be copied from here)
  256. /// @param answer server's message (options will be copied here)
  257. void copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  258. /// @brief Appends default options to server's answer.
  259. ///
  260. /// Adds required options to server's answer. In particular, server-id
  261. /// is added. Possibly other mandatory options will be added, depending
  262. /// on type (or content) of client message.
  263. ///
  264. /// @param question client's message
  265. /// @param answer server's message (options will be added here)
  266. void appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  267. /// @brief Appends requested options to server's answer.
  268. ///
  269. /// Appends options requested by client to the server's answer.
  270. ///
  271. /// @param question client's message
  272. /// @param answer server's message (options will be added here)
  273. void appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  274. /// @brief Assigns leases.
  275. ///
  276. /// It supports addresses (IA_NA) only. It does NOT support temporary
  277. /// addresses (IA_TA) nor prefixes (IA_PD).
  278. /// @todo: Extend this method once TA and PD becomes supported
  279. ///
  280. /// @param question client's message (with requested IA_NA)
  281. /// @param answer server's message (IA_NA options will be added here)
  282. /// @param fqdn an FQDN option generated in a response to the client's
  283. /// FQDN option.
  284. void assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer,
  285. const Option6ClientFqdnPtr& fqdn);
  286. /// @brief Processes Client FQDN Option.
  287. ///
  288. /// This function retrieves DHCPv6 Client FQDN %Option (if any) from the
  289. /// packet sent by a client and takes necessary actions upon this option.
  290. /// Received option comprises flags field which controls what DNS updates
  291. /// server should do. Server may override client's preference based on
  292. /// the current configuration. Server indicates that it has overridden
  293. /// the preference by storing DHCPv6 Client Fqdn %Option with the
  294. /// appropriate flags in the response to a client. This option is also
  295. /// used to communicate the client's domain-name which should be sent
  296. /// to the DNS in the update. Again, server may act upon the received
  297. /// domain-name, i.e. if the provided domain-name is partial it should
  298. /// generate the fully qualified domain-name.
  299. ///
  300. /// All the logic required to form appropriate answer to the client is
  301. /// held in this function.
  302. ///
  303. /// @param question Client's message.
  304. ///
  305. /// @return FQDN option produced in the response to the client's message.
  306. Option6ClientFqdnPtr processClientFqdn(const Pkt6Ptr& question);
  307. /// @brief Adds DHCPv6 Client FQDN %Option to the server response.
  308. ///
  309. /// This function will add the specified FQDN option into the server's
  310. /// response when FQDN is not NULL and server is either configured to
  311. /// always include the FQDN in the response or client requested it using
  312. /// %Option Request %Option.
  313. /// This function is exception safe.
  314. ///
  315. /// @param question A message received from the client.
  316. /// @param [out] answer A server's response where FQDN option will be added.
  317. /// @param fqdn A DHCPv6 Client FQDN %Option to be added to the server's
  318. /// response to a client.
  319. void appendClientFqdn(const Pkt6Ptr& question,
  320. Pkt6Ptr& answer,
  321. const Option6ClientFqdnPtr& fqdn);
  322. /// @brief Creates a number of @c isc::dhcp_ddns::NameChangeRequest objects
  323. /// based on the DHCPv6 Client FQDN %Option.
  324. ///
  325. /// The @c isc::dhcp_ddns::NameChangeRequest class encapsulates the request
  326. /// from the DHCPv6 server to the DHCP-DDNS module to perform DNS Update.
  327. /// The FQDN option carries response to the client about DNS updates that
  328. /// server intents to perform for the DNS client. Based on this, the
  329. /// function will create zero or more @c isc::dhcp_ddns::NameChangeRequest
  330. /// objects and store them in the internal queue. Requests created by this
  331. /// function are only adding or updating DNS records. In order to generate
  332. /// requests for DNS records removal, use @c createRemovalNameChangeRequest.
  333. ///
  334. /// @todo Add support for multiple IAADDR options in the IA_NA.
  335. ///
  336. /// @param answer A message beging sent to the Client.
  337. /// @param fqdn_answer A DHCPv6 Client FQDN %Option which is included in the
  338. /// response message sent to a client.
  339. void createNameChangeRequests(const Pkt6Ptr& answer,
  340. const Option6ClientFqdnPtr& fqdn_answer);
  341. /// @brief Creates a @c isc::dhcp_ddns::NameChangeRequest which requests
  342. /// removal of DNS entries for a particular lease.
  343. ///
  344. /// This function should be called upon removal of the lease from the lease
  345. /// database, i.e, when client sent Release or Decline message. It will
  346. /// create a single @c isc::dhcp_ddns::NameChangeRequest which removes the
  347. /// existing DNS records for the lease, which server is responsible for.
  348. /// Note that this function will not remove the entries which server hadn't
  349. /// added. This is the case, when client performs forward DNS update on its
  350. /// own.
  351. ///
  352. /// @param lease A lease for which the the removal of corresponding DNS
  353. /// records will be performed.
  354. void createRemovalNameChangeRequest(const Lease6Ptr& lease);
  355. /// @brief Sends all outstanding NameChangeRequests to bind10-d2 module.
  356. ///
  357. /// The purpose of this function is to pick all outstanding
  358. /// NameChangeRequests from the FIFO queue and send them to bind10-dhcp-ddns
  359. /// module.
  360. ///
  361. /// @todo Currently this function simply removes all requests from the
  362. /// queue but doesn't send them anywhere. In the future, the
  363. /// NameChangeSender will be used to deliver requests to the other module.
  364. void sendNameChangeRequests();
  365. /// @brief Attempts to renew received addresses
  366. ///
  367. /// It iterates through received IA_NA options and attempts to renew
  368. /// received addresses. If no such leases are found, proper status
  369. /// code is added to reply message. Renewed addresses are added
  370. /// as IA_NA/IAADDR to reply packet.
  371. /// @param renew client's message asking for renew
  372. /// @param reply server's response
  373. /// @param fqdn A DHCPv6 Client FQDN %Option generated in the response to the
  374. /// client's FQDN option.
  375. void renewLeases(const Pkt6Ptr& renew, Pkt6Ptr& reply,
  376. const Option6ClientFqdnPtr& fqdn);
  377. /// @brief Attempts to release received addresses
  378. ///
  379. /// It iterates through received IA_NA options and attempts to release
  380. /// received addresses. If no such leases are found, or the lease fails
  381. /// proper checks (e.g. belongs to someone else), a proper status
  382. /// code is added to reply message. Released addresses are not added
  383. /// to REPLY packet, just its IA_NA containers.
  384. /// @param release client's message asking to release
  385. /// @param reply server's response
  386. void releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply);
  387. /// @brief Sets server-identifier.
  388. ///
  389. /// This method attempts to generate server-identifier DUID. It generates a
  390. /// new DUID using interface link-layer addresses (EUI-64) + timestamp (DUID
  391. /// type duid-llt, see RFC3315, section 9.2). If there are no suitable
  392. /// interfaces present, exception it thrown
  393. ///
  394. /// @throws isc::Unexpected Failed to read DUID file and no suitable
  395. /// interfaces for new DUID generation are detected.
  396. void generateServerID();
  397. /// @brief attempts to load DUID from a file
  398. ///
  399. /// Tries to load duid from a text file. If the load is successful,
  400. /// it creates server-id option and stores it in serverid_ (to be used
  401. /// later by getServerID()).
  402. ///
  403. /// @param file_name name of the DUID file to load
  404. /// @return true if load was successful, false otherwise
  405. bool loadServerID(const std::string& file_name);
  406. /// @brief attempts to write DUID to a file
  407. /// Tries to write duid content (stored in serverid_) to a text file.
  408. ///
  409. /// @param file_name name of the DUID file to write
  410. /// @return true if write was successful, false otherwise
  411. bool writeServerID(const std::string& file_name);
  412. /// @brief converts DUID to text
  413. /// Converts content of DUID option to a text representation, e.g.
  414. /// 01:ff:02:03:06:80:90:ab:cd:ef
  415. ///
  416. /// @param opt option that contains DUID
  417. /// @return string representation
  418. static std::string duidToString(const OptionPtr& opt);
  419. /// @brief dummy wrapper around IfaceMgr::receive6
  420. ///
  421. /// This method is useful for testing purposes, where its replacement
  422. /// simulates reception of a packet. For that purpose it is protected.
  423. virtual Pkt6Ptr receivePacket(int timeout);
  424. /// @brief dummy wrapper around IfaceMgr::send()
  425. ///
  426. /// This method is useful for testing purposes, where its replacement
  427. /// simulates transmission of a packet. For that purpose it is protected.
  428. virtual void sendPacket(const Pkt6Ptr& pkt);
  429. private:
  430. /// @brief Allocation Engine.
  431. /// Pointer to the allocation engine that we are currently using
  432. /// It must be a pointer, because we will support changing engines
  433. /// during normal operation (e.g. to use different allocators)
  434. boost::shared_ptr<AllocEngine> alloc_engine_;
  435. /// Server DUID (to be sent in server-identifier option)
  436. OptionPtr serverid_;
  437. /// Indicates if shutdown is in progress. Setting it to true will
  438. /// initiate server shutdown procedure.
  439. volatile bool shutdown_;
  440. /// Indexes for registered hook points
  441. int hook_index_pkt6_receive_;
  442. int hook_index_subnet6_select_;
  443. int hook_index_pkt6_send_;
  444. /// UDP port number on which server listens.
  445. uint16_t port_;
  446. protected:
  447. /// Holds a list of @c isc::dhcp_ddns::NameChangeRequest objects, which
  448. /// are waiting for sending to b10-dhcp-ddns module.
  449. std::queue<isc::dhcp_ddns::NameChangeRequest> name_change_reqs_;
  450. };
  451. }; // namespace isc::dhcp
  452. }; // namespace isc
  453. #endif // DHCP6_SRV_H