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