alloc_engine.h 48 KB

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