lease.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Copyright (C) 2013-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef LEASE_H
  7. #define LEASE_H
  8. #include <asiolink/io_address.h>
  9. #include <dhcp/duid.h>
  10. #include <dhcp/option.h>
  11. #include <dhcp/hwaddr.h>
  12. namespace isc {
  13. namespace dhcp {
  14. /// @brief Unique identifier for a subnet (both v4 and v6)
  15. ///
  16. /// Let's copy SubnetID definition from subnet.h. We can't include it directly,
  17. /// because subnet.h needs Lease::Type, so it includes lease.h
  18. typedef uint32_t SubnetID;
  19. /// @brief a common structure for IPv4 and IPv6 leases
  20. ///
  21. /// This structure holds all information that is common between IPv4 and IPv6
  22. /// leases.
  23. struct Lease {
  24. /// @brief Type of lease or pool
  25. typedef enum {
  26. TYPE_NA = 0, ///< the lease contains non-temporary IPv6 address
  27. TYPE_TA = 1, ///< the lease contains temporary IPv6 address
  28. TYPE_PD = 2, ///< the lease contains IPv6 prefix (for prefix delegation)
  29. TYPE_V4 = 3 ///< IPv4 lease
  30. } Type;
  31. /// @brief returns text representation of a lease type
  32. /// @param type lease or pool type to be converted
  33. /// @return text decription
  34. static std::string typeToText(Type type);
  35. /// @name Common lease states constants.
  36. //@{
  37. ///
  38. /// @brief A lease in the default state.
  39. static const uint32_t STATE_DEFAULT;
  40. /// @brief Declined lease.
  41. static const uint32_t STATE_DECLINED;
  42. /// @brief Expired and reclaimed lease.
  43. static const uint32_t STATE_EXPIRED_RECLAIMED;
  44. /// @brief Returns name(s) of the basic lease state(s).
  45. ///
  46. /// @param state A numeric value holding a state information.
  47. /// Some states may be composite, i.e. the single state value
  48. /// maps to multiple logical states of the lease.
  49. ///
  50. /// @return Comma separated list of state names.
  51. static std::string basicStatesToText(const uint32_t state);
  52. /// @brief Constructor
  53. ///
  54. /// @param addr IP address
  55. /// @param t1 renewal time
  56. /// @param t2 rebinding time
  57. /// @param valid_lft Lifetime of the lease
  58. /// @param subnet_id Subnet identification
  59. /// @param cltt Client last transmission time
  60. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  61. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  62. /// @param hostname FQDN of the client which gets the lease.
  63. /// @param hwaddr Hardware/MAC address
  64. Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
  65. uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
  66. const bool fqdn_fwd, const bool fqdn_rev,
  67. const std::string& hostname,
  68. const HWAddrPtr& hwaddr);
  69. /// @brief Destructor
  70. virtual ~Lease() {}
  71. /// @brief IPv4 ot IPv6 address
  72. ///
  73. /// IPv4, IPv6 address or, in the case of a prefix delegation, the prefix.
  74. isc::asiolink::IOAddress addr_;
  75. /// @brief Renewal timer
  76. ///
  77. /// Specifies renewal time. Although technically it is a property of the
  78. /// IA container and not the address itself, since our data model does not
  79. /// define a separate IA entity, we are keeping it in the lease. In the
  80. /// case of multiple addresses/prefixes for the same IA, each must have
  81. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  82. uint32_t t1_;
  83. /// @brief Rebinding timer
  84. ///
  85. /// Specifies rebinding time. Although technically it is a property of the
  86. /// IA container and not the address itself, since our data model does not
  87. /// define a separate IA entity, we are keeping it in the lease. In the
  88. /// case of multiple addresses/prefixes for the same IA, each must have
  89. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  90. uint32_t t2_;
  91. /// @brief Valid lifetime
  92. ///
  93. /// Expressed as number of seconds since cltt.
  94. uint32_t valid_lft_;
  95. /// @brief Client last transmission time
  96. ///
  97. /// Specifies a timestamp giving the time when the last transmission from a
  98. /// client was received.
  99. time_t cltt_;
  100. /// @brief Subnet identifier
  101. ///
  102. /// Specifies the identification of the subnet to which the lease belongs.
  103. SubnetID subnet_id_;
  104. /// @brief Client hostname
  105. ///
  106. /// This field may be empty
  107. std::string hostname_;
  108. /// @brief Forward zone updated?
  109. ///
  110. /// Set true if the DNS AAAA record for this lease has been updated.
  111. bool fqdn_fwd_;
  112. /// @brief Reverse zone updated?
  113. ///
  114. /// Set true if the DNS PTR record for this lease has been updated.
  115. bool fqdn_rev_;
  116. /// @brief Client's MAC/hardware address
  117. ///
  118. /// This information may not be available in certain cases.
  119. HWAddrPtr hwaddr_;
  120. /// @brief Holds the lease state(s).
  121. ///
  122. /// This is the field that holds the lease state(s). Typically, a
  123. /// lease remains in a single states. However, it is posible to
  124. /// define a value for state which indicates that the lease remains
  125. /// in multiple logical states.
  126. ///
  127. /// The defined states are represented by the "STATE_*" constants
  128. /// belonging to this class.
  129. uint32_t state_;
  130. /// @brief Convert Lease to Printable Form
  131. ///
  132. /// @return String form of the lease
  133. virtual std::string toText() const = 0;
  134. /// @brief returns true if the lease is expired
  135. /// @return true if the lease is expired
  136. bool expired() const;
  137. /// @brief Indicates if the lease is in the "expired-reclaimed" state.
  138. ///
  139. /// @return true if the lease is in the "expired-reclaimed" state, false
  140. /// otherwise.
  141. bool stateExpiredReclaimed() const;
  142. /// @brief Indicates if the lease is in the "declined" state.
  143. ///
  144. /// @return true if the lease is in the "declined" state, false otherwise.
  145. bool stateDeclined() const;
  146. /// @brief Returns true if the other lease has equal FQDN data.
  147. ///
  148. /// @param other Lease which FQDN data is to be compared with our lease.
  149. ///
  150. /// @return Boolean value which indicates whether FQDN data of the other
  151. /// lease is equal to the FQDN data of our lease (true) or not (false).
  152. bool hasIdenticalFqdn(const Lease& other) const;
  153. /// @brief Returns raw (as vector) hardware address
  154. ///
  155. /// This method is needed in multi-index container as key extractor.
  156. /// The const reference is only valid as long as the object that returned it.
  157. /// In the unlikely case when Lease4 does not have a hardware address,
  158. /// the function will return an empty vector.
  159. ///
  160. /// @return const reference to the hardware address
  161. const std::vector<uint8_t>& getHWAddrVector() const;
  162. /// @brief Returns lease expiration time.
  163. ///
  164. /// The lease expiration time is a sum of a client last transmission time
  165. /// and valid lifetime.
  166. int64_t getExpirationTime() const;
  167. /// @brief Sets lease to DECLINED state.
  168. ///
  169. /// All client identifying parameters will be stripped off (HWaddr,
  170. /// client_id, hostname), timers set to 0 (t1, t2), cltt will be set
  171. /// to current time and valid_lft to parameter specified as probation
  172. /// period. Note that This method only sets fields in the structure.
  173. /// It is caller's responsibility to clean up DDNS, bump up stats,
  174. /// log, call hooks ets.
  175. ///
  176. /// @param probation_period lease lifetime will be set to this value
  177. virtual void decline(uint32_t probation_period) = 0;
  178. };
  179. /// @brief Structure that holds a lease for IPv4 address
  180. ///
  181. /// For performance reasons it is a simple structure, not a class. If we chose
  182. /// make it a class, all fields would have to made private and getters/setters
  183. /// would be required. As this is a critical part of the code that will be used
  184. /// extensively, direct access is warranted.
  185. struct Lease4 : public Lease {
  186. /// @brief Client identifier
  187. ///
  188. /// @todo Should this be a pointer to a client ID or the ID itself?
  189. /// Compare with the DUID in the Lease6 structure.
  190. ClientIdPtr client_id_;
  191. /// @brief Constructor
  192. ///
  193. /// @param addr IPv4 address.
  194. /// @param hwaddr A pointer to HWAddr structure
  195. /// @param clientid Client identification buffer
  196. /// @param clientid_len Length of client identification buffer
  197. /// @param valid_lft Lifetime of the lease
  198. /// @param t1 renewal time
  199. /// @param t2 rebinding time
  200. /// @param cltt Client last transmission time
  201. /// @param subnet_id Subnet identification
  202. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  203. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  204. /// @param hostname FQDN of the client which gets the lease.
  205. Lease4(const isc::asiolink::IOAddress& addr, const HWAddrPtr& hwaddr,
  206. const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
  207. uint32_t t1, uint32_t t2, time_t cltt, uint32_t subnet_id,
  208. const bool fqdn_fwd = false, const bool fqdn_rev = false,
  209. const std::string& hostname = "")
  210. : Lease(addr, t1, t2, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
  211. hostname, hwaddr) {
  212. if (clientid_len) {
  213. client_id_.reset(new ClientId(clientid, clientid_len));
  214. }
  215. }
  216. /// @brief Constructor.
  217. ///
  218. /// @param address IPv4 address.
  219. /// @param hw_address Pointer to client's HW addresss.
  220. /// @param client_id pointer to the client id structure.
  221. /// @param valid_lifetime Valid lifetime value.
  222. /// @param t1 Renew timer.
  223. /// @param t2 Rebind timer.
  224. /// @param cltt Timestamp when the lease is acquired, renewed.
  225. /// @param subnet_id Subnet identifier.
  226. /// @param fqdn_fwd Forward DNS update performed.
  227. /// @param fqdn_rev Reverse DNS update performed.
  228. /// @param hostname Client's name for the DNS update..
  229. Lease4(const isc::asiolink::IOAddress& address,
  230. const HWAddrPtr& hw_address,
  231. const ClientIdPtr& client_id,
  232. const uint32_t valid_lifetime,
  233. const uint32_t t1,
  234. const uint32_t t2,
  235. const time_t cltt,
  236. const SubnetID subnet_id,
  237. const bool fqdn_fwd = false,
  238. const bool fqdn_rev = false,
  239. const std::string& hostname = "");
  240. /// @brief Default constructor
  241. ///
  242. /// Initialize fields that don't have a default constructor.
  243. Lease4() : Lease(0, 0, 0, 0, 0, 0, false, false, "", HWAddrPtr())
  244. {
  245. }
  246. /// @brief Copy constructor
  247. ///
  248. /// @param other the @c Lease4 object to be copied.
  249. Lease4(const Lease4& other);
  250. /// @brief Returns name of the lease states specific to DHCPv4.
  251. ///
  252. /// @todo Currently it simply returns common states for DHCPv4 and DHCPv6.
  253. /// This method will have to be extended to handle DHCPv4 specific states
  254. /// when they are defined.
  255. ///
  256. /// @param state Numeric value holding lease states.
  257. /// @return Comma separated list of lease state names.
  258. static std::string statesToText(const uint32_t state);
  259. /// @brief Returns a client identifier.
  260. ///
  261. /// @warning Since the function returns the reference to a vector (not a
  262. /// copy), the returned object should be used with caution because it will
  263. /// remain valid only for the period of time when an object which returned
  264. /// it exists.
  265. ///
  266. /// @return A reference to a vector holding client identifier,
  267. /// or an empty vector if client identifier is NULL.
  268. const std::vector<uint8_t>& getClientIdVector() const;
  269. /// @brief Check if the lease belongs to the client with the given
  270. /// identifiers.
  271. ///
  272. /// This method checks if the lease belongs to the client using the
  273. /// specified HW address and/or client identifier. Note that any of the
  274. /// pointers passed to this method may be set to null, in which case
  275. /// they are treated as unspecified and are not used for matching the
  276. /// client with the lease.
  277. ///
  278. /// According to the DHCPv4 specifications, the client identifier takes
  279. /// precedence over the HW address when identifying the lease for the
  280. /// client on the server side. In particular, the RFC4361 introduces the
  281. /// use of DUID for DHCPv4 which should be a stable identifier for the
  282. /// client. The use of stable identifier allows for the correlation of the
  283. /// DHCPv4 and DHCPv6 clients in the dual stack networks. It also allows
  284. /// for allocating the same lease to the client which hardware (and thus
  285. /// MAC address) has changed.
  286. ///
  287. /// By default, Kea respects the precedence of the client identifier over
  288. /// MAC address and when this method finds the match of the client
  289. /// identifier with the client identifier stored in the lease, it will
  290. /// treat the lease as the lease of this client, even when the HW
  291. /// address doesn't match.
  292. ///
  293. /// The HW address is used for matching the client with the lease only
  294. /// when the lease is not associated with any client identifier (client
  295. /// identifier for the lease is null) or when the client identifier
  296. /// parameter passed to this method is null. This facilitates the following
  297. /// cases:
  298. /// - client didn't generate client identifier and is only using the chaddr
  299. /// field to identify itself.
  300. /// - server's administrator configured the server to NOT match client
  301. /// identifiers, the client obtained the new lease, and the administrator
  302. /// reconfigured the server to match the client identifiers. The client
  303. /// is trying to renew its lease and both the client identifier and HW
  304. /// address is used for matching the lease which doesn't have the record
  305. /// of the client identifier.
  306. /// - client obtained the lease using the HW address and client identifier,
  307. /// the server's administrator configured the server to NOT match the
  308. /// client identifiers, and the client returns to renew the lease. This
  309. /// time, the lease has a record of both client identifier and the HW
  310. /// address but only the HW address is used for matching the client to
  311. /// the lease.
  312. ///
  313. /// Note that the typical case when the server's administrator may want to
  314. /// disable matching the client identifier passed in the client's message
  315. /// is when the client is performing multi-stage boot. In such case, the
  316. /// client identifiers may change on various stages of the boot, but the
  317. /// HW address will remain stable. The server's administrator prefers
  318. /// using the HW address for client identification in this case.
  319. ///
  320. /// It may also be useful to disable matching client identifiers to
  321. /// mitigate the problem of broken client implementations which generate
  322. /// new client identifiers every time they connect to the network.
  323. ///
  324. /// @param hw_address Pointer to the HW address of the client.
  325. /// @param client_id Pointer to the client identifier structure.
  326. ///
  327. /// @return true if the lease belongs to the client using the specified
  328. /// hardware address and/or client identifier.
  329. bool belongsToClient(const HWAddrPtr& hw_address,
  330. const ClientIdPtr& client_id) const;
  331. /// @brief Assignment operator.
  332. ///
  333. /// @param other the @c Lease4 object to be assigned.
  334. Lease4& operator=(const Lease4& other);
  335. /// @brief Compare two leases for equality
  336. ///
  337. /// @param other lease6 object with which to compare
  338. bool operator==(const Lease4& other) const;
  339. /// @brief Compare two leases for inequality
  340. ///
  341. /// @param other lease6 object with which to compare
  342. bool operator!=(const Lease4& other) const {
  343. return (!operator==(other));
  344. }
  345. /// @brief Convert lease to printable form
  346. ///
  347. /// @return Textual represenation of lease data
  348. virtual std::string toText() const;
  349. /// @brief Sets IPv4 lease to declined state.
  350. ///
  351. /// See @ref Lease::decline for detailed description.
  352. ///
  353. /// @param probation_period valid lifetime will be set to this value
  354. void decline(uint32_t probation_period);
  355. /// @todo: Add DHCPv4 failover related fields here
  356. };
  357. /// @brief Pointer to a Lease4 structure.
  358. typedef boost::shared_ptr<Lease4> Lease4Ptr;
  359. /// @brief A collection of IPv4 leases.
  360. typedef std::vector<Lease4Ptr> Lease4Collection;
  361. /// @brief Structure that holds a lease for IPv6 address and/or prefix
  362. ///
  363. /// For performance reasons it is a simple structure, not a class. If we chose
  364. /// make it a class, all fields would have to made private and getters/setters
  365. /// would be required. As this is a critical part of the code that will be used
  366. /// extensively, direct access is warranted.
  367. struct Lease6 : public Lease {
  368. /// @brief Lease type
  369. ///
  370. /// One of normal address, temporary address, or prefix.
  371. Lease::Type type_;
  372. /// @brief IPv6 prefix length
  373. ///
  374. /// This is used only for prefix delegations and is ignored otherwise.
  375. uint8_t prefixlen_;
  376. /// @brief Identity Association Identifier (IAID)
  377. ///
  378. /// DHCPv6 stores all addresses and prefixes in IA containers (IA_NA,
  379. /// IA_TA, IA_PD). All containers may appear more than once in a message.
  380. /// To differentiate between them, the IAID field is present
  381. uint32_t iaid_;
  382. /// @brief Client identifier
  383. DuidPtr duid_;
  384. /// @brief preferred lifetime
  385. ///
  386. /// This parameter specifies the preferred lifetime since the lease was
  387. /// assigned or renewed (cltt), expressed in seconds.
  388. uint32_t preferred_lft_;
  389. /// @todo: Add DHCPv6 failover related fields here
  390. /// @brief Constructor
  391. /// @param type Lease type.
  392. /// @param addr Assigned address.
  393. /// @param duid A pointer to an object representing DUID.
  394. /// @param iaid IAID.
  395. /// @param preferred Preferred lifetime.
  396. /// @param valid Valid lifetime.
  397. /// @param t1 A value of the T1 timer.
  398. /// @param t2 A value of the T2 timer.
  399. /// @param subnet_id A Subnet identifier.
  400. /// @param hwaddr hardware/MAC address (optional)
  401. /// @param prefixlen An address prefix length (optional, defaults to 128)
  402. Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  403. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  404. uint32_t t2, SubnetID subnet_id, const HWAddrPtr& hwaddr = HWAddrPtr(),
  405. uint8_t prefixlen = 128);
  406. /// @brief Constructor, including FQDN data.
  407. ///
  408. /// @param type Lease type.
  409. /// @param addr Assigned address.
  410. /// @param duid A pointer to an object representing DUID.
  411. /// @param iaid IAID.
  412. /// @param preferred Preferred lifetime.
  413. /// @param valid Valid lifetime.
  414. /// @param t1 A value of the T1 timer.
  415. /// @param t2 A value of the T2 timer.
  416. /// @param subnet_id A Subnet identifier.
  417. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  418. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  419. /// @param hostname FQDN of the client which gets the lease.
  420. /// @param hwaddr hardware address (MAC), may be NULL
  421. /// @param prefixlen An address prefix length (optional, defaults to 128)
  422. Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  423. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  424. uint32_t t2, SubnetID subnet_id, const bool fqdn_fwd,
  425. const bool fqdn_rev, const std::string& hostname,
  426. const HWAddrPtr& hwaddr = HWAddrPtr(), uint8_t prefixlen = 128);
  427. /// @brief Constructor
  428. ///
  429. /// Initialize fields that don't have a default constructor.
  430. Lease6();
  431. /// @brief Returns name of the lease states specific to DHCPv6.
  432. ///
  433. /// @todo Currently it simply returns common states for DHCPv4 and DHCPv6.
  434. /// This method will have to be extended to handle DHCPv6 specific states
  435. /// when they are defined.
  436. ///
  437. /// @param state Numeric value holding lease states.
  438. /// @return Comma separated list of lease state names.
  439. static std::string statesToText(const uint32_t state);
  440. /// @brief Returns a reference to a vector representing a DUID.
  441. ///
  442. /// @warning Since the function returns the reference to a vector (not a
  443. /// copy), the returned object should be used with caution because it will
  444. /// remain valid only for the period of time when an object which returned
  445. /// it exists.
  446. ///
  447. /// @return A reference to a vector holding a DUID.
  448. const std::vector<uint8_t>& getDuidVector() const;
  449. /// @brief Sets IPv6 lease to declined state.
  450. ///
  451. /// See @ref Lease::decline for detailed description.
  452. ///
  453. /// @param probation_period valid lifetime will be set to this value
  454. void decline(uint32_t probation_period);
  455. /// @brief Compare two leases for equality
  456. ///
  457. /// @param other lease6 object with which to compare
  458. bool operator==(const Lease6& other) const;
  459. /// @brief Compare two leases for inequality
  460. ///
  461. /// @param other lease6 object with which to compare
  462. bool operator!=(const Lease6& other) const {
  463. return (!operator==(other));
  464. }
  465. /// @brief Convert Lease to Printable Form
  466. ///
  467. /// @return String form of the lease
  468. virtual std::string toText() const;
  469. };
  470. /// @brief Pointer to a Lease6 structure.
  471. typedef boost::shared_ptr<Lease6> Lease6Ptr;
  472. /// @brief Pointer to a const Lease6 structure.
  473. typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
  474. /// @brief A collection of IPv6 leases.
  475. typedef std::vector<Lease6Ptr> Lease6Collection;
  476. /// @brief Stream output operator.
  477. ///
  478. /// Dumps the output of Lease::toText to the given stream.
  479. /// @param os output stream to which the output is
  480. /// @param lease reference to Lease object to dump
  481. /// @return a reference to the output stream paramater
  482. std::ostream&
  483. operator<<(std::ostream& os, const Lease& lease);
  484. }; // end of isc::dhcp namespace
  485. }; // end of isc namespace
  486. #endif // LEASE_H