alloc_engine.h 24 KB

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