dhcp6_srv.h 8.5 KB

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