alloc_engine.h 49 KB

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