dhcp6_srv.h 15 KB

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