libdhcpsrv.dox 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Copyright (C) 2012-2016 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. /**
  7. @page libdhcpsrv libdhcpsrv - Server DHCP library
  8. This library contains code used for the DHCPv4 and DHCPv6 servers' operations,
  9. including the "Lease Manager" that manages information about leases and the
  10. "Configuration Manager" that stores the servers' configuration etc.
  11. The code here is server specific. For generic (useful to the server,
  12. client, relay and other tools like perfdhcp) code, please see
  13. \ref libdhcp.
  14. This library contains several crucial elements for the operation of the DHCP server:
  15. - isc::dhcp::LeaseMgr - lease manager is the name for the database backend that stores
  16. leases.
  17. - isc::dhcp::CfgMgr - configuration manager that holds DHCP specific
  18. configuration information (subnets, pools, options, timer values etc.) in
  19. easy to use format.
  20. - isc::dhcp::AllocEngine - allocation engine that handles new requests and allocates new
  21. leases.
  22. - isc::dhcp::HostMgr - manager for static reservations (a.k.a. host reservations).
  23. - isc::dhcp::D2ClientMgr - DHCP-DDNS (D2) client manager which is responsible for
  24. the communication between the DHCP server and the D2 component.
  25. - isc::dhcp::Dhcp4o6IpcBase - common part (base class) of DHCPv4-over-DHCPv6
  26. inter server communication (aka IPC).
  27. @section leasemgr Lease Manager
  28. LeaseMgr provides a common, unified abstract API for all database backends. All
  29. backends are derived from the base class isc::dhcp::LeaseMgr. Currently Kea
  30. supports three backends, implemented in the following classes:
  31. - isc::dhcp::Memfile_LeaseMgr - stores leases in a CSV file,
  32. - isc::dhcp::MySqlLeaseMgr - stores leases in a MySQL database
  33. - isc::dhcp::PgSqlLeaseMgr - stores leases in a PostgreSQL database
  34. @section cfgmgr Configuration Manager
  35. Configuration Manager (\ref isc::dhcp::CfgMgr) is a singleton object which
  36. holds configuration information necessary for the operation of Kea daemons.
  37. A complete collection of information for the daemon is stored in the
  38. \ref isc::dhcp::SrvConfig object. Internally, the Configuration Manager
  39. holds a list of \ref isc::dhcp::SrvConfig objects, from which one
  40. is marked as "current configuration".
  41. When the server starts up or is being reconfigured a new
  42. \ref isc::dhcp::SrvConfig object, referred to as "staging configuration",
  43. is created. The staging configuration is held at the tip of the list of
  44. configurations. The object can be accessed by calling the
  45. \ref isc::dhcp::CfgMgr::getStagingCfg. This object can be accessed
  46. from different stages of the configuration parsing and modified as needed.
  47. Modifications of the staging configuration do not affect the current
  48. configuration. The staging configuration is unused until the
  49. \ref isc::dhcp::CfgMgr::commit function is called. This exception safe method
  50. marks the staging object as "current configuration". The const pointer to the
  51. current configuration can be accessed by calling a
  52. \ref isc::dhcp::CfgMgr::getCurrentCfg.
  53. The staging configuration can be discarded at any time before it is committed
  54. by calling the \ref isc::dhcp::CfgMgr::rollback. This removes the
  55. \ref isc::dhcp::SrvConfig object from the Configuration Manager. When
  56. the \ref isc::dhcp::CfgMgr::getStagingCfg is called again a fresh/default
  57. \ref isc::dhcp::SrvConfig object is returned.
  58. The Configuration Manager stores previous configurations, i.e. configurations
  59. which occurred prior to the most current configuration. This is currently
  60. unused (except for unit tests) by the daemons, but in the future this
  61. mechanism can be used to trigger a rollover of the server configuration
  62. to a last good configuration that the administrator prefers.
  63. The previous configurations are identified by the value which specifies a
  64. distance between the current configuration and the previous
  65. configuration. For example: the value of 1 identifies an immediate
  66. predecessor of the current configuration, the value of 2 identifies the
  67. one that occurred before it etc.
  68. @section hostmgr Host Manager
  69. Host Manager implemented by the \ref isc::dhcp::HostMgr is a singleton object
  70. which provides means to retrieve resources statically assigned to the DHCP
  71. clients, such as IP addresses, prefixes or hostnames. The statically assigned
  72. resources are called reservations (or host reservations) and they are
  73. represented in the code by the \ref isc::dhcp::Host class.
  74. The reservations can be specified in the configuration file or in some
  75. other storage (typically in a database). A dedicated object, called
  76. host data source, is needed to retrieve the host reservations from the
  77. database. This object must implement the \ref isc::dhcp::BaseHostDataSource
  78. interface and its implementation is specific to the type of storage
  79. holding the reservations. For example, the host data source managing
  80. host reservations in the MySQL database is required to establish
  81. connection to the MySQL database and issue specific queries. Once
  82. implemented, the \ref isc::dhcp::HostMgr::create method must be updated
  83. to create an instance of this datasource. Note, that this instance is
  84. created as "alternate host data source" as opposed to the primary data
  85. source which returns host reservations specified in the configuration file.
  86. The primary data source is implemented internally in the
  87. \ref isc::dhcp::HostMgr and uses the configuration data structures held by
  88. the \ref isc::dhcp::CfgMgr to retrieve the reservations. In general, the
  89. \ref isc::dhcp::HostMgr first searches for the reservations using the
  90. primary data source and falls back to the use of alternate data source
  91. when nothing has been found. For those methods which are meant to return
  92. multiple reservations (e.g. find all reservations for the particular
  93. client), the \ref isc::dhcp::HostMgr will use both primary and alternate
  94. data source (if present) and concatenate results.
  95. For more information about the \ref isc::dhcp::HostMgr please refer to its
  96. documentation.
  97. @subsection postgreSQLHostMgr PostgreSQL Host Reservation Management
  98. Storing and retrieving host reservations within a PostgreSQL schema is
  99. provided by the class, \ref isc::dhcp::PgSqlHostDataSource, a derivation of
  100. \ref isc::dhcp::BaseHostDataSource and is depicted in the following
  101. class diagram:
  102. @image html pgsql_host_data_source.svg "PgSqlHostDataSource Class Diagram"
  103. @section optionsConfig Options Configuration Information
  104. The \ref isc::dhcp::CfgOption object holds a collection of options being
  105. sent to the client. Since each subnet comes with a distinct set of
  106. options, every \ref isc::dhcp::Subnet object holds its own copy of the
  107. \ref isc::dhcp::CfgOption object with specific options.
  108. The DHCP server also allows for configuration of "global" options
  109. which are shared by all subnets. The rule here is that if a particular
  110. option appears in the global options set and the subnet specific options
  111. set, the subnet specific option takes precedence. The global options
  112. configuration is held in the dedicated instance of the
  113. \ref isc::dhcp::CfgOption class. This instance is owned by the
  114. \ref isc::dhcp::SrvConfig class.
  115. When the new configuration is parsed, the global options are merged into
  116. the \ref isc::dhcp::CfgOption instances for all subnets. This is
  117. causing some overhead during the reconfiguration of the server but on
  118. the other hand it avoids the lookup of options in two places (among
  119. subnet specific options and global options) during each packet
  120. processing.
  121. One of the benefits of keeping a separate set of global options is
  122. that there may be cases when the server administrator doesn't specify
  123. any subnet configuration and only wants global options to be used.
  124. This is the case, when the DHCP server is used for stateless
  125. configuration, i.e. client's are not allocated an address or prefix,
  126. and only stateless configuration is handed out.
  127. @section allocengine Allocation Engine
  128. Allocation Engine (\ref isc::dhcp::AllocEngine) is what its name say - an engine
  129. that handles allocation of new leases. It takes parameters that the client
  130. provided (client-id, DUID, subnet, a hint if the user provided one, etc.) and
  131. then attempts to allocate a lease.
  132. There is no single best solution to the address assignment problem. Server
  133. is expected to pick an address from its available pools is currently not used.
  134. There are many possible algorithms that can do that, each with its own advantages
  135. and drawbacks. This allocation engine must provide robust operation is radically
  136. different scenarios, so there address selection problem was abstracted into
  137. separate module, called allocator. Its sole purpose is to pick an address from
  138. a pool. Allocation engine will then check if the picked address is free and if
  139. it is not, then will ask allocator to pick again.
  140. At least 3 allocators will be implemented:
  141. - Iterative - it iterates over all resources (addresses or prefixes) in
  142. available pools, one by one. The advantages of this approach are: speed
  143. (typically it only needs to increase address just one), the guarantee to cover
  144. all addresses and predictability. This allocator behaves reasonably good in
  145. case of nearing depletion. Even when pools are almost completely allocated, it
  146. still will be able to allocate outstanding leases efficiently. Predictability
  147. can also be considered a serious flaw in some environments, as prediction of the
  148. next address is trivial and can be leveraged by an attacker. Another drawback of
  149. this allocator is that it does not attempt to give the same address to returning
  150. clients (clients that released or expired their leases and are requesting a new
  151. lease will likely to get a different lease). This allocator is not suitable for
  152. temporary addresses, which must be randomized. This allocator is implemented
  153. in \ref isc::dhcp::AllocEngine::IterativeAllocator.
  154. - Hashed - ISC-DHCP uses hash of the client-id or DUID to determine, which
  155. address is tried first. If that address is not available, the result is hashed
  156. again. That procedure is repeated until available address is found or there
  157. are no more addresses left. The benefit of that approach is that it provides
  158. a relative lease stability, so returning old clients are likely to get the same
  159. address again. The drawbacks are increased computation cost, as each iteration
  160. requires use of a hashing function. That is especially difficult when the
  161. pools are almost depleted. It also may be difficult to guarantee that the
  162. repeated hashing will iterate over all available addresses in all pools. Flawed
  163. hash algorithm can go into cycles that iterate over only part of the addresses.
  164. It is difficult to detect such issues as only some initial seed (client-id
  165. or DUID) values may trigger short cycles. This allocator is currently not
  166. implemented. This will be the only allocator allowed for temporary addresses.
  167. - Random - Another possible approach to address selection is randomization. This
  168. allocator can pick an address randomly from the configured pool. The benefit
  169. of this approach is that it is easy to implement and makes attacks based on
  170. address prediction more difficult. The drawback of this approach is that
  171. returning clients are almost guaranteed to get a different address. Another
  172. drawback is that with almost depleted pools it is increasingly difficult to
  173. "guess" an address that is free. This allocator is currently not implemented.
  174. @subsection allocEngineTypes Different lease types support
  175. Allocation Engine has been extended to support different types of leases. Four
  176. types are supported: TYPE_V4 (IPv4 addresses), TYPE_NA (normal IPv6 addresses),
  177. TYPE_TA (temporary IPv6 addresses) and TYPE_PD (delegated prefixes). Support for
  178. TYPE_TA is partial. Some routines are able to handle it, while other are
  179. not. The major missing piece is the RandomAllocator, so there is no way to randomly
  180. generate an address. This defeats the purpose of using temporary addresses for now.
  181. @subsection allocEnginePD Prefix Delegation support in AllocEngine
  182. The Allocation Engine supports allocation of the IPv6 addresses and prefixes.
  183. For a prefix pool, the iterative allocator "walks over"
  184. every available pool. It is similar to how it iterates over address pool,
  185. but instead of increasing address by just one, it walks over the whole delegated
  186. prefix length in one step. This is implemented in
  187. isc::dhcp::AllocEngine::IterativeAllocator::increasePrefix(). Functionally the
  188. increaseAddress(addr) call is equivalent to increasePrefix(addr, 128)
  189. (increasing by a /128 prefix, i.e. a single address). However, both methods are
  190. kept, because increaseAddress() is faster and this is a routine that may be
  191. called many hundred thousands times per second.
  192. @subsection allocEngineDHCPv4HostReservation Host Reservation support
  193. The Allocation Engine supports allocation of statically assigned addresses
  194. to the DHCPv4 clients, a.k.a. Host Reservation.
  195. When the server receives a DHCPDISCOVER or DHCPREQUEST from the client it
  196. calls \ref isc::dhcp::AllocEngine::allocateLease4 to obtain the suitable lease
  197. for the client. If the Allocation Engine determines that the particular client
  198. has a reservation it will try to allocate a reserved address for it. If the
  199. client requested allocation or renewal of a different address, the Allocation
  200. Engine will respond with a NULL lease to indicate that the address
  201. desired by the client could not be assigned. The DHCP server should send
  202. a DHCPNAK to the client and the client should fall back to the DHCP
  203. server discovery. When the client sends DHCPDISCOVER, the Allocation
  204. Engine offers the reserved address and the client should request the
  205. offered address in subsequent DHCPREQUEST messages.
  206. There are cases when the Allocation Engine is unable to assign the
  207. reserved address for the client. This includes the situations when
  208. the address had been previously reserved for another client or the
  209. address had been assigned out of the dynamic address pool. Such address
  210. may still remain in use of the client which obtained it first and the
  211. Allocation Engine must not assign it to the client for which it is
  212. reserved until the client using this address releases or the server
  213. assigns a different address for it.
  214. In order to resolve this conflict the Allocation Engine will refuse to
  215. renew the lease for the client using the address not reserved for it.
  216. This client should fall back to the 4-way exchange and the Allocation
  217. Engine will assign a different address. As a result, the reserved
  218. address will be freed for the use of the client for which the reservation
  219. was made. The client will be offered/allocated a reserved address
  220. the next time it retries sending a DHCPDISCOVER/DHCPREQUEST message to
  221. the server.
  222. @section timerManager Timer Manager
  223. The fundamental role of the DHCP server is to receive and process DHCP
  224. messages received over the sockets opened on network interfaces. The
  225. servers' include the main loops in which the servers passively wait
  226. for the messages. This is done by calling the
  227. @c isc::dhcp::IfaceMgr::receive4 and/or @c isc::dhcp::IfaceMgr::receive6
  228. methods for DHCPv4 and DHCPv6 server respectively. Internally, these
  229. methods call @c select() on open sockets, which blocks for a
  230. specified amount of time.
  231. The implication of using the @c select() is that the server has no
  232. means to process any "events" while it is waiting for the @c select()
  233. to return. An example of such an event is the expiration of the timer
  234. which controls when the server should detect and process expired
  235. leases.
  236. The @c isc::dhcp::TimerMgr has been created to address the issue of
  237. processing expired leases according to the the dedicated timer.
  238. Nevertheless, this concept is universal and should be used for
  239. all timers which need to be triggered asynchronously, i.e. independently
  240. from processing the DHCP messages.
  241. The @c TimerMgr allows for registering timers and associating them with
  242. user callback functions, which are executed without waiting for the
  243. call to the @c select() function to return as a result of the timeout.
  244. When the particular timer elapses, the blocking call to select is
  245. interrupted by sending data over a dedicated (for a timer)
  246. @c isc::util::WatchSocket. Each timer has an instance of
  247. @c isc::util::WatchSocket associated with it, and each such socket
  248. is registered with the @c IfaceMgr using the @c IfaceMgr::addExternalSocket.
  249. When the transmission of the data over the watch socket interrupts the
  250. @c select() call, the user callback is executed by
  251. @c isc::dhcp::IfaceMgr and the watch socket is cleared to accept
  252. subsequent events for that particular timer.
  253. The timers are implemented using the @c isc::asiolink::IntervalTimer class.
  254. They are run in a dedicated thread which is owned (created and destroyed)
  255. by @c isc::dhcp::TimerMgr. This worker thread runs an instance
  256. of @c isc::asiolink::IOService object which is associated with all
  257. registered timers. The thread uses a common callback function which
  258. is executed when a timer elapses. This callback function receives
  259. a name of the elapsed timer as an argument and, based on that, selects the
  260. appropriate @c isc::util::WatchSocket to be marked as ready. In order to
  261. overcome the race conditions with the main thread, the worker thread blocks
  262. right after it marks the watch socket as ready, and waits for this
  263. socket to be cleared by the main thread. This is the indication
  264. that the timer specific callback function has been invoked and the
  265. worker thread may continue monitoring registered timers and signal
  266. their readiness when they elapse.
  267. @section leaseReclamationRoutine Leases Reclamation Routine
  268. Lease reclamation is the process in which the expired lease becomes
  269. available for re-assignment to the same or another client. When the
  270. server reclaims the lease it executes the callouts registered for the
  271. "lease4_expire" and "lease6_expire" hook points, performs the DNS update
  272. to remove any DNS records associated with the expired lease, and finally
  273. marks a lease as reclaimed in the lease database. The lease may be
  274. marked as reclaimed by setting its state to @c Lease::STATE_EXPIRED_RECLAIMED
  275. or by being removed from the database.
  276. Reclamation is performed periodically for a bulk of expired
  277. leases in the lease reclamation routine. The lease reclamation routines
  278. for both DHCP servers are implemented in the @c isc::dhcp::AllocEngine:
  279. - @c isc::dhcp::AllocEngine::reclaimExpiredLeases4 (DHCPv4)
  280. - @c isc::dhcp::AllocEngine::reclaimExpiredLeases6 (DHCPv6)
  281. Note that besides the reclamation of the leases, these methods also
  282. update the relevant statistics, i.e. decrease the number of assigned
  283. leases and increase the number of reclaimed leases.
  284. The reclamation routines are executed periodically according to
  285. the server configuration (see the documentation for the
  286. "expired-leases-processing" configuration map). Internally, they are
  287. registered as callback functions in the @c isc::dhcp::TimerMgr
  288. (see @ref timerManager for the details), during the servers' startup
  289. or reconfiguration.
  290. Execution of the reclamation routine may take a relatively
  291. long period of time. It depends on the complexity of the callouts,
  292. whether the DNS update is required for leases, and the type of the
  293. lease database used. While the reclamation routine is
  294. executed, the server will not process any DHCP messages to avoid
  295. race conditions being a result of concurrent access to the lease
  296. database to allocate and reclaim leases. To make sure that the
  297. server remains responsive, it is possible to limit the number of
  298. leases being processed by the leases reclamation routine and/or
  299. limit the time for the reclamation routine to process
  300. leases. Both limits are specified in the respective arguments
  301. passed to the lease reclamation routines.
  302. As mentioned above, reclaimed leases may be marked as such, by
  303. updating their state to @c Lease::STATE_EXPIRED_RECLAIMED or by
  304. being removed. This behavior is controlled by the boolean parameter
  305. passed to the reclamation routine. The first approach is desired
  306. when the server should provide "lease affinity", i.e. ability to
  307. re-assign the same lease to the returning client. By only
  308. updating the lease state, the server preserves association of the
  309. lease with a particular client. When that client returns the
  310. server may assign the same lease to the client, assuming that this
  311. lease is still available. The lease is removed during the
  312. reclamation when the lease affinity is not required and it is
  313. preferred to not keep redundant information (about expired
  314. leases) in the lease database.
  315. If the reclaimed leases are not removed, they are held in the
  316. database for a specified amount of time after their expiration.
  317. Each reclaimed lease is removed when this time elapses for it.
  318. The @c isc::dhcp::LeaseMgr::deleteExpiredReclaimedLeases4 and
  319. @c isc::dhcp::LeaseMgr::deleteExpiredReclaimedLeases6 are used
  320. to remove those leases for which the specified amount of time
  321. since expiration elapsed. These methods are executed periodically
  322. by the DHCP servers using the dedicated timers registered in the
  323. @c isc::dhcp::TimerMgr.
  324. @section subnetSelect Subnet Selection
  325. An important service offered by this library is the subnet selection
  326. from a query packet.
  327. @subsection dhcp4SubnetSelect DHCPv4 Subnet Selection
  328. Selectors (i.e., members of @c SubnetSelector class) are:
  329. - incoming interface name
  330. - gateway address - giaddr field
  331. - client address - ciaddr field
  332. - local address
  333. - remote address
  334. - option select - from the first Relay Agent Link Selection suboption or
  335. from a Subnet Selection option
  336. - client classes - used to reject a matching rule and try next rules
  337. First use the option select, next if the message was relayed (not undefined
  338. gateway address) use the gateway address as a subnet relay address.
  339. If a subnet was not already selected choose an address between:
  340. - if the gateway address is not undefined the gateway address
  341. - if the client address is not undefined and the local address not the
  342. broadcast address (i.e., renew or rebind) the client address
  343. - if the remote address is not undefined and the local address not the
  344. broadcast address (i.e., renew or rebind) the remote address
  345. - at this point try the interface name as a subnet interface
  346. - if the interface name does not select a subnet choose the interface address
  347. (last resort)
  348. Match the chosen address in a subnet address range.
  349. @subsection dhcp4o6SubnetSelect DHCPv4-over-DHCPv6 Subnet Selection
  350. Selectors (i.e., members of @c SubnetSelector class) are:
  351. - incoming interface name
  352. - gateway address - giaddr field (should be always undefined)
  353. - client address - ciaddr field
  354. - local address - set to the interface IPv4 address
  355. - remote address - IPv6 address
  356. - option select - from a Subnet Selection option
  357. - first relay link address - (IPv6) undefined or the first relay link
  358. address which is not undefined or link local (i.e., usable)
  359. - interface ID - (IPv6) when a relay message includes an interface ID
  360. relay option
  361. - client classes - used to reject a matching rule and try next rules
  362. Check if DHCPv4-over-DHCPv6 is enabled for this particular subnet and
  363. continue with the next subnet is if it is not.
  364. First the remote address is matched in a subnet IPv6 range, second
  365. the interface ID if it is set is matched, third the interface name.
  366. These rules are applied for each subnet configuration so if two
  367. subnets match the first one is returned (vs. the first matching rule).
  368. @todo better DHCPv4-over-DHCPv6 selection, e.g., handle relayed
  369. messages and return best (vs first) match.
  370. @subsection dhcp6SubnetSelection DHCPv6 Subnet Selection
  371. Selectors (i.e., members of @c SubnetSelector class) are:
  372. - incoming interface name
  373. - remote address
  374. - first relay link address - undefined or the first relay link address which
  375. is not undefined or link local (i.e., usable)
  376. - interface ID - when a relay message includes an interface ID relay option
  377. - client classes - used to reject a matching rule and try next rules
  378. If the first relay link address is undefined the client is directly connected:
  379. the interface name is matched and if it does not select a subnet the remote
  380. address is matched in a subnet address range.
  381. If the first relay link address is not undefined the query was relayed:
  382. the interface ID is tried and if it does not select a subnet the first
  383. relay address is matched as a subnet relay address.
  384. @section dhcp4o6Ipc DHCPv4-over-DHCPv6 Inter Process Communication
  385. DHCPv4-over-DHCPv6 support is implemented using cooperating
  386. DHCPv6 and DHCPv6 servers. Servers communicate over a pair of
  387. local UDP sockets using consecutive ports. The common part of
  388. the Inter Process Communication (IPC) is provided by the base class
  389. @c isc::dhcp::Dhcp4o6IpcBase.
  390. The receiving interface name and remote IPv6 address meta information
  391. are conveyed within a Vendor Specific Information option with the ISC
  392. enterprise ID carrying interface and remote address suboptions.
  393. \ref isc::dhcp::Dhcp4o6IpcBase::send adds them,
  394. \ref isc::dhcp::Dhcp4o6IpcBase::receive decodes and removes them.
  395. Also see \ref dhcpv4o6Dhcp4 and \ref dhcpv4o6Dhcp6 for details on how IPC
  396. is used by DHCPv4 and DHCPv6 components.
  397. @todo
  398. DHCPv4-over-DHCPv6 which are relayed by a DHCPv6 relay are not yet supported.
  399. */