cfgmgr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef CFGMGR_H
  15. #define CFGMGR_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_definition.h>
  19. #include <dhcp/option_space.h>
  20. #include <dhcp/classify.h>
  21. #include <dhcpsrv/d2_client_mgr.h>
  22. #include <dhcpsrv/option_space_container.h>
  23. #include <dhcpsrv/pool.h>
  24. #include <dhcpsrv/subnet.h>
  25. #include <util/buffer.h>
  26. #include <boost/shared_ptr.hpp>
  27. #include <boost/noncopyable.hpp>
  28. #include <map>
  29. #include <string>
  30. #include <vector>
  31. #include <list>
  32. namespace isc {
  33. namespace dhcp {
  34. /// @brief Exception thrown when the same interface has been specified twice.
  35. ///
  36. /// In particular, this exception is thrown when adding interface to the set
  37. /// of interfaces on which server is supposed to listen.
  38. class DuplicateListeningIface : public Exception {
  39. public:
  40. DuplicateListeningIface(const char* file, size_t line, const char* what) :
  41. isc::Exception(file, line, what) { };
  42. };
  43. /// @brief Configuration Manager
  44. ///
  45. /// This singleton class holds the whole configuration for DHCPv4 and DHCPv6
  46. /// servers. It currently holds information about zero or more subnets6.
  47. /// Each subnet may contain zero or more pools. Pool4 and Pool6 is the most
  48. /// basic "chunk" of configuration. It contains a range of assignable
  49. /// addresses.
  50. ///
  51. /// Below is a sketch of configuration inheritance (not implemented yet).
  52. /// Let's investigate the following configuration:
  53. ///
  54. /// @code
  55. /// preferred-lifetime 500;
  56. /// valid-lifetime 1000;
  57. /// subnet6 2001:db8:1::/48 {
  58. /// pool6 2001::db8:1::1 - 2001::db8:1::ff;
  59. /// };
  60. /// subnet6 2001:db8:2::/48 {
  61. /// valid-lifetime 2000;
  62. /// pool6 2001::db8:2::1 - 2001::db8:2::ff;
  63. /// };
  64. /// @endcode
  65. ///
  66. /// Parameters defined in a global scope are applicable to everything until
  67. /// they are overwritten in a smaller scope, in this case subnet6.
  68. /// In the example above, the first subnet6 has preferred lifetime of 500s
  69. /// and a valid lifetime of 1000s. The second subnet has preferred lifetime
  70. /// of 500s, but valid lifetime of 2000s.
  71. ///
  72. /// Parameter inheritance is likely to be implemented in configuration handling
  73. /// routines, so there is no storage capability in a global scope for
  74. /// subnet-specific parameters.
  75. ///
  76. /// @todo: Implement Subnet4 support (ticket #2237)
  77. /// @todo: Implement option definition support
  78. /// @todo: Implement parameter inheritance
  79. class CfgMgr : public boost::noncopyable {
  80. public:
  81. /// @brief returns a single instance of Configuration Manager
  82. ///
  83. /// CfgMgr is a singleton and this method is the only way of
  84. /// accessing it.
  85. static CfgMgr& instance();
  86. /// @brief Add new option definition.
  87. ///
  88. /// @param def option definition to be added.
  89. /// @param option_space name of the option space to add definition to.
  90. ///
  91. /// @throw isc::dhcp::DuplicateOptionDefinition when the particular
  92. /// option definition already exists.
  93. /// @throw isc::dhcp::MalformedOptionDefinition when the pointer to
  94. /// an option definition is NULL.
  95. /// @throw isc::BadValue when the option space name is empty or
  96. /// when trying to override the standard option (in dhcp4 or dhcp6
  97. /// option space).
  98. void addOptionDef(const OptionDefinitionPtr& def,
  99. const std::string& option_space);
  100. /// @brief Return option definitions for particular option space.
  101. ///
  102. /// @param option_space option space.
  103. ///
  104. /// @return pointer to the collection of option definitions for
  105. /// the particular option space. The option collection is empty
  106. /// if no option exists for the option space specified.
  107. OptionDefContainerPtr
  108. getOptionDefs(const std::string& option_space) const;
  109. /// @brief Return option definition for a particular option space and code.
  110. ///
  111. /// @param option_space option space.
  112. /// @param option_code option code.
  113. ///
  114. /// @return an option definition or NULL pointer if option definition
  115. /// has not been found.
  116. OptionDefinitionPtr getOptionDef(const std::string& option_space,
  117. const uint16_t option_code) const;
  118. /// @brief Adds new DHCPv4 option space to the collection.
  119. ///
  120. /// @param space option space to be added.
  121. ///
  122. /// @throw isc::dhcp::InvalidOptionSpace invalid option space
  123. /// has been specified.
  124. void addOptionSpace4(const OptionSpacePtr& space);
  125. /// @brief Adds new DHCPv6 option space to the collection.
  126. ///
  127. /// @param space option space to be added.
  128. ///
  129. /// @throw isc::dhcp::InvalidOptionSpace invalid option space
  130. /// has been specified.
  131. void addOptionSpace6(const OptionSpacePtr& space);
  132. /// @brief Return option spaces for DHCPv4.
  133. ///
  134. /// @return A collection of option spaces.
  135. const OptionSpaceCollection& getOptionSpaces4() const {
  136. return (spaces4_);
  137. }
  138. /// @brief Return option spaces for DHCPv6.
  139. ///
  140. /// @return A collection of option spaces.
  141. const OptionSpaceCollection& getOptionSpaces6() const {
  142. return (spaces6_);
  143. }
  144. /// @brief get IPv6 subnet by address
  145. ///
  146. /// Finds a matching subnet, based on an address. This can be used
  147. /// in two cases: when trying to find an appropriate lease based on
  148. /// a) relay link address (that must be the address that is on link)
  149. /// b) our global address on the interface the message was received on
  150. /// (for directly connected clients)
  151. ///
  152. /// If there are any classes specified in a subnet, that subnet
  153. /// will be selected only if the client belongs to appropriate class.
  154. ///
  155. /// @note The client classification is checked before any relay
  156. /// information checks are conducted.
  157. ///
  158. /// If relay is true then relay info overrides (i.e. value the sysadmin
  159. /// can configure in Dhcp6/subnet6[X]/relay/ip-address) can be used.
  160. /// That is applicable only for relays. Those overrides must not be used
  161. /// for client address or for client hints. They are for link-addr field
  162. /// in the RELAY_FORW message only.
  163. ///
  164. /// @param hint an address that belongs to a searched subnet
  165. /// @param classes classes the client belongs to
  166. /// @param relay true if address specified in hint is a relay
  167. ///
  168. /// @return a subnet object (or NULL if no suitable match was fount)
  169. Subnet6Ptr getSubnet6(const isc::asiolink::IOAddress& hint,
  170. const isc::dhcp::ClientClasses& classes,
  171. const bool relay = false);
  172. /// @brief get IPv6 subnet by interface name
  173. ///
  174. /// Finds a matching local subnet, based on interface name. This
  175. /// is used for selecting subnets that were explicitly marked by the
  176. /// user as reachable over specified network interface.
  177. ///
  178. /// If there are any classes specified in a subnet, that subnet
  179. /// will be selected only if the client belongs to appropriate class.
  180. ///
  181. /// @param iface_name interface name
  182. /// @param classes classes the client belongs to
  183. ///
  184. /// @return a subnet object (or NULL if no suitable match was fount)
  185. Subnet6Ptr getSubnet6(const std::string& iface_name,
  186. const isc::dhcp::ClientClasses& classes);
  187. /// @brief get IPv6 subnet by interface-id
  188. ///
  189. /// Another possibility to find a subnet is based on interface-id.
  190. ///
  191. /// If there are any classes specified in a subnet, that subnet
  192. /// will be selected only if the client belongs to appropriate class.
  193. ///
  194. /// @param interface_id content of interface-id option returned by a relay
  195. /// @param classes classes the client belongs to
  196. ///
  197. /// @return a subnet object
  198. Subnet6Ptr getSubnet6(OptionPtr interface_id,
  199. const isc::dhcp::ClientClasses& classes);
  200. /// @brief adds an IPv6 subnet
  201. ///
  202. /// @param subnet new subnet to be added.
  203. void addSubnet6(const Subnet6Ptr& subnet);
  204. /// @brief Delete all option definitions.
  205. void deleteOptionDefs();
  206. /// @todo: Add subnet6 removal routines. Currently it is not possible
  207. /// to remove subnets. The only case where subnet6 removal would be
  208. /// needed is a dynamic server reconfiguration - a use case that is not
  209. /// planned to be supported any time soon.
  210. /// @brief removes all IPv6 subnets
  211. ///
  212. /// This method removes all existing IPv6 subnets. It is used during
  213. /// reconfiguration - old configuration is wiped and new definitions
  214. /// are used to recreate subnets.
  215. ///
  216. /// @todo Implement more intelligent approach. Note that comparison
  217. /// between old and new configuration is tricky. For example: is
  218. /// 2000::/64 and 2000::/48 the same subnet or is it something
  219. /// completely new?
  220. void deleteSubnets6();
  221. /// @brief returns const reference to all subnets6
  222. ///
  223. /// This is used in a hook (subnet4_select), where the hook is able
  224. /// to choose a different subnet. Server code has to offer a list
  225. /// of possible choices (i.e. all subnets).
  226. /// @return a pointer to const Subnet6 collection
  227. const Subnet4Collection* getSubnets4() const {
  228. return (&subnets4_);
  229. }
  230. /// @brief returns const reference to all subnets6
  231. ///
  232. /// This is used in a hook (subnet6_select), where the hook is able
  233. /// to choose a different subnet. Server code has to offer a list
  234. /// of possible choices (i.e. all subnets).
  235. /// @return a pointer to const Subnet6 collection
  236. const Subnet6Collection* getSubnets6() {
  237. return (&subnets6_);
  238. }
  239. /// @brief get IPv4 subnet by address
  240. ///
  241. /// Finds a matching subnet, based on an address. This can be used
  242. /// in two cases: when trying to find an appropriate lease based on
  243. /// a) relay link address (that must be the address that is on link)
  244. /// b) our global address on the interface the message was received on
  245. /// (for directly connected clients)
  246. ///
  247. /// If there are any classes specified in a subnet, that subnet
  248. /// will be selected only if the client belongs to appropriate class.
  249. ///
  250. /// If relay is true then relay info overrides (i.e. value the sysadmin
  251. /// can configure in Dhcp4/subnet4[X]/relay/ip-address) can be used.
  252. /// That is true only for relays. Those overrides must not be used
  253. /// for client address or for client hints. They are for giaddr only.
  254. ///
  255. /// @param hint an address that belongs to a searched subnet
  256. /// @param classes classes the client belongs to
  257. /// @param relay true if address specified in hint is a relay
  258. ///
  259. /// @return a subnet object
  260. Subnet4Ptr getSubnet4(const isc::asiolink::IOAddress& hint,
  261. const isc::dhcp::ClientClasses& classes,
  262. bool relay = false) const;
  263. /// @brief Returns a subnet for the specified local interface.
  264. ///
  265. /// This function checks that the IP address assigned to the specified
  266. /// interface belongs to any IPv4 subnet configured, and returns this
  267. /// subnet.
  268. ///
  269. /// @todo Implement classes support here.
  270. ///
  271. /// @param iface Short name of the interface which is being checked.
  272. /// @param classes classes the client belongs to
  273. ///
  274. /// @return Pointer to the subnet matching interface specified or NULL
  275. /// pointer if IPv4 address on the interface doesn't match any subnet.
  276. Subnet4Ptr getSubnet4(const std::string& iface,
  277. const isc::dhcp::ClientClasses& classes) const;
  278. /// @brief adds a subnet4
  279. void addSubnet4(const Subnet4Ptr& subnet);
  280. /// @brief removes all IPv4 subnets
  281. ///
  282. /// This method removes all existing IPv4 subnets. It is used during
  283. /// reconfiguration - old configuration is wiped and new definitions
  284. /// are used to recreate subnets.
  285. ///
  286. /// @todo Implement more intelligent approach. Note that comparison
  287. /// between old and new configuration is tricky. For example: is
  288. /// 192.0.2.0/23 and 192.0.2.0/24 the same subnet or is it something
  289. /// completely new?
  290. void deleteSubnets4();
  291. /// @brief returns path do the data directory
  292. ///
  293. /// This method returns a path to writeable directory that DHCP servers
  294. /// can store data in.
  295. /// @return data directory
  296. std::string getDataDir();
  297. /// @brief Adds the name of the interface to the set of interfaces on which
  298. /// server should listen.
  299. ///
  300. /// @param iface A name of the interface being added to the listening set.
  301. void addActiveIface(const std::string& iface);
  302. /// @brief Sets the flag which indicates that server is supposed to listen
  303. /// on all available interfaces.
  304. ///
  305. /// This function does not close or open sockets. It simply marks that
  306. /// server should start to listen on all interfaces the next time sockets
  307. /// are reopened. Server should examine this flag when it gets reconfigured
  308. /// and configuration changes the interfaces that server should listen on.
  309. void activateAllIfaces();
  310. /// @brief Clear the collection of the interfaces that server should listen
  311. /// on.
  312. ///
  313. /// Apart from clearing the list of interfaces specified with
  314. /// @c CfgMgr::addListeningInterface, it also disables listening on all
  315. /// interfaces if it has been enabled using
  316. /// @c CfgMgr::activateAllInterfaces.
  317. /// Likewise @c CfgMgr::activateAllIfaces, this function does not close or
  318. /// open sockets. It marks all interfaces inactive for DHCP traffic.
  319. /// Server should examine this new setting when it attempts to
  320. /// reopen sockets (as a result of reconfiguration).
  321. void deleteActiveIfaces();
  322. /// @brief Check if specified interface should be used to listen to DHCP
  323. /// traffic.
  324. ///
  325. /// @param iface A name of the interface to be checked.
  326. ///
  327. /// @return true if the specified interface belongs to the set of the
  328. /// interfaces on which server is configured to listen.
  329. bool isActiveIface(const std::string& iface) const;
  330. /// @brief returns unicast a given interface should listen on (or NULL)
  331. ///
  332. /// This method will return an address for a specified interface, if the
  333. /// server is supposed to listen on unicast address. This address is
  334. /// intended to be used immediately. This pointer is valid only until
  335. /// the next configuration change.
  336. ///
  337. /// @return IOAddress pointer (or NULL if none)
  338. const isc::asiolink::IOAddress*
  339. getUnicast(const std::string& iface) const;
  340. /// @brief Sets whether server should send back client-id in DHCPv4
  341. ///
  342. /// This is a compatibility flag. The default (true) is compliant with
  343. /// RFC6842. False is for backward compatibility.
  344. ///
  345. /// @param echo should the client-id be sent or not
  346. void echoClientId(const bool echo) {
  347. echo_v4_client_id_ = echo;
  348. }
  349. /// @brief Returns whether server should send back client-id in DHCPv4.
  350. /// @return true if client-id should be returned, false otherwise.
  351. bool echoClientId() const {
  352. return (echo_v4_client_id_);
  353. }
  354. /// @brief Updates the DHCP-DDNS client configuration to the given value.
  355. ///
  356. /// @param new_config pointer to the new client configuration.
  357. ///
  358. /// @throw Underlying method(s) will throw D2ClientError if given an empty
  359. /// pointer.
  360. void setD2ClientConfig(D2ClientConfigPtr& new_config);
  361. /// @brief Convenience method for checking if DHCP-DDNS updates are enabled.
  362. ///
  363. /// @return True if the D2 configuration is enabled.
  364. bool ddnsEnabled();
  365. /// @brief Fetches the DHCP-DDNS configuration pointer.
  366. ///
  367. /// @return a reference to the current configuration pointer.
  368. const D2ClientConfigPtr& getD2ClientConfig() const;
  369. /// @brief Fetches the DHCP-DDNS manager.
  370. ///
  371. /// @return a reference to the DHCP-DDNS manager.
  372. D2ClientMgr& getD2ClientMgr();
  373. protected:
  374. /// @brief Protected constructor.
  375. ///
  376. /// This constructor is protected for 2 reasons. First, it forbids any
  377. /// instantiations of this class (CfgMgr is a singleton). Second, it
  378. /// allows derived class to instantiate it. That is useful for testing
  379. /// purposes.
  380. CfgMgr();
  381. /// @brief virtual destructor
  382. virtual ~CfgMgr();
  383. /// @brief a container for IPv6 subnets.
  384. ///
  385. /// That is a simple vector of pointers. It does not make much sense to
  386. /// optimize access time (e.g. using a map), because typical search
  387. /// pattern will use calling inRange() method on each subnet until
  388. /// a match is found.
  389. Subnet6Collection subnets6_;
  390. /// @brief a container for IPv4 subnets.
  391. ///
  392. /// That is a simple vector of pointers. It does not make much sense to
  393. /// optimize access time (e.g. using a map), because typical search
  394. /// pattern will use calling inRange() method on each subnet until
  395. /// a match is found.
  396. Subnet4Collection subnets4_;
  397. private:
  398. /// @brief Checks if the specified interface is listed as active.
  399. ///
  400. /// This function searches for the specified interface name on the list of
  401. /// active interfaces: @c CfgMgr::active_ifaces_. It does not take into
  402. /// account @c CfgMgr::all_ifaces_active_ flag. If this flag is set to true
  403. /// but the specified interface does not belong to
  404. /// @c CfgMgr::active_ifaces_, it will return false.
  405. ///
  406. /// @param iface interface name.
  407. ///
  408. /// @return true if specified interface belongs to
  409. /// @c CfgMgr::active_ifaces_.
  410. bool isIfaceListedActive(const std::string& iface) const;
  411. /// @brief A collection of option definitions.
  412. ///
  413. /// A collection of option definitions that can be accessed
  414. /// using option space name they belong to.
  415. OptionSpaceContainer<OptionDefContainer,
  416. OptionDefinitionPtr, std::string> option_def_spaces_;
  417. /// @brief Container for defined DHCPv6 option spaces.
  418. OptionSpaceCollection spaces6_;
  419. /// @brief Container for defined DHCPv4 option spaces.
  420. OptionSpaceCollection spaces4_;
  421. /// @brief directory where data files (e.g. server-id) are stored
  422. std::string datadir_;
  423. /// @name A collection of interface names on which server listens.
  424. //@{
  425. typedef std::list<std::string> ActiveIfacesCollection;
  426. std::list<std::string> active_ifaces_;
  427. //@}
  428. /// @name a collection of unicast addresses and the interfaces names the
  429. // server is supposed to listen on
  430. //@{
  431. typedef std::map<std::string, isc::asiolink::IOAddress> UnicastIfacesCollection;
  432. UnicastIfacesCollection unicast_addrs_;
  433. /// A flag which indicates that server should listen on all available
  434. /// interfaces.
  435. bool all_ifaces_active_;
  436. /// Indicates whether v4 server should send back client-id
  437. bool echo_v4_client_id_;
  438. /// @brief Manages the DHCP-DDNS client and its configuration.
  439. D2ClientMgr d2_client_mgr_;
  440. };
  441. } // namespace isc::dhcp
  442. } // namespace isc
  443. #endif // CFGMGR_H