lease.h 22 KB

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