alloc_engine.h 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. // Copyright (C) 2012-2015 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 <dhcp/pkt4.h>
  20. #include <dhcp/pkt6.h>
  21. #include <dhcp/option6_ia.h>
  22. #include <dhcpsrv/host.h>
  23. #include <dhcpsrv/subnet.h>
  24. #include <dhcpsrv/lease_mgr.h>
  25. #include <hooks/callout_handle.h>
  26. #include <boost/shared_ptr.hpp>
  27. #include <boost/noncopyable.hpp>
  28. #include <map>
  29. namespace isc {
  30. namespace dhcp {
  31. /// An exception that is thrown when allocation module fails (e.g. due to
  32. /// lack of available addresses)
  33. class AllocFailed : public isc::Exception {
  34. public:
  35. /// @brief constructor
  36. ///
  37. /// @param file name of the file, where exception occurred
  38. /// @param line line of the file, where exception occurred
  39. /// @param what text description of the issue that caused exception
  40. AllocFailed(const char* file, size_t line, const char* what)
  41. : isc::Exception(file, line, what) {}
  42. };
  43. /// @brief DHCPv4 and DHCPv6 allocation engine
  44. ///
  45. /// This class represents a DHCP allocation engine. It is responsible
  46. /// for picking subnets, choosing and allocating a lease, extending,
  47. /// renewing, releasing and possibly expiring leases.
  48. ///
  49. /// @todo: Does not handle out of leases well
  50. /// @todo: Does not handle out of allocation attempts well
  51. class AllocEngine : public boost::noncopyable {
  52. protected:
  53. /// @brief base class for all address/prefix allocation algorithms
  54. ///
  55. /// This is an abstract class that should not be used directly, but rather
  56. /// specialized implementations should be used instead.
  57. class Allocator {
  58. public:
  59. /// @brief picks one address out of available pools in a given subnet
  60. ///
  61. /// This method returns one address from the available pools in the
  62. /// specified subnet. It should not check if the address is used or
  63. /// reserved - AllocEngine will check that and will call pickAddress
  64. /// again if necessary. The number of times this method is called will
  65. /// increase as the number of available leases will decrease.
  66. ///
  67. /// This method can also be used to pick a prefix. We should not rename
  68. /// it to pickLease(), because at this early stage there is no concept
  69. /// of a lease yet. Here it is a matter of selecting one address or
  70. /// prefix from the defined pool, without going into details who it is
  71. /// for or who uses it. I thought that pickAddress() is less confusing
  72. /// than pickResource(), because nobody would immediately know what the
  73. /// resource means in this context.
  74. ///
  75. /// @param subnet next address will be returned from pool of that subnet
  76. /// @param duid Client's DUID
  77. /// @param hint client's hint
  78. ///
  79. /// @return the next address
  80. virtual isc::asiolink::IOAddress
  81. pickAddress(const SubnetPtr& subnet, const DuidPtr& duid,
  82. const isc::asiolink::IOAddress& hint) = 0;
  83. /// @brief Default constructor.
  84. ///
  85. /// Specifies which type of leases this allocator will assign
  86. /// @param pool_type specifies pool type (addresses, temp. addr or prefixes)
  87. Allocator(Lease::Type pool_type)
  88. :pool_type_(pool_type) {
  89. }
  90. /// @brief virtual destructor
  91. virtual ~Allocator() {
  92. }
  93. protected:
  94. /// @brief defines pool type allocation
  95. Lease::Type pool_type_;
  96. };
  97. /// defines a pointer to allocator
  98. typedef boost::shared_ptr<Allocator> AllocatorPtr;
  99. /// @brief Address/prefix allocator that iterates over all addresses
  100. ///
  101. /// This class implements an iterative algorithm that returns all addresses in
  102. /// a pool iteratively, one after another. Once the last address is reached,
  103. /// it starts allocating from the beginning of the first pool (i.e. it loops
  104. /// over).
  105. class IterativeAllocator : public Allocator {
  106. public:
  107. /// @brief default constructor
  108. ///
  109. /// Does not do anything
  110. /// @param type - specifies allocation type
  111. IterativeAllocator(Lease::Type type);
  112. /// @brief returns the next address from pools in a subnet
  113. ///
  114. /// @param subnet next address will be returned from pool of that subnet
  115. /// @param duid Client's DUID (ignored)
  116. /// @param hint client's hint (ignored)
  117. /// @return the next address
  118. virtual isc::asiolink::IOAddress
  119. pickAddress(const SubnetPtr& subnet,
  120. const DuidPtr& duid,
  121. const isc::asiolink::IOAddress& hint);
  122. protected:
  123. /// @brief Returns the next prefix
  124. ///
  125. /// This method works for IPv6 addresses only. It increases the
  126. /// specified prefix by a given prefix_len. For example, 2001:db8::
  127. /// increased by prefix length /32 will become 2001:db9::. This method
  128. /// is used to iterate over IPv6 prefix pools
  129. ///
  130. /// @param prefix prefix to be increased
  131. /// @param prefix_len length of the prefix to be increased
  132. /// @return result prefix
  133. static isc::asiolink::IOAddress
  134. increasePrefix(const isc::asiolink::IOAddress& prefix,
  135. const uint8_t prefix_len);
  136. };
  137. /// @brief Address/prefix allocator that gets an address based on a hash
  138. ///
  139. /// @todo: This is a skeleton class for now and is missing an implementation.
  140. class HashedAllocator : public Allocator {
  141. public:
  142. /// @brief default constructor (does nothing)
  143. /// @param type - specifies allocation type
  144. HashedAllocator(Lease::Type type);
  145. /// @brief returns an address based on hash calculated from client's DUID.
  146. ///
  147. /// @todo: Implement this method
  148. ///
  149. /// @param subnet an address will be picked from pool of that subnet
  150. /// @param duid Client's DUID
  151. /// @param hint a hint (last address that was picked)
  152. /// @return selected address
  153. virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr& subnet,
  154. const DuidPtr& duid,
  155. const isc::asiolink::IOAddress& hint);
  156. };
  157. /// @brief Random allocator that picks address randomly
  158. ///
  159. /// @todo: This is a skeleton class for now and is missing an implementation.
  160. class RandomAllocator : public Allocator {
  161. public:
  162. /// @brief default constructor (does nothing)
  163. /// @param type - specifies allocation type
  164. RandomAllocator(Lease::Type type);
  165. /// @brief returns a random address from pool of specified subnet
  166. ///
  167. /// @todo: Implement this method
  168. ///
  169. /// @param subnet an address will be picked from pool of that subnet
  170. /// @param duid Client's DUID (ignored)
  171. /// @param hint the last address that was picked (ignored)
  172. /// @return a random address from the pool
  173. virtual isc::asiolink::IOAddress
  174. pickAddress(const SubnetPtr& subnet, const DuidPtr& duid,
  175. const isc::asiolink::IOAddress& hint);
  176. };
  177. public:
  178. /// @brief specifies allocation type
  179. typedef enum {
  180. ALLOC_ITERATIVE, // iterative - one address after another
  181. ALLOC_HASHED, // hashed - client's DUID/client-id is hashed
  182. ALLOC_RANDOM // random - an address is randomly selected
  183. } AllocType;
  184. /// @brief Constructor.
  185. ///
  186. /// Instantiates necessary services, required to run DHCP server.
  187. /// In particular, creates IfaceMgr that will be responsible for
  188. /// network interaction. Will instantiate lease manager, and load
  189. /// old or create new DUID.
  190. ///
  191. /// @param engine_type selects allocation algorithm
  192. /// @param attempts number of attempts for each lease allocation before
  193. /// we give up (0 means unlimited)
  194. /// @param ipv6 specifies if the engine should work for IPv4 or IPv6
  195. AllocEngine(AllocType engine_type, uint64_t attempts, bool ipv6 = true);
  196. /// @brief Destructor.
  197. virtual ~AllocEngine() { }
  198. /// @brief Returns allocator for a given pool type
  199. ///
  200. /// @param type type of pool (V4, IA, TA or PD)
  201. /// @throw BadValue if allocator for a given type is missing
  202. /// @return pointer to allocator handling a given resource types
  203. AllocatorPtr getAllocator(Lease::Type type);
  204. private:
  205. /// @brief a pointer to currently used allocator
  206. ///
  207. /// For IPv4, there will be only one allocator: TYPE_V4
  208. /// For IPv6, there will be 3 allocators: TYPE_NA, TYPE_TA, TYPE_PD
  209. std::map<Lease::Type, AllocatorPtr> allocators_;
  210. /// @brief number of attempts before we give up lease allocation (0=unlimited)
  211. uint64_t attempts_;
  212. // hook name indexes (used in hooks callouts)
  213. int hook_index_lease4_select_; ///< index for lease4_select hook
  214. int hook_index_lease6_select_; ///< index for lease6_select hook
  215. public:
  216. /// @brief Defines a single hint (an address + prefix-length).
  217. ///
  218. /// This is an entry that represents what the client had requested,
  219. /// either an address or a prefix. Prefix length is 128 for regular
  220. /// addresses.
  221. typedef std::pair<isc::asiolink::IOAddress, uint8_t> HintType;
  222. /// @brief Container for client's hints.
  223. typedef std::vector<HintType> HintContainer;
  224. /// @brief Context information for the DHCPv6 leases allocation.
  225. ///
  226. /// This structure holds a set of information provided by the DHCPv6
  227. /// server to the allocation engine. In particular, it holds the
  228. /// client identifying information, such as HW address or client
  229. /// identifier. It also holds the information about the subnet that
  230. /// the client is connected to.
  231. ///
  232. /// This structure is also used to pass some information from
  233. /// the allocation engine back to the server, i.e. the old leases
  234. /// which the client had before the allocation.
  235. ///
  236. /// This structure is expected to be common for a single client, even
  237. /// if multiple IAs are used. Some of the fields will need to be
  238. /// updated for every call (there's a separate call to the allocation
  239. /// engine for each IA option).
  240. ///
  241. /// This structure is meant to be extended in the future, if more
  242. /// information should be passed to the allocation engine. Note
  243. /// that the big advantage of using the context structure to pass
  244. /// information to the allocation engine methods is that adding
  245. /// new information doesn't modify the API of the allocation engine.
  246. struct ClientContext6 {
  247. /// @brief Subnet selected for the client by the server.
  248. Subnet6Ptr subnet_;
  249. /// @brief Client identifier
  250. DuidPtr duid_;
  251. /// @brief iaid IAID field from IA_NA or IA_PD that is being processed
  252. uint32_t iaid_;
  253. /// @brief Lease type (IA or PD)
  254. Lease::Type type_;
  255. /// @brief Hardware/MAC address (if available, may be NULL)
  256. HWAddrPtr hwaddr_;
  257. /// @brief client's hints
  258. ///
  259. /// There will typically be just one address, but the protocol allows
  260. /// more than one address or prefix for each IA container.
  261. HintContainer hints_;
  262. /// @brief A boolean value which indicates that server takes
  263. /// responsibility for the forward DNS Update for this lease
  264. /// (if true).
  265. bool fwd_dns_update_;
  266. /// @brief A boolean value which indicates that server takes
  267. /// responsibility for the reverse DNS Update for this lease
  268. /// (if true).
  269. bool rev_dns_update_;
  270. /// @brief Hostname.
  271. ///
  272. /// The server retrieves the hostname from the Client FQDN option,
  273. /// Hostname option or the host reservation record for the client.
  274. std::string hostname_;
  275. /// @brief Callout handle associated with the client's message.
  276. hooks::CalloutHandlePtr callout_handle_;
  277. /// @brief Indicates if this is a real or fake allocation.
  278. ///
  279. /// The real allocation is when the allocation engine is supposed
  280. /// to make an update in a lease database: create new lease, or
  281. /// update existing lease.
  282. bool fake_allocation_;
  283. /// @brief A pointer to any old leases that the client had before update
  284. /// but are no longer valid after the update/allocation.
  285. ///
  286. /// This collection is typically empty, except cases when we are doing
  287. /// address reassignment, e.g. because there is a host reservation that
  288. /// gives this address to someone else, so we had to return the address,
  289. /// and give a new one to this client.
  290. Lease6Collection old_leases_;
  291. /// @brief A pointer to any leases that have changed FQDN information.
  292. ///
  293. /// This list may contain old versions of the leases that are still
  294. /// valid. In particular, it will contain a lease if the client's
  295. /// FQDN has changed.
  296. Lease6Collection changed_leases_;
  297. /// @brief A pointer to the object identifying host reservations.
  298. ///
  299. /// May be NULL if there are no reservations.
  300. ConstHostPtr host_;
  301. /// @brief A pointer to the client's message
  302. ///
  303. /// This is used exclusively for hook purposes.
  304. Pkt6Ptr query_;
  305. /// @brief A pointer to the IA_NA/IA_PD option to be sent in response
  306. Option6IAPtr ia_rsp_;
  307. /// @brief Default constructor.
  308. ClientContext6();
  309. /// @brief Constructor with parameters.
  310. ///
  311. /// Note that several less frequently used parameters (callout_handle,
  312. /// old_leases, host) fields are not set. They should be set explicitly,
  313. /// if needed.
  314. ///
  315. /// @param subnet subnet the allocation should come from
  316. /// @param duid Client's DUID
  317. /// @param iaid iaid field from the IA_NA container that client sent
  318. /// @param hint a hint that the client provided
  319. /// @param type lease type (IA, TA or PD)
  320. /// @param fwd_dns A boolean value which indicates that server takes
  321. /// responsibility for the forward DNS Update for this lease
  322. /// (if true).
  323. /// @param rev_dns A boolean value which indicates that server takes
  324. /// responsibility for the reverse DNS Update for this lease
  325. /// (if true).
  326. /// @param hostname A fully qualified domain-name of the client.
  327. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  328. /// an address for SOLICIT that is not really allocated (true)
  329. ClientContext6(const Subnet6Ptr& subnet, const DuidPtr& duid,
  330. const uint32_t iaid, const isc::asiolink::IOAddress& hint,
  331. const Lease::Type type, const bool fwd_dns, const bool
  332. rev_dns, const std::string& hostname, const bool
  333. fake_allocation);
  334. };
  335. /// @brief Allocates IPv6 leases for a given IA container
  336. ///
  337. /// This method uses the currently selected allocator to pick allocatable
  338. /// resources (i.e. addresses or prefixes) from specified subnet, creates
  339. /// a lease (one or more, if needed) for that resources and then inserts
  340. /// it into LeaseMgr (if this allocation is not fake, i.e. this is not a
  341. /// response to SOLICIT).
  342. ///
  343. /// This method uses host reservation if ctx.host_ is set. The easy way to
  344. /// set it is to call @ref isc::dhcp::AllocEngine::findReservation(ctx).
  345. /// The host reservation is convenient, but incurs performance penalty,
  346. /// so it can be tweaked on a per subnet basis. There are three possible modes:
  347. /// 1. disabled (no host reservation at all). This is the most performant one
  348. /// as the code can skip all checks;
  349. /// 2. out-of-pool (only reservations that are outside
  350. /// of the dynamic pools are allowed. This is a compromise - it requires
  351. /// a sysadmin to be more careful with the reservations, but the code
  352. /// can skip reservation checks while managing in-pool addresses);
  353. /// 3. in-pool (which also allow out-of-pool; this is the most flexible
  354. /// mode, but it means that the allocation engine has to do reservation
  355. /// checks on every lease, even those dynamically assigned, which degrades
  356. /// performance).
  357. ///
  358. /// The logic in this method is as follows:
  359. /// -# Case 1. if there are no leases, and there are reservations...
  360. /// Are the reserved addresses/prefixes used by someone else?
  361. /// -# yes: we have a problem. We can't assign the reserved address yet,
  362. /// because it is used by someone else. We can't immediately release
  363. /// the lease as there is some other client that is currently using it.
  364. /// We will temporarily assign a different, unreserved lease for this
  365. /// client. In the mean time, the other client will hopefully get back
  366. /// to us, so we could revoke his lease.
  367. /// -# no: assign them => done
  368. /// -# Case 2. if there are leases and there are no reservations...
  369. /// Are the leases reserved for someone else?
  370. /// -# yes: release them, assign something else
  371. /// -# no: renew them => done
  372. /// -# Case 3. if there are leases and there are reservations...
  373. /// Are the leases matching reservations?
  374. /// -# yes: renew them => done
  375. /// -# no: release existing leases, assign new ones based on reservations
  376. /// -# Case 4. if there are no leases and no reservations...
  377. /// assign new leases (this is the "normal" case when the reservations
  378. /// are disabled).
  379. ///
  380. /// @param ctx client context that passes all necessary information. See
  381. /// @ref ClientContext6 for details.
  382. ///
  383. /// The following fields of ClientContext6 are used:
  384. ///
  385. /// @ref ClientContext6::subnet_ subnet the allocation should come from<br/>
  386. /// @ref ClientContext6::duid_ Client's DUID<br/>
  387. /// @ref ClientContext6::iaid_ iaid field from the IA_NA container
  388. /// that client sent<br/>
  389. /// @ref ClientContext6::hints_ a hint that the client provided<br/>
  390. /// @ref ClientContext6::type_ lease type (IA, TA or PD)<br/>
  391. /// @ref ClientContext6::fwd_dns_update_ A boolean value which indicates
  392. /// that server takes responsibility for the forward DNS Update
  393. /// for this lease (if true).<br/>
  394. /// @ref ClientContext6::rev_dns_update_ A boolean value which indicates
  395. /// that server takes responsibility for the reverse DNS Update for
  396. /// this lease (if true).<br/>
  397. /// @ref ClientContext6::hostname_ A fully qualified domain-name of the client.<br/>
  398. /// @ref ClientContext6::fake_allocation_ is this real i.e. REQUEST (false)
  399. /// or just picking an address for SOLICIT that is not really
  400. /// allocated (true)<br/>
  401. /// @ref ClientContext6::callout_handle_ a callout handle (used in hooks). A
  402. /// lease callouts will be executed if this parameter is passed.<br/>
  403. /// @ref ClientContext6::old_leases_ [out] Collection to which this function
  404. /// will append old leases. Leases are stored in the same order as in
  405. /// the collection of new leases, being returned. For newly allocated
  406. /// leases (not renewed) the NULL pointers are stored in this
  407. /// collection as old leases.<br/>
  408. /// @ref ClientContext6::hwaddr_ Hardware address (optional, may be null if
  409. /// not available)<br/>
  410. /// @ref ClientContext6::host_ Host reservation. allocateLeases6 will set
  411. /// this field, if appropriate reservation is found.
  412. ///
  413. /// @return Allocated IPv6 leases (may be empty if allocation failed)
  414. Lease6Collection
  415. allocateLeases6(ClientContext6& ctx);
  416. /// @brief Renews existing DHCPv6 leases for a given IA.
  417. ///
  418. /// This method updates the leases associated with a specified IA container.
  419. /// It will extend the leases under normal circumstances, but sometimes
  420. /// there may be reasons why not to do so. Such a reasons may be:
  421. /// - client attempts to renew an address that is not valid
  422. /// - client attempts to renew an address that is now reserved for someone
  423. /// else (see host reservation)
  424. /// - client's leases does not match his reservations
  425. ///
  426. /// This method will call the lease6_renew callout.
  427. ///
  428. /// @param ctx Message processing context. It holds various information
  429. /// extracted from the client's message and required to allocate a lease.
  430. /// In particular, @ref ClientContext6::hints_ provides list of addresses or
  431. /// prefixes the client had sent. @ref ClientContext6::old_leases_ will
  432. /// contain removed leases in this case.
  433. ///
  434. /// @return Returns renewed lease.
  435. Lease6Collection renewLeases6(ClientContext6& ctx);
  436. /// @brief Reclaims expired leases.
  437. ///
  438. /// This method retrieves a collection of expired leases and reclaims them.
  439. /// See http://kea.isc.org/wiki/LeaseExpirationDesign#LeasesReclamationRoutine
  440. /// for the details.
  441. ///
  442. /// @param max_leases Maximum number of leases to be reclaimed.
  443. /// @param timeout Maximum amount of time that the reclaimation routine
  444. /// may be processing expired leases, expressed in seconds.
  445. /// @param remove_lease A boolean value indicating if the lease should
  446. /// be removed when it is reclaimed (if true) or it should be left in the
  447. /// database in the "expired-reclaimed" state (if false).
  448. void reclaimExpiredLeases6(const size_t max_leases, const uint16_t timeout,
  449. const bool remove_lease);
  450. /// @brief Attempts to find appropriate host reservation.
  451. ///
  452. /// Attempts to find appropriate host reservation in HostMgr. If found, it
  453. /// will be set in ctx.host_.
  454. /// @param ctx Client context that contains all necessary information.
  455. void findReservation(ClientContext6& ctx) const;
  456. private:
  457. /// @brief creates a lease and inserts it in LeaseMgr if necessary
  458. ///
  459. /// Creates a lease based on specified parameters and tries to insert it
  460. /// into the database. That may fail in some cases, i.e. when there is another
  461. /// allocation process and we lost a race to a specific lease.
  462. ///
  463. /// @param ctx client context that passes all necessary information. See
  464. /// @ref ClientContext6 for details.
  465. /// @param addr an address that was selected and is confirmed to be
  466. /// available
  467. /// @param prefix_len length of the prefix (for PD only)
  468. /// should be 128 for other lease types
  469. ///
  470. /// The following fields of the ctx structure are used:
  471. /// @ref ClientContext6::subnet_ subnet the lease is allocated from
  472. /// @ref ClientContext6::duid_ client's DUID
  473. /// @ref ClientContext6::iaid_ IAID from the IA_NA container the client sent to us
  474. /// @ref ClientContext6::type_ lease type (IA, TA or PD)
  475. /// @ref ClientContext6::fwd_dns_update_ A boolean value which indicates that server takes
  476. /// responsibility for the forward DNS Update for this lease
  477. /// (if true).
  478. /// @ref ClientContext6::rev_dns_update_ A boolean value which indicates that server takes
  479. /// responsibility for the reverse DNS Update for this lease
  480. /// (if true).
  481. /// @ref ClientContext6::hostname_ A fully qualified domain-name of the client.
  482. /// @ref ClientContext6::hwaddr_ Hardware address (optional, may be null for Lease6)
  483. /// @ref ClientContext6::callout_handle_ a callout handle (used in hooks). A lease callouts
  484. /// will be executed if this parameter is passed (and there are callouts
  485. /// registered)
  486. /// @ref ClientContext6::fake_allocation_ is this real i.e. REQUEST (false) or just picking
  487. /// an address for SOLICIT that is not really allocated (true)
  488. /// @return allocated lease (or NULL in the unlikely case of the lease just
  489. /// became unavailable)
  490. Lease6Ptr createLease6(ClientContext6& ctx,
  491. const isc::asiolink::IOAddress& addr,
  492. const uint8_t prefix_len);
  493. /// @brief Allocates a normal, in-pool, unreserved lease from the pool.
  494. ///
  495. /// It attempts to pick a hint first, then uses allocator iteratively until
  496. /// an available (not used, not reserved) lease is found. In principle, it
  497. /// may return more than one lease, but we currently handle only one.
  498. /// This may change in the future.
  499. ///
  500. /// @param ctx client context that contains all details (subnet, client-id, etc.)
  501. /// @return collection of newly allocated leases
  502. Lease6Collection allocateUnreservedLeases6(ClientContext6& ctx);
  503. /// @brief Creates new leases based on reservations.
  504. ///
  505. /// This method allocates new leases, based on host reservation. Existing
  506. /// leases are specified in existing_leases parameter. A new lease is not created,
  507. /// if there is a lease for specified address on existing_leases list or there is
  508. /// a lease used by someone else.
  509. ///
  510. /// @param ctx client context that contains all details (subnet, client-id, etc.)
  511. /// @param existing_leases leases that are already associated with the client
  512. void
  513. allocateReservedLeases6(ClientContext6& ctx, Lease6Collection& existing_leases);
  514. /// @brief Removes leases that are reserved for someone else.
  515. ///
  516. /// Goes through the list specified in existing_leases and removes those that
  517. /// are reserved by someone else. The removed leases are added to the
  518. /// ctx.removed_leases_ collection.
  519. ///
  520. /// @param ctx client context that contains all details (subnet, client-id, etc.)
  521. /// @param existing_leases [in/out] leases that should be checked
  522. void
  523. removeNonmatchingReservedLeases6(ClientContext6& ctx,
  524. Lease6Collection& existing_leases);
  525. /// @brief Removed leases that are not reserved for this client
  526. ///
  527. /// This method iterates over existing_leases and will remove leases that are
  528. /// not reserved for this client. It will leave at least one lease on the list,
  529. /// if possible. The reason to run this method is that if there is a reservation
  530. /// for address A for client X and client X already has a lease for a
  531. /// different address B, we should assign A and release B. However,
  532. /// if for some reason we can't assign A, keeping B would be better than
  533. /// not having a lease at all. Hence we may keep B if that's the only lease
  534. /// left.
  535. ///
  536. /// @param ctx client context that contains all details (subnet, client-id, etc.)
  537. /// @param existing_leases [in/out] leases that should be checked
  538. void
  539. removeNonreservedLeases6(ClientContext6& ctx,
  540. Lease6Collection& existing_leases);
  541. /// @brief Reuses expired IPv6 lease
  542. ///
  543. /// Updates existing expired lease with new information. Lease database
  544. /// is updated if this is real (i.e. REQUEST, fake_allocation = false), not
  545. /// dummy allocation request (i.e. SOLICIT, fake_allocation = true).
  546. ///
  547. /// @param expired old, expired lease
  548. /// @param ctx client context that contains all details.
  549. /// @param prefix_len prefix length (for PD leases)
  550. /// Should be 128 for other lease types
  551. ///
  552. /// The following parameters are used from the ctx structure:
  553. /// @ref ClientContext6::subnet_ subnet the lease is allocated from
  554. /// @ref ClientContext6::duid_ client's DUID
  555. /// @ref ClientContext6::iaid_ IAID from the IA_NA container the client sent to us
  556. /// @ref ClientContext6::fwd_dns_update_ A boolean value which indicates that server takes
  557. /// responsibility for the forward DNS Update for this lease
  558. /// (if true).
  559. /// @ref ClientContext6::rev_dns_update_ A boolean value which indicates that server takes
  560. /// responsibility for the reverse DNS Update for this lease
  561. /// (if true).
  562. /// @ref ClientContext6::hostname_ A fully qualified domain-name of the client.
  563. /// @ref ClientContext6::callout_handle_ a callout handle (used in hooks). A
  564. /// lease callouts will be executed if this parameter is passed.
  565. /// @ref ClientContext6::fake_allocation_ is this real i.e. REQUEST (false)
  566. /// or just picking an address for SOLICIT that is not really
  567. /// allocated (true)
  568. ///
  569. /// @return refreshed lease
  570. /// @throw BadValue if trying to recycle lease that is still valid
  571. Lease6Ptr reuseExpiredLease(Lease6Ptr& expired,
  572. ClientContext6& ctx,
  573. uint8_t prefix_len);
  574. /// @brief Updates FQDN and Client's Last Tranmission Time for a collection
  575. /// of leases.
  576. ///
  577. /// This method is executed when the server finds existing leases for a
  578. /// client and updates some date for these leases if needed:
  579. /// - client's last transmission time (cltt), if the lease to be returned
  580. /// to the client should have its lifetime extended,
  581. /// - FQDN data, when the client has negotiated new FQDN with the server.
  582. ///
  583. /// @param ctx IPv6 client context (old versions of the leases that had
  584. /// FQDN data changed will be stored in ctx.changed_leases_,
  585. /// ctx.fwd_dns_update, ctx.rev_dns_update, ctx.hostname_
  586. /// and ctx.fake_allocation_ will be used.
  587. /// @param leases Collection of leases for which lease data should be
  588. /// updated.
  589. ///
  590. /// @return Collection of leases with updated data. Note that returned
  591. /// collection holds updated FQDN data even for fake allocation.
  592. Lease6Collection updateLeaseData(ClientContext6& ctx,
  593. const Lease6Collection& leases);
  594. /// @brief Utility function that removes all leases with a specified address
  595. /// @param container A collection of Lease6 pointers
  596. /// @param addr address to be removed
  597. /// @return true if removed (false otherwise)
  598. static bool
  599. removeLeases(Lease6Collection& container,
  600. const asiolink::IOAddress& addr);
  601. /// @brief Extends specified IPv6 lease
  602. ///
  603. /// This method attempts to extend the lease. It will call the lease6_renew
  604. /// or lease6_rebind hooks (depending on the client's message specified in
  605. /// ctx.query). The lease will be extended in LeaseMgr, unless the hooks
  606. /// library will set the skip flag.
  607. ///
  608. /// @param ctx client context that passes all necessary information. See
  609. /// @ref ClientContext6 for details.
  610. /// @param lease IPv6 lease to be extended.
  611. void extendLease6(ClientContext6& ctx, Lease6Ptr lease);
  612. /// @brief Sends removal name change reuqest to D2.
  613. ///
  614. /// This method is exception safe.
  615. ///
  616. /// @param lease Pointer to a lease for which NCR should be sent.
  617. /// @param identifier Identifier to be used to generate DHCID for
  618. /// the DNS update. For DHCPv4 it will be hardware address, client
  619. /// identifier. For DHCPv6 it will be a DUID.
  620. ///
  621. /// @tparam LeasePtrType Pointer to a lease.
  622. /// @tparam Identifier HW Address, Client Identifier or DUID.
  623. template<typename LeasePtrType, typename IdentifierType>
  624. void queueRemovalNameChangeRequest(const LeasePtrType& lease,
  625. const IdentifierType& identifier) const;
  626. public:
  627. /// @brief Context information for the DHCPv4 lease allocation.
  628. ///
  629. /// This structure holds a set of information provided by the DHCPv4
  630. /// server to the allocation engine. In particular, it holds the
  631. /// client identifying information, such as HW address or client
  632. /// identifier. It also holds the information about the subnet that
  633. /// the client is connected to.
  634. ///
  635. /// This structure is also used to pass some information from
  636. /// the allocation engine back to the server, i.e. the old lease
  637. /// which the client had before the allocation.
  638. ///
  639. /// This structure is meant to be extended in the future, if more
  640. /// information should be passed to the allocation engine. Note
  641. /// that the big advantage of using the context structure to pass
  642. /// information to the allocation engine methods is that adding
  643. /// new information doesn't modify the API of the allocation engine.
  644. struct ClientContext4 {
  645. /// @brief Subnet selected for the client by the server.
  646. Subnet4Ptr subnet_;
  647. /// @brief Client identifier from the DHCP message.
  648. ClientIdPtr clientid_;
  649. /// @brief HW address from the DHCP message.
  650. HWAddrPtr hwaddr_;
  651. /// @brief An address that the client desires.
  652. ///
  653. /// If this address is set to 0 it indicates that this address
  654. /// is unspecified.
  655. asiolink::IOAddress requested_address_;
  656. /// @brief Perform forward DNS update.
  657. bool fwd_dns_update_;
  658. /// @brief Perform reverse DNS update.
  659. bool rev_dns_update_;
  660. /// @brief Hostname.
  661. ///
  662. /// The server retrieves the hostname from the Client FQDN option,
  663. /// Hostname option or the host reservation record for the client.
  664. std::string hostname_;
  665. /// @brief Callout handle associated with the client's message.
  666. hooks::CalloutHandlePtr callout_handle_;
  667. /// @brief Indicates if this is a real or fake allocation.
  668. ///
  669. /// The real allocation is when the allocation engine is supposed
  670. /// to make an update in a lease database: create new lease, or
  671. /// update existing lease.
  672. bool fake_allocation_;
  673. /// @brief A pointer to an old lease that the client had before update.
  674. Lease4Ptr old_lease_;
  675. /// @brief A pointer to the object identifying host reservations.
  676. ConstHostPtr host_;
  677. /// @brief A pointer to the object representing a lease in conflict.
  678. ///
  679. /// This pointer is set by some of the allocation methods when
  680. /// the lease can't be allocated because there is another lease
  681. /// which is in conflict with this allocation.
  682. Lease4Ptr conflicting_lease_;
  683. /// @brief A pointer to the client's message.
  684. ///
  685. /// This is used in logging to retrieve the client's and the
  686. /// transaction identification information.
  687. Pkt4Ptr query_;
  688. /// @brief Default constructor.
  689. ClientContext4();
  690. /// @brief Constructor with parameters
  691. ///
  692. /// @param subnet subnet the allocation should come from (mandatory)
  693. /// @param clientid Client identifier (optional)
  694. /// @param hwaddr Client's hardware address info (mandatory)
  695. /// @param requested_addr A hint that the client provided (may be 0.0.0.0)
  696. /// @param fwd_dns_update Indicates whether forward DNS
  697. /// update will be performed for the client (true) or not (false).
  698. /// @param rev_dns_update Indicates whether reverse DNS
  699. /// update will be performed for the client (true) or not (false).
  700. /// @param hostname A string carrying hostname to be used for DNS updates.
  701. /// @param fake_allocation Is this real i.e. REQUEST (false)
  702. /// or just picking an address for DISCOVER that is not really
  703. /// allocated (true)
  704. ClientContext4(const Subnet4Ptr& subnet, const ClientIdPtr& clientid,
  705. const HWAddrPtr& hwaddr,
  706. const asiolink::IOAddress& requested_addr,
  707. const bool fwd_dns_update, const bool rev_dns_update,
  708. const std::string& hostname, const bool fake_allocation);
  709. };
  710. /// @brief Pointer to the @c ClientContext4.
  711. typedef boost::shared_ptr<ClientContext4> ClientContext4Ptr;
  712. /// @brief Returns IPv4 lease.
  713. ///
  714. /// This method finds a lease for a client using the following algorithm:
  715. /// - If a lease exists for the combination of the HW address or client id
  716. /// and a subnet, try to use this lease for the client. If the client
  717. /// has a reservation for an address for which the lease was created or
  718. /// the client desires to renew the lease for this address (ciaddr or
  719. /// requested IP address option), the server renews the lease for the
  720. /// client. If the client desires a different address or the server has
  721. /// a (potentially new) reservation for a different address for this
  722. /// client, the existing lease is replaced with a new lease.
  723. /// - If the client has no lease in the lease database the server will try
  724. /// to allocate a new lease. If the client has a reservation for the
  725. /// particular address or if it has specified a desired address the
  726. /// server will check if the particular address is not allocated to
  727. /// another client. If the address is available, the server will allocate
  728. /// this address for the client.
  729. /// - If the desired address is unavailable the server checks if the
  730. /// lease for this address has expired. If the lease is expired, the
  731. /// server will allocate this lease to the client. The relevant
  732. /// information will be updated, e.g. new client HW address, host name
  733. /// etc.
  734. /// - If the desired address is in use by another client, the server will
  735. /// try to allocate a different address. The server picks addresses from
  736. /// a dynamic pool and checks if the address is available and that
  737. /// it is not reserved for another client. If it is in use by another
  738. /// client or if it is reserved for another client, the address is not
  739. /// allocated. The server picks the next address and repeats this check.
  740. /// Note that the server ceases allocation after the configured number
  741. /// of unsuccessful attempts.
  742. ///
  743. /// The lease allocation process is slightly different for the
  744. /// DHCPDISCOVER and DHCPREQUEST messages. In the former case, the client
  745. /// may specify the requested IP address option with a desired address and
  746. /// the server treats this address as a hint. This means that the server may
  747. /// allocate a different address at its discretion and send it to the
  748. /// client in the DHCPOFFER. If the client accepts this offer it specifies
  749. /// this address in the requested IP address option in the DHCPREQUEST.
  750. /// At this point, the allocation engine will use the requested IP address
  751. /// as a hard requirement and if this address can't be allocated for
  752. /// any reason, the allocation engine returns NULL lease. As a result,
  753. /// the DHCP server sends a DHCPNAK to the client and the client
  754. /// falls back to the DHCP server discovery.
  755. ///
  756. /// The only exception from this rule is when the client doesn't specify
  757. /// a requested IP address option (invalid behavior) in which case the
  758. /// allocation engine will try to allocate any address.
  759. ///
  760. /// If there is an address reservation specified for the particular client
  761. /// the reserved address always takes precedence over addresses from the
  762. /// dynamic pool or even an address currently allocated for this client.
  763. ///
  764. /// It is possible that the address reserved for the particular client
  765. /// is in use by another client, e.g. as a result of pools reconfiguration.
  766. /// In this case, when the client requests allocation of the reserved
  767. /// address and the server determines that it is leased to someone else,
  768. /// the allocation engine allocates a different address for this client.
  769. ///
  770. /// When the client having a lease returns to renew, the allocation engine
  771. /// doesn't extend the lease for it and returns a NULL pointer. The client
  772. /// falls back to the 4-way exchange and a different lease is allocated.
  773. /// At this point, the reserved address is freed and can be allocated to
  774. /// the client which holds this reservation. However, this client has a
  775. /// lease for a different address at this time. When the client renews its
  776. /// lease it receives the DHCPNAK and falls back to the DHCP server
  777. /// discovery and obtains the lease for the reserved address.
  778. ///
  779. /// When a server should do DNS updates, it is required that allocation
  780. /// returns the information about how the lease was obtained by the allocation
  781. /// engine. In particular, the DHCP server should be able to check whether
  782. /// an existing lease was returned, or a new lease was allocated. When an
  783. /// existing lease was returned, the server should check whether the FQDN has
  784. /// changed between the allocation of the old and new lease. If so, the server
  785. /// should perform the appropriate DNS update. If not, the server may choose
  786. /// to not perform the update. The information about the old lease is returned via
  787. /// @c old_lease parameter. If NULL value is returned, it is an indication
  788. /// that a new lease was allocated for the client. If non-NULL value is
  789. /// returned, it is an indication that allocation engine reused/renewed an
  790. /// existing lease.
  791. ///
  792. /// @param ctx client context that passes all necessary information. See
  793. /// @ref ClientContext4 for details.
  794. ///
  795. /// The following fields of @ref ClientContext4 are used:
  796. ///
  797. /// - @ref ClientContext4::subnet_ subnet the allocation should come from
  798. /// - @ref ClientContext4::clientid_ Client identifier
  799. /// - @ref ClientContext4::hwaddr_ Client's hardware address info
  800. /// - @ref ClientContext4::requested_address_ A hint that the client provided
  801. /// - @ref ClientContext4::fwd_dns_update_ Indicates whether forward DNS
  802. /// update will be performed for the client (true) or not (false).
  803. /// - @ref ClientContext4::rev_dns_update_ Indicates whether reverse DNS
  804. /// update will be performed for the client (true) or not (false).
  805. /// - @ref ClientContext4::hostname_ A string carrying hostname to be used for
  806. /// DNS updates.
  807. /// - @ref ClientContext4::fake_allocation_ Is this real i.e. REQUEST (false)
  808. /// or just picking an address for DISCOVER that is not really
  809. /// allocated (true)
  810. /// - @ref ClientContext4::host_ Pointer to the object representing the
  811. // static reservations (host reservations) for the client.
  812. /// - @ref ClientContext4::callout_handle_ A callout handle (used in hooks).
  813. /// A lease callouts will be executed if this parameter is passed.
  814. /// - @ref ClientContext4::old_lease_ [out] Holds the pointer to a previous
  815. /// instance of a lease. The NULL pointer indicates that lease didn't
  816. /// exist prior to calling this function (e.g. new lease has been allocated).
  817. ///
  818. /// @return Allocated IPv4 lease (or NULL if allocation failed).
  819. Lease4Ptr allocateLease4(ClientContext4& ctx);
  820. /// @brief Attempts to find the host reservation for the client.
  821. ///
  822. /// This method attempts to find the host reservation for the client. If
  823. /// found, it is set in the @c ctx.host_. If the host reservations are
  824. /// disabled for the particular subnet or the reservation is not found
  825. /// for the client, the @c ctx.host_ is set to NULL.
  826. ///
  827. /// @param ctx Client context holding various information about the client.
  828. static void findReservation(ClientContext4& ctx);
  829. private:
  830. /// @brief Offers the lease.
  831. ///
  832. /// This method is called by the @c AllocEngine::allocateLease4 when
  833. /// the server is processing a DHCPDISCOVER message, i.e. the fake
  834. /// allocation case.
  835. ///
  836. /// This method doesn't modify leases in the lease database. It finds
  837. /// the most suitable lease for the client and returns it to the caller.
  838. /// The server uses this lease when it sends the DHCPOFFER to the
  839. /// client from which it has received a DHCPDISCOVER message.
  840. ///
  841. /// The lease is found using the following algorithm:
  842. /// -# If there is a reservation for the client, try to use the reserved
  843. /// address. This may fail if the particular address is in use by
  844. /// another client. In such case:
  845. /// -# If the client has a lease, try to offer this lease. This may fail
  846. /// if it turns out that this address is reserved for another client
  847. /// or the address doesn't belong to the address pool. In such case:
  848. /// -# Try to allocate the address provided by the client as a hint.
  849. /// This may fail if the address is in use or is reserved by some
  850. /// other client. In such case:
  851. /// -# Try to offer an address from the dynamic pool.
  852. ///
  853. /// @throw various exceptions if the allocation goes wrong.
  854. ///
  855. /// @param ctx Client context holding the data extracted from the
  856. /// client's message.
  857. ///
  858. /// @return A pointer to the offered lease, or NULL if no suitable lease
  859. /// has been found.
  860. Lease4Ptr discoverLease4(ClientContext4& ctx);
  861. /// @brief Allocates the lease.
  862. ///
  863. /// This method is called by the @c AllocEngine::allocateLease4 when
  864. /// the server is processing a DHCPREQUEST message, i.e. the real
  865. /// allocation case.
  866. ///
  867. /// This method modifies the lease information in the lease database.
  868. /// It adds new leases, modifies existing leases or deletes them.
  869. ///
  870. /// The method returns NULL to indicate that the lease allocation
  871. /// has failed when any of the following occur:
  872. /// -# The requested address is specified but is reserved for another
  873. /// client.
  874. /// -# The requested address is in use by another client.
  875. /// -# There is a reservation for the particular client, the
  876. /// reserved address is not in use by another client and the
  877. /// requested address is different than the reserved address.
  878. /// -# There is no reservation for the client and the requested address
  879. /// is not in the dynamic pool.
  880. ///
  881. /// If none of the above occurs, the method will try to allocate the
  882. /// lease for the client using the following algorithm:
  883. /// -# If the client has a lease and the client is requesting the
  884. /// address for which it has a lease, renew its lease.
  885. /// -# If the client is requesting a different address than that for
  886. /// which it has a lease, try to allocate the requested address.
  887. /// This may fail if the address is in use by another client.
  888. /// -# If the client is not requesting any specific address, allocate
  889. /// the address from the dynamic pool.
  890. ///
  891. /// @throws various exceptions if the allocation goes wrong.
  892. ///
  893. /// @param ctx Client context holding the data extracted from the
  894. /// client's message.
  895. ///
  896. /// @return A pointer to the allocated lease, or NULL if no suitable
  897. /// lease could be allocated.
  898. Lease4Ptr requestLease4(ClientContext4& ctx);
  899. /// @brief Creates a lease and inserts it in LeaseMgr if necessary
  900. ///
  901. /// Creates a lease based on specified parameters and tries to insert it
  902. /// into the database. That may fail in some cases, e.g. when there is another
  903. /// allocation process and we lost a race to a specific lease.
  904. ///
  905. /// @param ctx client context that contains additional parameters.
  906. /// @param addr An address that was selected and is confirmed to be available
  907. ///
  908. /// In particular, the following fields from Client context are used:
  909. /// - @ref ClientContext4::subnet_ Subnet the lease is allocated from
  910. /// - @ref ClientContext4::clientid_ Client identifier
  911. /// - @ref ClientContext4::hwaddr_ Client's hardware address
  912. /// - @ref ClientContext4::fwd_dns_update_ Indicates whether forward DNS update
  913. /// will be performed for the client (true) or not (false).
  914. /// - @ref ClientContext4::rev_dns_update_ Indicates whether reverse DNS update
  915. /// will be performed for the client (true) or not (false).
  916. /// - @ref ClientContext4::hostname_ A string carrying hostname to be used for
  917. /// DNS updates.
  918. /// - @ref ClientContext4::callout_handle_ a callout handle (used in hooks).
  919. /// A lease callouts will be executed if this parameter is passed
  920. /// (and there are callouts registered)
  921. /// - @ref ClientContext4::fake_allocation_ Is this real i.e. REQUEST (false)
  922. /// or just picking an address for DISCOVER that is not really
  923. /// allocated (true)
  924. /// @return allocated lease (or NULL in the unlikely case of the lease just
  925. /// becomed unavailable)
  926. Lease4Ptr createLease4(const ClientContext4& ctx,
  927. const isc::asiolink::IOAddress& addr);
  928. /// @brief Renews a DHCPv4 lease.
  929. ///
  930. /// This method updates the lease with the information from the provided
  931. /// context and invokes the lease4_renew callout.
  932. ///
  933. /// The address of the lease being renewed is NOT updated.
  934. ///
  935. /// @param lease A lease to be renewed.
  936. /// @param ctx Message processing context. It holds various information
  937. /// extracted from the client's message and required to allocate a lease.
  938. ///
  939. /// @return Returns renewed lease. Note that the lease is only updated when
  940. /// it is an actual allocation (not processing a DHCPDISCOVER message).
  941. Lease4Ptr renewLease4(const Lease4Ptr& lease, ClientContext4& ctx);
  942. /// @brief Reuses expired DHCPv4 lease.
  943. ///
  944. /// Makes a new allocation using an expired lease. The lease is updated with
  945. /// the information from the provided context. Typically, an expired lease
  946. /// which belonged to one client may be assigned to another client
  947. /// which asked for the specific address.
  948. ///
  949. /// @param expired An old, expired lease.
  950. /// @param ctx Message processing context. It holds various information
  951. /// extracted from the client's message and required to allocate a lease.
  952. ///
  953. /// @return Updated lease instance.
  954. /// @throw BadValue if trying to reuse a lease which is still valid or
  955. /// when the provided parameters are invalid.
  956. Lease4Ptr reuseExpiredLease4(Lease4Ptr& expired, ClientContext4& ctx);
  957. /// @brief Allocates the lease by replacing an existing lease.
  958. ///
  959. /// This method checks if the lease database contains the lease for
  960. /// the specified address. If the lease exists and has expired, it
  961. /// reuses the expired lease. If the lease doesn't exist, it creates
  962. /// the new lease.
  963. ///
  964. /// @param address Requested address for which the lease should be
  965. /// allocted.
  966. /// @param ctx Client context holding the data extracted from the
  967. /// client's message.
  968. ///
  969. /// @return A pointer to the allocated lease or NULL if the allocation
  970. /// was not successful.
  971. Lease4Ptr allocateOrReuseLease4(const asiolink::IOAddress& address,
  972. ClientContext4& ctx);
  973. /// @brief Allocates the lease from the dynamic pool.
  974. ///
  975. /// This method allocates the lease from the dynamic pool. It uses
  976. /// one of the allocators to pick addresses from the pool and if the
  977. /// address appears to be available, it allocates the new lease
  978. /// using this address. The number of attempts depends on the size
  979. /// of the dynamic pool. If all of the addresses in the pool have
  980. /// been tried and all of them appeared to be in use, the allocation
  981. /// fails. This is the case when the pool is exhausted.
  982. ///
  983. /// The time required to find a suitable lease depends on the current
  984. /// pool utilization.
  985. ///
  986. /// @param ctx Client context holding the data extracted from the
  987. /// client's message.
  988. ///
  989. /// @return A pointer to the allocated lease or NULL if the allocation
  990. /// was not successful.
  991. Lease4Ptr allocateUnreservedLease4(ClientContext4& ctx);
  992. /// @brief Updates the specified lease with the information from a context.
  993. ///
  994. /// The context, specified as an argument to this method, holds various
  995. /// information gathered from the client's message and passed to the
  996. /// allocation engine. The allocation engine uses this information to make
  997. /// lease allocation decisions. Some public methods of the allocation engine
  998. /// requires updating the lease information with the data gathered from the
  999. /// context, e.g. @c AllocEngine::reuseExpiredLease requires updating the
  1000. /// expired lease with fresh information from the context to create a
  1001. /// lease to be held for the client.
  1002. ///
  1003. /// Note that this doesn't update the lease address.
  1004. ///
  1005. /// @warning This method doesn't check if the pointer to the lease is
  1006. /// valid nor if the subnet to the pointer in the @c ctx is valid.
  1007. /// The caller is responsible for making sure that they are valid.
  1008. ///
  1009. /// @param [out] lease A pointer to the lease to be updated.
  1010. /// @param ctx A context containing information from the server about the
  1011. /// client and its message.
  1012. void updateLease4Information(const Lease4Ptr& lease,
  1013. ClientContext4& ctx) const;
  1014. /// @brief Extends the lease lifetime.
  1015. ///
  1016. /// This function is called to conditionally extend the lifetime of
  1017. /// the DHCPv4 or DHCPv6 lease. It is envisaged that this function will
  1018. /// make a decision if the lease lifetime should be extended, using
  1019. /// a preconfigured threshold, which would indicate how many percent
  1020. /// of the valid lifetime should have passed for the lease lifetime
  1021. /// to be extended. The lease lifetime would not be extended if
  1022. /// the threshold hasn't been reached.
  1023. ///
  1024. /// @todo Currently this function always extends the lease lifetime.
  1025. /// In the future, it will take the threshold value into account,
  1026. /// once the threshold is configurable.
  1027. ///
  1028. /// @param [in,out] lease A lease for which the lifetime should be
  1029. /// extended.
  1030. ///
  1031. /// @return true if the lease lifetime has been extended, false
  1032. /// otherwise.
  1033. bool conditionalExtendLifetime(Lease& lease) const;
  1034. };
  1035. /// @brief A pointer to the @c AllocEngine object.
  1036. typedef boost::shared_ptr<AllocEngine> AllocEnginePtr;
  1037. }; // namespace isc::dhcp
  1038. }; // namespace isc
  1039. #endif // ALLOC_ENGINE_H