alloc_engine.h 70 KB

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