dhcp6_srv.h 10 KB

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