dhcp6_srv.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <d2/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 <boost/noncopyable.hpp>
  27. #include <iostream>
  28. namespace isc {
  29. namespace dhcp {
  30. /// @brief DHCPv6 server service.
  31. ///
  32. /// This class represents DHCPv6 server. It contains all
  33. /// top-level methods and routines necessary for server operation.
  34. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  35. /// that is going to be used as server-identifier, receives incoming
  36. /// packets, processes them, manages leases assignment and generates
  37. /// appropriate responses.
  38. ///
  39. /// @note Only one instance of this class is instantiated as it encompasses
  40. /// the whole operation of the server. Nothing, however, enforces the
  41. /// singleton status of the object.
  42. class Dhcpv6Srv : public boost::noncopyable {
  43. public:
  44. /// @brief defines if certain option may, must or must not appear
  45. typedef enum {
  46. FORBIDDEN,
  47. MANDATORY,
  48. OPTIONAL
  49. } RequirementLevel;
  50. /// @brief Minimum length of a MAC address to be used in DUID generation.
  51. static const size_t MIN_MAC_LEN = 6;
  52. /// @brief Default constructor.
  53. ///
  54. /// Instantiates necessary services, required to run DHCPv6 server.
  55. /// In particular, creates IfaceMgr that will be responsible for
  56. /// network interaction. Will instantiate lease manager, and load
  57. /// old or create new DUID.
  58. ///
  59. /// @param port port on will all sockets will listen
  60. Dhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT);
  61. /// @brief Destructor. Used during DHCPv6 service shutdown.
  62. virtual ~Dhcpv6Srv();
  63. /// @brief Returns server-indentifier option.
  64. ///
  65. /// @return server-id option
  66. OptionPtr getServerID() { return serverid_; }
  67. /// @brief Main server processing loop.
  68. ///
  69. /// Main server processing loop. Receives incoming packets, verifies
  70. /// their correctness, generates appropriate answer (if needed) and
  71. /// transmits respones.
  72. ///
  73. /// @return true, if being shut down gracefully, fail if experienced
  74. /// critical error.
  75. bool run();
  76. /// @brief Instructs the server to shut down.
  77. void shutdown();
  78. protected:
  79. /// @brief verifies if specified packet meets RFC requirements
  80. ///
  81. /// Checks if mandatory option is really there, that forbidden option
  82. /// is not there, and that client-id or server-id appears only once.
  83. ///
  84. /// @param pkt packet to be checked
  85. /// @param clientid expectation regarding client-id option
  86. /// @param serverid expectation regarding server-id option
  87. /// @throw RFCViolation if any issues are detected
  88. void sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
  89. RequirementLevel serverid);
  90. /// @brief Processes incoming SOLICIT and returns response.
  91. ///
  92. /// Processes received SOLICIT message and verifies that its sender
  93. /// should be served. In particular IA, TA and PD options are populated
  94. /// with to-be assinged addresses, temporary addresses and delegated
  95. /// prefixes, respectively. In the usual 4 message exchange, server is
  96. /// expected to respond with ADVERTISE message. However, if client
  97. /// requests rapid-commit and server supports it, REPLY will be sent
  98. /// instead of ADVERTISE and requested leases will be assigned
  99. /// immediately.
  100. ///
  101. /// @param solicit SOLICIT message received from client
  102. ///
  103. /// @return ADVERTISE, REPLY message or NULL
  104. Pkt6Ptr processSolicit(const Pkt6Ptr& solicit);
  105. /// @brief Processes incoming REQUEST and returns REPLY response.
  106. ///
  107. /// Processes incoming REQUEST message and verifies that its sender
  108. /// should be served. In particular IA, TA and PD options are populated
  109. /// with assinged addresses, temporary addresses and delegated
  110. /// prefixes, respectively. Uses LeaseMgr to allocate or update existing
  111. /// leases.
  112. ///
  113. /// @param request a message received from client
  114. ///
  115. /// @return REPLY message or NULL
  116. Pkt6Ptr processRequest(const Pkt6Ptr& request,
  117. isc::d2::NameChangeRequestPtr& ncr);
  118. /// @brief Stub function that will handle incoming RENEW messages.
  119. ///
  120. /// @param renew message received from client
  121. Pkt6Ptr processRenew(const Pkt6Ptr& renew,
  122. isc::d2::NameChangeRequestPtr& ncr);
  123. /// @brief Stub function that will handle incoming REBIND messages.
  124. ///
  125. /// @param rebind message received from client
  126. Pkt6Ptr processRebind(const Pkt6Ptr& rebind,
  127. isc::d2::NameChangeRequestPtr& ncr);
  128. /// @brief Stub function that will handle incoming CONFIRM messages.
  129. ///
  130. /// @param confirm message received from client
  131. Pkt6Ptr processConfirm(const Pkt6Ptr& confirm);
  132. /// @brief Stub function that will handle incoming RELEASE messages.
  133. ///
  134. /// @param release message received from client
  135. Pkt6Ptr processRelease(const Pkt6Ptr& release,
  136. isc::d2::NameChangeRequestPtr& ncr);
  137. /// @brief Stub function that will handle incoming DECLINE messages.
  138. ///
  139. /// @param decline message received from client
  140. Pkt6Ptr processDecline(const Pkt6Ptr& decline);
  141. /// @brief Stub function that will handle incoming INF-REQUEST messages.
  142. ///
  143. /// @param infRequest message received from client
  144. Pkt6Ptr processInfRequest(const Pkt6Ptr& infRequest);
  145. /// @brief Creates status-code option.
  146. ///
  147. /// @param code status code value (see RFC3315)
  148. /// @param text textual explanation (will be sent in status code option)
  149. /// @return status-code option
  150. OptionPtr createStatusCode(uint16_t code, const std::string& text);
  151. /// @brief Selects a subnet for a given client's packet.
  152. ///
  153. /// @param question client's message
  154. /// @return selected subnet (or NULL if no suitable subnet was found)
  155. isc::dhcp::Subnet6Ptr selectSubnet(const Pkt6Ptr& question);
  156. /// @brief Processes IA_NA option (and assigns addresses if necessary).
  157. ///
  158. /// Generates response to IA_NA. This typically includes selecting (and
  159. /// allocating a lease in case of REQUEST) a lease and creating
  160. /// IAADDR option. In case of allocation failure, it may contain
  161. /// status code option with non-zero status, denoting cause of the
  162. /// allocation failure.
  163. ///
  164. /// @param subnet subnet the client is connected to
  165. /// @param duid client's duid
  166. /// @param question client's message (typically SOLICIT or REQUEST)
  167. /// @param ia pointer to client's IA_NA option (client's request)
  168. /// @return IA_NA option (server's response)
  169. OptionPtr assignIA_NA(const isc::dhcp::Subnet6Ptr& subnet,
  170. const isc::dhcp::DuidPtr& duid,
  171. isc::dhcp::Pkt6Ptr question,
  172. boost::shared_ptr<Option6IA> ia);
  173. /// @brief Renews specific IA_NA option
  174. ///
  175. /// Generates response to IA_NA in Renew. This typically includes finding a
  176. /// lease that corresponds to the received address. If no such lease is
  177. /// found, an IA_NA response is generated with an appropriate status code.
  178. ///
  179. /// @param subnet subnet the sender belongs to
  180. /// @param duid client's duid
  181. /// @param question client's message
  182. /// @param ia IA_NA option that is being renewed
  183. /// @return IA_NA option (server's response)
  184. OptionPtr renewIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
  185. Pkt6Ptr question, boost::shared_ptr<Option6IA> ia);
  186. /// @brief Releases specific IA_NA option
  187. ///
  188. /// Generates response to IA_NA in Release message. This covers finding and
  189. /// removal of a lease that corresponds to the received address. If no such
  190. /// lease is found, an IA_NA response is generated with an appropriate
  191. /// status code.
  192. ///
  193. /// As RFC 3315 requires that a single status code be sent for the whole message,
  194. /// this method may update the passed general_status: it is set to SUCCESS when
  195. /// message processing begins, but may be updated to some error code if the
  196. /// release process fails.
  197. ///
  198. /// @param duid client's duid
  199. /// @param question client's message
  200. /// @param general_status a global status (it may be updated in case of errors)
  201. /// @param ia IA_NA option that is being renewed
  202. /// @return IA_NA option (server's response)
  203. OptionPtr releaseIA_NA(const DuidPtr& duid, Pkt6Ptr question,
  204. int& general_status,
  205. boost::shared_ptr<Option6IA> ia);
  206. /// @brief Copies required options from client message to server answer.
  207. ///
  208. /// Copies options that must appear in any server response (ADVERTISE, REPLY)
  209. /// to client's messages (SOLICIT, REQUEST, RENEW, REBIND, DECLINE, RELEASE).
  210. /// One notable example is client-id. Other options may be copied as required.
  211. ///
  212. /// @param question client's message (options will be copied from here)
  213. /// @param answer server's message (options will be copied here)
  214. void copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  215. /// @brief Appends default options to server's answer.
  216. ///
  217. /// Adds required options to server's answer. In particular, server-id
  218. /// is added. Possibly other mandatory options will be added, depending
  219. /// on type (or content) of client message.
  220. ///
  221. /// @param question client's message
  222. /// @param answer server's message (options will be added here)
  223. void appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  224. /// @brief Appends requested options to server's answer.
  225. ///
  226. /// Appends options requested by client to the server's answer.
  227. ///
  228. /// @param question client's message
  229. /// @param answer server's message (options will be added here)
  230. void appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  231. /// @brief Assigns leases.
  232. ///
  233. /// It supports addresses (IA_NA) only. It does NOT support temporary
  234. /// addresses (IA_TA) nor prefixes (IA_PD).
  235. /// @todo: Extend this method once TA and PD becomes supported
  236. ///
  237. /// @param question client's message (with requested IA_NA)
  238. /// @param answer server's message (IA_NA options will be added here)
  239. void assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer);
  240. /// @brief Processes Client FQDN Option.
  241. ///
  242. /// This function retrieves DHCPv6 Client FQDN %Option (if any) from the
  243. /// packet sent by a client and takes necessary actions upon this option.
  244. /// Received option comprises flags field which controls what DNS updates
  245. /// server should do. Server may override client's preference based on
  246. /// the current configuration. Server indicates that it has overridden
  247. /// the preference by storing DHCPv6 Client Fqdn %Option with the
  248. /// appropriate flags in the response to a client. This option is also
  249. /// used to communicate the client's domain-name which should be sent
  250. /// to the DNS in the update. Again, server may act upon the received
  251. /// domain-name, i.e. if the provided domain-name is partial it should
  252. /// generate the fully qualified domain-name.
  253. ///
  254. /// All the logic required to form appropriate answer to the client is
  255. /// held in this function.
  256. ///
  257. /// @param question Client's message.
  258. /// @param answer Server's response to the client.
  259. void processClientFqdn(const Pkt6Ptr& question, Pkt6Ptr& answer,
  260. d2::NameChangeRequestPtr& ncr);
  261. /// @brief Creates a @c isc::d2::NameChangeRequest based on the DHCPv6
  262. /// Client FQDN %Option stored in the response to the client.
  263. ///
  264. /// The @c isc:d2::NameChangeRequest class encapsulates the request from
  265. /// the DHCPv6 server to the DHCP-DDNS module to perform DNS Update.
  266. ///
  267. /// @param answer A response being sent to a client.
  268. /// @param fqdn_answer A DHCPv6 Client FQDN %Option which is included in the
  269. /// response message sent to a client.
  270. /// @param [out] ncr A @c isc::d2::NameChangeRequest object to be sent to
  271. /// the DHCP-DDNS module as a result of the Client FQDN %Option processing.
  272. void createNameChangeRequest(const Pkt6Ptr& answer,
  273. const Option6ClientFqdnPtr& fqdn_answer,
  274. isc::d2::NameChangeRequestPtr& ncr);
  275. /// @brief Attempts to renew received addresses
  276. ///
  277. /// It iterates through received IA_NA options and attempts to renew
  278. /// received addresses. If no such leases are found, proper status
  279. /// code is added to reply message. Renewed addresses are added
  280. /// as IA_NA/IAADDR to reply packet.
  281. /// @param renew client's message asking for renew
  282. /// @param reply server's response
  283. void renewLeases(const Pkt6Ptr& renew, Pkt6Ptr& reply);
  284. /// @brief Attempts to release received addresses
  285. ///
  286. /// It iterates through received IA_NA options and attempts to release
  287. /// received addresses. If no such leases are found, or the lease fails
  288. /// proper checks (e.g. belongs to someone else), a proper status
  289. /// code is added to reply message. Released addresses are not added
  290. /// to REPLY packet, just its IA_NA containers.
  291. /// @param release client's message asking to release
  292. /// @param reply server's response
  293. void releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply);
  294. /// @brief Sets server-identifier.
  295. ///
  296. /// This method attempts to generate server-identifier DUID. It generates a
  297. /// new DUID using interface link-layer addresses (EUI-64) + timestamp (DUID
  298. /// type duid-llt, see RFC3315, section 9.2). If there are no suitable
  299. /// interfaces present, exception it thrown
  300. ///
  301. /// @throws isc::Unexpected Failed to read DUID file and no suitable
  302. /// interfaces for new DUID generation are detected.
  303. void generateServerID();
  304. /// @brief attempts to load DUID from a file
  305. ///
  306. /// Tries to load duid from a text file. If the load is successful,
  307. /// it creates server-id option and stores it in serverid_ (to be used
  308. /// later by getServerID()).
  309. ///
  310. /// @param file_name name of the DUID file to load
  311. /// @return true if load was successful, false otherwise
  312. bool loadServerID(const std::string& file_name);
  313. /// @brief attempts to write DUID to a file
  314. /// Tries to write duid content (stored in serverid_) to a text file.
  315. ///
  316. /// @param file_name name of the DUID file to write
  317. /// @return true if write was successful, false otherwise
  318. bool writeServerID(const std::string& file_name);
  319. /// @brief converts DUID to text
  320. /// Converts content of DUID option to a text representation, e.g.
  321. /// 01:ff:02:03:06:80:90:ab:cd:ef
  322. ///
  323. /// @param opt option that contains DUID
  324. /// @return string representation
  325. static std::string duidToString(const OptionPtr& opt);
  326. private:
  327. /// @brief Allocation Engine.
  328. /// Pointer to the allocation engine that we are currently using
  329. /// It must be a pointer, because we will support changing engines
  330. /// during normal operation (e.g. to use different allocators)
  331. boost::shared_ptr<AllocEngine> alloc_engine_;
  332. /// Server DUID (to be sent in server-identifier option)
  333. OptionPtr serverid_;
  334. /// Indicates if shutdown is in progress. Setting it to true will
  335. /// initiate server shutdown procedure.
  336. volatile bool shutdown_;
  337. };
  338. }; // namespace isc::dhcp
  339. }; // namespace isc
  340. #endif // DHCP6_SRV_H