dhcp6_srv.h 14 KB

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