dhcp6_srv.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 file name of a server-id file
  29. ///
  30. /// Server must store its duid in persistent storage that must not change
  31. /// between restarts. This is name of the file that is created in dataDir
  32. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  33. /// double digit hex values separated by colons format, e.g.
  34. /// 01:ff:02:03:06:80:90:ab:cd:ef. Server will create it during first
  35. /// run and then use it afterwards.
  36. static const char* SERVER_DUID_FILE = "b10-dhcp6-serverid";
  37. /// @brief DHCPv6 server service.
  38. ///
  39. /// This class represents DHCPv6 server. It contains all
  40. /// top-level methods and routines necessary for server operation.
  41. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  42. /// that is going to be used as server-identifier, receives incoming
  43. /// packets, processes them, manages leases assignment and generates
  44. /// appropriate responses.
  45. ///
  46. /// @note Only one instance of this class is instantated as it encompasses
  47. /// the whole operation of the server. Nothing, however, enforces the
  48. /// singleton status of the object.
  49. class Dhcpv6Srv : public boost::noncopyable {
  50. public:
  51. /// @brief defines if certain option may, must or must not appear
  52. typedef enum {
  53. FORBIDDEN,
  54. MANDATORY,
  55. OPTIONAL
  56. } RequirementLevel;
  57. /// @brief Minimum length of a MAC address to be used in DUID generation.
  58. static const size_t MIN_MAC_LEN = 6;
  59. /// @brief Default constructor.
  60. ///
  61. /// Instantiates necessary services, required to run DHCPv6 server.
  62. /// In particular, creates IfaceMgr that will be responsible for
  63. /// network interaction. Will instantiate lease manager, and load
  64. /// old or create new DUID.
  65. ///
  66. /// @param port port on will all sockets will listen
  67. /// @param dbconfig Lease manager configuration string. The default
  68. /// of the "memfile" manager is used for testing.
  69. Dhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT,
  70. const char* dbconfig = "type=memfile");
  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 generate server-identifier DUID. It generates a
  268. /// new DUID using interface link-layer addresses (EUI-64) + timestamp (DUID
  269. /// type duid-llt, see RFC3315, section 9.2). If there are no suitable
  270. /// interfaces present, exception it thrown
  271. ///
  272. /// @throws isc::Unexpected Failed to read DUID file and no suitable
  273. /// interfaces for new DUID generation are detected.
  274. void generateServerID();
  275. /// @brief attempts to load DUID from a file
  276. ///
  277. /// Tries to load duid from a text file. If the load is successful,
  278. /// it creates server-id option and stores it in serverid_ (to be used
  279. /// later by getServerID()).
  280. ///
  281. /// @param file_name name of the DUID file to load
  282. /// @return true if load was successful, false otherwise
  283. bool loadServerID(const std::string& file_name);
  284. /// @brief attempts to write DUID to a file
  285. /// Tries to write duid content (stored in serverid_) to a text file.
  286. ///
  287. /// @param file_name name of the DUID file to write
  288. /// @return true if write was successful, false otherwise
  289. bool writeServerID(const std::string& file_name);
  290. /// @brief converts DUID to text
  291. /// Converts content of DUID option to a text representation, e.g.
  292. /// 01:ff:02:03:06:80:90:ab:cd:ef
  293. ///
  294. /// @param opt option that contains DUID
  295. /// @return string representation
  296. static std::string duidToString(const OptionPtr& opt);
  297. private:
  298. /// @brief Allocation Engine.
  299. /// Pointer to the allocation engine that we are currently using
  300. /// It must be a pointer, because we will support changing engines
  301. /// during normal operation (e.g. to use different allocators)
  302. boost::shared_ptr<AllocEngine> alloc_engine_;
  303. /// Server DUID (to be sent in server-identifier option)
  304. OptionPtr serverid_;
  305. /// Indicates if shutdown is in progress. Setting it to true will
  306. /// initiate server shutdown procedure.
  307. volatile bool shutdown_;
  308. };
  309. }; // namespace isc::dhcp
  310. }; // namespace isc
  311. #endif // DHCP6_SRV_H