dhcp6_srv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright (C) 2011-2012 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/dhcp6.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option6_ia.h>
  20. #include <dhcp/option_definition.h>
  21. #include <dhcp/pkt6.h>
  22. #include <dhcpsrv/alloc_engine.h>
  23. #include <dhcpsrv/subnet.h>
  24. #include <boost/noncopyable.hpp>
  25. #include <iostream>
  26. namespace isc {
  27. namespace dhcp {
  28. /// @brief DHCPv6 server service.
  29. ///
  30. /// This class represents DHCPv6 server. It contains all
  31. /// top-level methods and routines necessary for server operation.
  32. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  33. /// that is going to be used as server-identifier, receives incoming
  34. /// packets, processes them, manages leases assignment and generates
  35. /// appropriate responses.
  36. ///
  37. /// @note Only one instance of this class is instantated as it encompasses
  38. /// the whole operation of the server. Nothing, however, enforces the
  39. /// singleton status of the object.
  40. class Dhcpv6Srv : public boost::noncopyable {
  41. public:
  42. /// @brief Minimum length of a MAC address to be used in DUID generation.
  43. static const size_t MIN_MAC_LEN = 6;
  44. /// @brief Default constructor.
  45. ///
  46. /// Instantiates necessary services, required to run DHCPv6 server.
  47. /// In particular, creates IfaceMgr that will be responsible for
  48. /// network interaction. Will instantiate lease manager, and load
  49. /// old or create new DUID.
  50. ///
  51. /// @param port port on will all sockets will listen
  52. /// @param dbconfig Lease manager configuration string. The default
  53. /// of the "memfile" manager is used for testing.
  54. Dhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT,
  55. const char* dbconfig = "type=memfile");
  56. /// @brief Destructor. Used during DHCPv6 service shutdown.
  57. virtual ~Dhcpv6Srv();
  58. /// @brief Returns server-intentifier option.
  59. ///
  60. /// @return server-id option
  61. OptionPtr getServerID() { return serverid_; }
  62. /// @brief Main server processing loop.
  63. ///
  64. /// Main server processing loop. Receives incoming packets, verifies
  65. /// their correctness, generates appropriate answer (if needed) and
  66. /// transmits respones.
  67. ///
  68. /// @return true, if being shut down gracefully, fail if experienced
  69. /// critical error.
  70. bool run();
  71. /// @brief Instructs the server to shut down.
  72. void shutdown();
  73. /// @brief Return textual type of packet received by server.
  74. ///
  75. /// Returns the name of valid packet received by the server (e.g. SOLICIT).
  76. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  77. /// expected to be received by the server (such as an ADVERTISE), the string
  78. /// "UNKNOWN" is returned. This method is used in debug messages.
  79. ///
  80. /// As the operation of the method does not depend on any server state, it
  81. /// is declared static.
  82. ///
  83. /// @param type DHCPv4 packet type
  84. ///
  85. /// @return Pointer to "const" string containing the packet name.
  86. /// Note that this string is statically allocated and MUST NOT
  87. /// be freed by the caller.
  88. static const char* serverReceivedPacketName(uint8_t type);
  89. protected:
  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. /// @brief Stub function that will handle incoming RENEW messages.
  118. ///
  119. /// @param renew message received from client
  120. Pkt6Ptr processRenew(const Pkt6Ptr& renew);
  121. /// @brief Stub function that will handle incoming REBIND messages.
  122. ///
  123. /// @param rebind message received from client
  124. Pkt6Ptr processRebind(const Pkt6Ptr& rebind);
  125. /// @brief Stub function that will handle incoming CONFIRM messages.
  126. ///
  127. /// @param confirm message received from client
  128. Pkt6Ptr processConfirm(const Pkt6Ptr& confirm);
  129. /// @brief Stub function that will handle incoming RELEASE messages.
  130. ///
  131. /// @param release message received from client
  132. Pkt6Ptr processRelease(const Pkt6Ptr& release);
  133. /// @brief Stub function that will handle incoming DECLINE messages.
  134. ///
  135. /// @param decline message received from client
  136. Pkt6Ptr processDecline(const Pkt6Ptr& decline);
  137. /// @brief Stub function that will handle incoming INF-REQUEST messages.
  138. ///
  139. /// @param infRequest message received from client
  140. Pkt6Ptr processInfRequest(const Pkt6Ptr& infRequest);
  141. /// @brief Creates status-code option.
  142. ///
  143. /// @param code status code value (see RFC3315)
  144. /// @param text textual explanation (will be sent in status code option)
  145. /// @return status-code option
  146. OptionPtr createStatusCode(uint16_t code, const std::string& text);
  147. /// @brief Selects a subnet for a given client's packet.
  148. ///
  149. /// @param question client's message
  150. /// @return selected subnet (or NULL if no suitable subnet was found)
  151. isc::dhcp::Subnet6Ptr selectSubnet(const Pkt6Ptr& question);
  152. /// @brief Processes IA_NA option (and assigns addresses if necessary).
  153. ///
  154. /// Generates response to IA_NA. This typically includes selecting (and
  155. /// allocating a lease in case of REQUEST) a lease and creating
  156. /// IAADDR option. In case of allocation failure, it may contain
  157. /// status code option with non-zero status, denoting cause of the
  158. /// allocation failure.
  159. ///
  160. /// @param subnet subnet the client is connected to
  161. /// @param duid client's duid
  162. /// @param question client's message (typically SOLICIT or REQUEST)
  163. /// @param ia pointer to client's IA_NA option (client's request)
  164. /// @return IA_NA option (server's response)
  165. OptionPtr handleIA_NA(const isc::dhcp::Subnet6Ptr& subnet,
  166. const isc::dhcp::DuidPtr& duid,
  167. isc::dhcp::Pkt6Ptr question,
  168. boost::shared_ptr<Option6IA> ia);
  169. /// @brief Copies required options from client message to server answer.
  170. ///
  171. /// Copies options that must appear in any server response (ADVERTISE, REPLY)
  172. /// to client's messages (SOLICIT, REQUEST, RENEW, REBIND, DECLINE, RELEASE).
  173. /// One notable example is client-id. Other options may be copied as required.
  174. ///
  175. /// @param question client's message (options will be copied from here)
  176. /// @param answer server's message (options will be copied here)
  177. void copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  178. /// @brief Appends default options to server's answer.
  179. ///
  180. /// Adds required options to server's answer. In particular, server-id
  181. /// is added. Possibly other mandatory options will be added, depending
  182. /// on type (or content) of client message.
  183. ///
  184. /// @param question client's message
  185. /// @param answer server's message (options will be added here)
  186. void appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  187. /// @brief Appends requested options to server's answer.
  188. ///
  189. /// Appends options requested by client to the server's answer.
  190. ///
  191. /// @param question client's message
  192. /// @param answer server's message (options will be added here)
  193. void appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
  194. /// @brief Assigns leases.
  195. ///
  196. /// TODO: This method is currently a stub. It just appends one
  197. /// hardcoded lease. It supports addresses (IA_NA) only. It does NOT
  198. /// support temporary addresses (IA_TA) nor prefixes (IA_PD).
  199. ///
  200. /// @param question client's message (with requested IA_NA)
  201. /// @param answer server's message (IA_NA options will be added here)
  202. void assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer);
  203. /// @brief Sets server-identifier.
  204. ///
  205. /// This method attempts to set server-identifier DUID. It loads it
  206. /// from a file. If file load fails, it generates new DUID using
  207. /// interface link-layer addresses (EUI-64) + timestamp (DUID type
  208. /// duid-llt, see RFC3315, section 9.2). If there are no suitable
  209. /// interfaces present, exception it thrown
  210. ///
  211. /// @throws isc::Unexpected Failed to read DUID file and no suitable
  212. /// interfaces for new DUID generation are detected.
  213. void setServerID();
  214. private:
  215. /// @brief Allocation Engine.
  216. /// Pointer to the allocation engine that we are currently using
  217. /// It must be a pointer, because we will support changing engines
  218. /// during normal operation (e.g. to use different allocators)
  219. boost::shared_ptr<AllocEngine> alloc_engine_;
  220. /// Server DUID (to be sent in server-identifier option)
  221. boost::shared_ptr<isc::dhcp::Option> serverid_;
  222. /// Indicates if shutdown is in progress. Setting it to true will
  223. /// initiate server shutdown procedure.
  224. volatile bool shutdown_;
  225. };
  226. }; // namespace isc::dhcp
  227. }; // namespace isc
  228. #endif // DHCP6_SRV_H