alloc_engine.h 67 KB

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