alloc_engine.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // Copyright (C) 2012-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 ALLOC_ENGINE_H
  15. #define ALLOC_ENGINE_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/hwaddr.h>
  19. #include <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/lease_mgr.h>
  21. #include <hooks/callout_handle.h>
  22. #include <boost/shared_ptr.hpp>
  23. #include <boost/noncopyable.hpp>
  24. namespace isc {
  25. namespace dhcp {
  26. /// An exception that is thrown when allocation module fails (e.g. due to
  27. /// lack of available addresses)
  28. class AllocFailed : public isc::Exception {
  29. public:
  30. /// @brief constructor
  31. ///
  32. /// @param file name of the file, where exception occurred
  33. /// @param line line of the file, where exception occurred
  34. /// @param what text description of the issue that caused exception
  35. AllocFailed(const char* file, size_t line, const char* what)
  36. : isc::Exception(file, line, what) {}
  37. };
  38. /// @brief DHCPv4 and DHCPv6 allocation engine
  39. ///
  40. /// This class represents DHCP allocation engine. It is responsible
  41. /// for picking subnets, choosing and allocating a lease, extending,
  42. /// renewing, releasing and possibly expiring leases.
  43. ///
  44. /// @todo: Does not handle out of leases well
  45. /// @todo: Does not handle out of allocation attempts well
  46. class AllocEngine : public boost::noncopyable {
  47. protected:
  48. /// @brief base class for all address/prefix allocation algorithms
  49. ///
  50. /// This is an abstract class that should not be used directly, but rather
  51. /// specialized implementations should be used instead.
  52. class Allocator {
  53. public:
  54. /// @brief picks one address out of available pools in a given subnet
  55. ///
  56. /// This method returns one address from the available pools in the
  57. /// specified subnet. It should not check if the address is used or
  58. /// reserved - AllocEngine will check that and will call pickAddress
  59. /// again if necessary. The number of times this method is called will
  60. /// increase as the number of available leases will decrease.
  61. ///
  62. /// @param subnet next address will be returned from pool of that subnet
  63. /// @param duid Client's DUID
  64. /// @param hint client's hint
  65. ///
  66. /// @return the next address
  67. virtual isc::asiolink::IOAddress
  68. pickAddress(const SubnetPtr& subnet, const DuidPtr& duid,
  69. const isc::asiolink::IOAddress& hint) = 0;
  70. /// @brief virtual destructor
  71. virtual ~Allocator() {
  72. }
  73. protected:
  74. };
  75. /// @brief Address/prefix allocator that iterates over all addresses
  76. ///
  77. /// This class implements iterative algorithm that returns all addresses in
  78. /// a pool iteratively, one after another. Once the last address is reached,
  79. /// it starts allocating from the beginning of the first pool (i.e. it loops
  80. /// over).
  81. class IterativeAllocator : public Allocator {
  82. public:
  83. /// @brief default constructor
  84. ///
  85. /// Does not do anything
  86. IterativeAllocator();
  87. /// @brief returns the next address from pools in a subnet
  88. ///
  89. /// @param subnet next address will be returned from pool of that subnet
  90. /// @param duid Client's DUID (ignored)
  91. /// @param hint client's hint (ignored)
  92. /// @return the next address
  93. virtual isc::asiolink::IOAddress
  94. pickAddress(const SubnetPtr& subnet,
  95. const DuidPtr& duid,
  96. const isc::asiolink::IOAddress& hint);
  97. private:
  98. /// @brief returns an address by one
  99. /// @param addr address to be increased
  100. /// @return address increased by one
  101. isc::asiolink::IOAddress increaseAddress(const isc::asiolink::IOAddress& addr);
  102. };
  103. /// @brief Address/prefix allocator that gets an address based on a hash
  104. ///
  105. /// @todo: This is a skeleton class for now and is missing implementation.
  106. class HashedAllocator : public Allocator {
  107. public:
  108. /// @brief default constructor (does nothing)
  109. HashedAllocator();
  110. /// @brief returns an address based on hash calculated from client's DUID.
  111. ///
  112. /// @todo: Implement this method
  113. ///
  114. /// @param subnet an address will be picked from pool of that subnet
  115. /// @param duid Client's DUID
  116. /// @param hint a hint (last address that was picked)
  117. /// @return selected address
  118. virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr& subnet,
  119. const DuidPtr& duid,
  120. const isc::asiolink::IOAddress& hint);
  121. };
  122. /// @brief Random allocator that picks address randomly
  123. ///
  124. /// @todo: This is a skeleton class for now and is missing implementation.
  125. class RandomAllocator : public Allocator {
  126. public:
  127. /// @brief default constructor (does nothing)
  128. RandomAllocator();
  129. /// @brief returns an random address from pool of specified subnet
  130. ///
  131. /// @todo: Implement this method
  132. ///
  133. /// @param subnet an address will be picked from pool of that subnet
  134. /// @param duid Client's DUID (ignored)
  135. /// @param hint the last address that was picked (ignored)
  136. /// @return a random address from the pool
  137. virtual isc::asiolink::IOAddress
  138. pickAddress(const SubnetPtr& subnet, const DuidPtr& duid,
  139. const isc::asiolink::IOAddress& hint);
  140. };
  141. public:
  142. /// @brief specifies allocation type
  143. typedef enum {
  144. ALLOC_ITERATIVE, // iterative - one address after another
  145. ALLOC_HASHED, // hashed - client's DUID/client-id is hashed
  146. ALLOC_RANDOM // random - an address is randomly selected
  147. } AllocType;
  148. /// @brief Default constructor.
  149. ///
  150. /// Instantiates necessary services, required to run DHCPv6 server.
  151. /// In particular, creates IfaceMgr that will be responsible for
  152. /// network interaction. Will instantiate lease manager, and load
  153. /// old or create new DUID.
  154. ///
  155. /// @param engine_type selects allocation algorithm
  156. /// @param attempts number of attempts for each lease allocation before
  157. /// we give up (0 means unlimited)
  158. AllocEngine(AllocType engine_type, unsigned int attempts);
  159. /// @brief Returns IPv4 lease.
  160. ///
  161. /// This method finds the appropriate lease for the client using the
  162. /// following algorithm:
  163. /// - If lease exists for the combination of the HW address, client id and
  164. /// subnet, try to renew a lease and return it.
  165. /// - If lease exists for the combination of the client id and subnet, try
  166. /// to renew the lease and return it.
  167. /// - If client supplied an address hint and this address is available,
  168. /// allocate the new lease with this address.
  169. /// - If client supplied an address hint and the lease for this address
  170. /// exists in the database, return this lease if it is expired.
  171. /// - Pick new address from the pool and try to allocate it for the client,
  172. /// if expired lease exists for the picked address, try to reuse this lease.
  173. ///
  174. /// When a server should do DNS updates, it is required that allocation
  175. /// returns the information how the lease was obtained by the allocation
  176. /// engine. In particular, the DHCP server should be able to check whether
  177. /// existing lease was returned, or new lease was allocated. When existing
  178. /// lease was returned, server should check whether the FQDN has changed
  179. /// between the allocation of the old and new lease. If so, server should
  180. /// perform appropriate DNS update. If not, server may choose to not
  181. /// perform the update. The information about the old lease is returned via
  182. /// @c old_lease parameter. If NULL value is returned, it is an indication
  183. /// that new lease was allocated for the client. If non-NULL value is
  184. /// returned, it is an indication that allocation engine reused/renewed an
  185. /// existing lease.
  186. ///
  187. /// @param subnet subnet the allocation should come from
  188. /// @param clientid Client identifier
  189. /// @param hwaddr Client's hardware address info
  190. /// @param hint A hint that the client provided
  191. /// @param fwd_dns_update Indicates whether forward DNS update will be
  192. /// performed for the client (true) or not (false).
  193. /// @param rev_dns_update Indicates whether reverse DNS update will be
  194. /// performed for the client (true) or not (false).
  195. /// @param hostname A string carrying hostname to be used for DNS updates.
  196. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  197. /// an address for DISCOVER that is not really allocated (true)
  198. /// @param callout_handle A callout handle (used in hooks). A lease callouts
  199. /// will be executed if this parameter is passed.
  200. /// @param [out] old_lease Holds the pointer to a previous instance of a
  201. /// lease. The NULL pointer indicates that lease didn't exist prior
  202. /// to calling this function (e.g. new lease has been allocated).
  203. ///
  204. /// @return Allocated IPv4 lease (or NULL if allocation failed)
  205. Lease4Ptr
  206. allocateAddress4(const SubnetPtr& subnet,
  207. const ClientIdPtr& clientid,
  208. const HWAddrPtr& hwaddr,
  209. const isc::asiolink::IOAddress& hint,
  210. const bool fwd_dns_update,
  211. const bool rev_dns_update,
  212. const std::string& hostname,
  213. bool fake_allocation,
  214. const isc::hooks::CalloutHandlePtr& callout_handle,
  215. Lease4Ptr& old_lease);
  216. /// @brief Renews a IPv4 lease
  217. ///
  218. /// Since both request and renew are implemented in DHCPv4 as the sending of
  219. /// a REQUEST packet, it is difficult to easily distinguish between those
  220. /// cases. Therefore renew for DHCPv4 is done in the allocation engine.
  221. /// This method is also used when client crashed/rebooted and tries
  222. /// to get a new lease. It thinks that it gets a new lease, but in fact
  223. /// we are only renewing the still valid lease for that client.
  224. ///
  225. /// @param subnet A subnet the client is attached to
  226. /// @param clientid Client identifier
  227. /// @param hwaddr Client's hardware address
  228. /// @param fwd_dns_update Indicates whether forward DNS update will be
  229. /// performed for the client (true) or not (false).
  230. /// @param rev_dns_update Indicates whether reverse DNS update will be
  231. /// performed for the client (true) or not (false).
  232. /// @param hostname A string carrying hostname to be used for DNS updates.
  233. /// @param lease A lease to be renewed
  234. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  235. /// will be executed if this parameter is passed.
  236. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  237. /// an address for DISCOVER that is not really allocated (true)
  238. Lease4Ptr
  239. renewLease4(const SubnetPtr& subnet,
  240. const ClientIdPtr& clientid,
  241. const HWAddrPtr& hwaddr,
  242. const bool fwd_dns_update,
  243. const bool rev_dns_update,
  244. const std::string& hostname,
  245. const Lease4Ptr& lease,
  246. const isc::hooks::CalloutHandlePtr& callout_handle,
  247. bool fake_allocation /* = false */);
  248. /// @brief Allocates an IPv6 lease
  249. ///
  250. /// This method uses currently selected allocator to pick an address from
  251. /// specified subnet, creates a lease for that address and then inserts
  252. /// it into LeaseMgr (if this allocation is not fake).
  253. ///
  254. /// @param subnet subnet the allocation should come from
  255. /// @param duid Client's DUID
  256. /// @param iaid iaid field from the IA_NA container that client sent
  257. /// @param hint a hint that the client provided
  258. /// @param fwd_dns_update A boolean value which indicates that server takes
  259. /// responsibility for the forward DNS Update for this lease
  260. /// (if true).
  261. /// @param rev_dns_update A boolean value which indicates that server takes
  262. /// responsibility for the reverse DNS Update for this lease
  263. /// (if true).
  264. /// @param hostname A fully qualified domain-name of the client.
  265. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  266. /// an address for SOLICIT that is not really allocated (true)
  267. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  268. /// will be executed if this parameter is passed.
  269. ///
  270. /// @return Allocated IPv6 leases (may be empty if allocation failed)
  271. Lease6Collection
  272. allocateAddress6(const Subnet6Ptr& subnet,
  273. const DuidPtr& duid,
  274. uint32_t iaid,
  275. const isc::asiolink::IOAddress& hint,
  276. const bool fwd_dns_update,
  277. const bool rev_dns_update,
  278. const std::string& hostname,
  279. bool fake_allocation,
  280. const isc::hooks::CalloutHandlePtr& callout_handle);
  281. /// @brief Destructor. Used during DHCPv6 service shutdown.
  282. virtual ~AllocEngine();
  283. private:
  284. /// @brief Creates a lease and inserts it in LeaseMgr if necessary
  285. ///
  286. /// Creates a lease based on specified parameters and tries to insert it
  287. /// into the database. That may fail in some cases, e.g. when there is another
  288. /// allocation process and we lost a race to a specific lease.
  289. ///
  290. /// @param subnet Subnet the lease is allocated from
  291. /// @param clientid Client identifier
  292. /// @param hwaddr Client's hardware address
  293. /// @param addr An address that was selected and is confirmed to be available
  294. /// @param fwd_dns_update Indicates whether forward DNS update will be
  295. /// performed for the client (true) or not (false).
  296. /// @param rev_dns_update Indicates whether reverse DNS update will be
  297. /// performed for the client (true) or not (false).
  298. /// @param hostname A string carrying hostname to be used for DNS updates.
  299. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  300. /// will be executed if this parameter is passed (and there are callouts
  301. /// registered)
  302. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  303. /// an address for DISCOVER that is not really allocated (true)
  304. /// @return allocated lease (or NULL in the unlikely case of the lease just
  305. /// becomed unavailable)
  306. Lease4Ptr createLease4(const SubnetPtr& subnet, const DuidPtr& clientid,
  307. const HWAddrPtr& hwaddr,
  308. const isc::asiolink::IOAddress& addr,
  309. const bool fwd_dns_update,
  310. const bool rev_dns_update,
  311. const std::string& hostname,
  312. const isc::hooks::CalloutHandlePtr& callout_handle,
  313. bool fake_allocation = false);
  314. /// @brief creates a lease and inserts it in LeaseMgr if necessary
  315. ///
  316. /// Creates a lease based on specified parameters and tries to insert it
  317. /// into the database. That may fail in some cases, i.e. when there is another
  318. /// allocation process and we lost a race to a specific lease.
  319. ///
  320. /// @param subnet subnet the lease is allocated from
  321. /// @param duid client's DUID
  322. /// @param iaid IAID from the IA_NA container the client sent to us
  323. /// @param addr an address that was selected and is confirmed to be
  324. /// available
  325. /// @param fwd_dns_update A boolean value which indicates that server takes
  326. /// responsibility for the forward DNS Update for this lease
  327. /// (if true).
  328. /// @param rev_dns_update A boolean value which indicates that server takes
  329. /// responsibility for the reverse DNS Update for this lease
  330. /// (if true).
  331. /// @param hostname A fully qualified domain-name of the client.
  332. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  333. /// will be executed if this parameter is passed (and there are callouts
  334. /// registered)
  335. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  336. /// an address for SOLICIT that is not really allocated (true)
  337. /// @return allocated lease (or NULL in the unlikely case of the lease just
  338. /// became unavailable)
  339. Lease6Ptr createLease6(const Subnet6Ptr& subnet, const DuidPtr& duid,
  340. uint32_t iaid, const isc::asiolink::IOAddress& addr,
  341. const bool fwd_dns_update, const bool rev_dns_update,
  342. const std::string& hostname,
  343. const isc::hooks::CalloutHandlePtr& callout_handle,
  344. bool fake_allocation = false);
  345. /// @brief Reuses expired IPv4 lease
  346. ///
  347. /// Updates existing expired lease with new information. Lease database
  348. /// is updated if this is real (i.e. REQUEST, fake_allocation = false), not
  349. /// dummy allocation request (i.e. DISCOVER, fake_allocation = true).
  350. ///
  351. /// @param expired Old, expired lease
  352. /// @param subnet Subnet the lease is allocated from
  353. /// @param clientid Client identifier
  354. /// @param hwaddr Client's hardware address
  355. /// @param fwd_dns_update Indicates whether forward DNS update will be
  356. /// performed for the client (true) or not (false).
  357. /// @param rev_dns_update Indicates whether reverse DNS update will be
  358. /// performed for the client (true) or not (false).
  359. /// @param hostname A string carrying hostname to be used for DNS updates.
  360. /// @param callout_handle A callout handle (used in hooks). A lease callouts
  361. /// will be executed if this parameter is passed.
  362. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  363. /// an address for DISCOVER that is not really allocated (true)
  364. /// @return refreshed lease
  365. /// @throw BadValue if trying to recycle lease that is still valid
  366. Lease4Ptr reuseExpiredLease(Lease4Ptr& expired,
  367. const SubnetPtr& subnet,
  368. const ClientIdPtr& clientid,
  369. const HWAddrPtr& hwaddr,
  370. const bool fwd_dns_update,
  371. const bool rev_dns_update,
  372. const std::string& hostname,
  373. const isc::hooks::CalloutHandlePtr& callout_handle,
  374. bool fake_allocation = false);
  375. /// @brief Reuses expired IPv6 lease
  376. ///
  377. /// Updates existing expired lease with new information. Lease database
  378. /// is updated if this is real (i.e. REQUEST, fake_allocation = false), not
  379. /// dummy allocation request (i.e. SOLICIT, fake_allocation = true).
  380. ///
  381. /// @param expired old, expired lease
  382. /// @param subnet subnet the lease is allocated from
  383. /// @param duid client's DUID
  384. /// @param iaid IAID from the IA_NA container the client sent to us
  385. /// @param fwd_dns_update A boolean value which indicates that server takes
  386. /// responsibility for the forward DNS Update for this lease
  387. /// (if true).
  388. /// @param rev_dns_update A boolean value which indicates that server takes
  389. /// responsibility for the reverse DNS Update for this lease
  390. /// (if true).
  391. /// @param hostname A fully qualified domain-name of the client.
  392. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  393. /// will be executed if this parameter is passed.
  394. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  395. /// an address for SOLICIT that is not really allocated (true)
  396. /// @return refreshed lease
  397. /// @throw BadValue if trying to recycle lease that is still valid
  398. Lease6Ptr reuseExpiredLease(Lease6Ptr& expired, const Subnet6Ptr& subnet,
  399. const DuidPtr& duid, uint32_t iaid,
  400. const bool fwd_dns_update,
  401. const bool rev_dns_update,
  402. const std::string& hostname,
  403. const isc::hooks::CalloutHandlePtr& callout_handle,
  404. bool fake_allocation = false);
  405. /// @brief a pointer to currently used allocator
  406. boost::shared_ptr<Allocator> allocator_;
  407. /// @brief number of attempts before we give up lease allocation (0=unlimited)
  408. unsigned int attempts_;
  409. // hook name indexes (used in hooks callouts)
  410. int hook_index_lease4_select_; ///< index for lease4_select hook
  411. int hook_index_lease6_select_; ///< index for lease6_select hook
  412. };
  413. }; // namespace isc::dhcp
  414. }; // namespace isc
  415. #endif // ALLOC_ENGINE_H