alloc_engine.h 65 KB

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