alloc_engine.h 24 KB

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