cfgmgr.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. #include <asiolink/io_address.h>
  15. #include <dhcp/libdhcp++.h>
  16. #include <dhcpsrv/cfgmgr.h>
  17. #include <dhcpsrv/dhcpsrv_log.h>
  18. #include <string>
  19. using namespace isc::asiolink;
  20. using namespace isc::util;
  21. namespace isc {
  22. namespace dhcp {
  23. CfgMgr&
  24. CfgMgr::instance() {
  25. static CfgMgr cfg_mgr;
  26. return (cfg_mgr);
  27. }
  28. void
  29. CfgMgr::addOptionSpace4(const OptionSpacePtr& space) {
  30. if (!space) {
  31. isc_throw(InvalidOptionSpace,
  32. "provided option space object is NULL.");
  33. }
  34. OptionSpaceCollection::iterator it = spaces4_.find(space->getName());
  35. if (it != spaces4_.end()) {
  36. isc_throw(InvalidOptionSpace, "option space " << space->getName()
  37. << " already added.");
  38. }
  39. spaces4_.insert(make_pair(space->getName(), space));
  40. }
  41. void
  42. CfgMgr::addOptionSpace6(const OptionSpacePtr& space) {
  43. if (!space) {
  44. isc_throw(InvalidOptionSpace,
  45. "provided option space object is NULL.");
  46. }
  47. OptionSpaceCollection::iterator it = spaces6_.find(space->getName());
  48. if (it != spaces6_.end()) {
  49. isc_throw(InvalidOptionSpace, "option space " << space->getName()
  50. << " already added.");
  51. }
  52. spaces6_.insert(make_pair(space->getName(), space));
  53. }
  54. void
  55. CfgMgr::addOptionDef(const OptionDefinitionPtr& def,
  56. const std::string& option_space) {
  57. // @todo we need better validation of the provided option space name here.
  58. // This will be implemented when #2313 is merged.
  59. if (option_space.empty()) {
  60. isc_throw(BadValue, "option space name must not be empty");
  61. } else if (!def) {
  62. // Option definition must point to a valid object.
  63. isc_throw(MalformedOptionDefinition, "option definition must not be NULL");
  64. } else if (getOptionDef(option_space, def->getCode())) {
  65. // Option definition must not be overriden.
  66. isc_throw(DuplicateOptionDefinition, "option definition already added"
  67. << " to option space " << option_space);
  68. // We must not override standard (assigned) option for which there is a
  69. // definition in libdhcp++. The standard options belong to dhcp4 or dhcp6
  70. // option space.
  71. } else if ((option_space == "dhcp4" &&
  72. LibDHCP::isStandardOption(Option::V4, def->getCode()) &&
  73. LibDHCP::getOptionDef(Option::V4, def->getCode())) ||
  74. (option_space == "dhcp6" &&
  75. LibDHCP::isStandardOption(Option::V6, def->getCode()) &&
  76. LibDHCP::getOptionDef(Option::V6, def->getCode()))) {
  77. isc_throw(BadValue, "unable to override definition of option '"
  78. << def->getCode() << "' in standard option space '"
  79. << option_space << "'.");
  80. }
  81. // Actually add a new item.
  82. option_def_spaces_.addItem(def, option_space);
  83. }
  84. OptionDefContainerPtr
  85. CfgMgr::getOptionDefs(const std::string& option_space) const {
  86. // @todo Validate the option space once the #2313 is implemented.
  87. return (option_def_spaces_.getItems(option_space));
  88. }
  89. OptionDefinitionPtr
  90. CfgMgr::getOptionDef(const std::string& option_space,
  91. const uint16_t option_code) const {
  92. // @todo Validate the option space once the #2313 is implemented.
  93. // Get a reference to option definitions for a particular option space.
  94. OptionDefContainerPtr defs = getOptionDefs(option_space);
  95. // If there are no matching option definitions then return the empty pointer.
  96. if (!defs || defs->empty()) {
  97. return (OptionDefinitionPtr());
  98. }
  99. // If there are some option definitions for a particular option space
  100. // use an option code to get the one we want.
  101. const OptionDefContainerTypeIndex& idx = defs->get<1>();
  102. const OptionDefContainerTypeRange& range = idx.equal_range(option_code);
  103. // If there is no definition that matches option code, return empty pointer.
  104. if (std::distance(range.first, range.second) == 0) {
  105. return (OptionDefinitionPtr());
  106. }
  107. // If there is more than one definition matching an option code, return
  108. // the first one. This should not happen because we check for duplicates
  109. // when addOptionDef is called.
  110. return (*range.first);
  111. }
  112. Subnet6Ptr
  113. CfgMgr::getSubnet6(const std::string& iface) {
  114. if (!iface.length()) {
  115. return (Subnet6Ptr());
  116. }
  117. // If there is more than one, we need to choose the proper one
  118. for (Subnet6Collection::iterator subnet = subnets6_.begin();
  119. subnet != subnets6_.end(); ++subnet) {
  120. if (iface == (*subnet)->getIface()) {
  121. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  122. DHCPSRV_CFGMGR_SUBNET6_IFACE)
  123. .arg((*subnet)->toText()).arg(iface);
  124. return (*subnet);
  125. }
  126. }
  127. return (Subnet6Ptr());
  128. }
  129. Subnet6Ptr
  130. CfgMgr::getSubnet6(const isc::asiolink::IOAddress& hint) {
  131. // If there's only one subnet configured, let's just use it
  132. // The idea is to keep small deployments easy. In a small network - one
  133. // router that also runs DHCPv6 server. User specifies a single pool and
  134. // expects it to just work. Without this, the server would complain that it
  135. // doesn't have IP address on its interfaces that matches that
  136. // configuration. Such requirement makes sense in IPv4, but not in IPv6.
  137. // The server does not need to have a global address (using just link-local
  138. // is ok for DHCPv6 server) from the pool it serves.
  139. if ((subnets6_.size() == 1) && hint.isV6LinkLocal()) {
  140. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  141. DHCPSRV_CFGMGR_ONLY_SUBNET6)
  142. .arg(subnets6_[0]->toText()).arg(hint.toText());
  143. return (subnets6_[0]);
  144. }
  145. // If there is more than one, we need to choose the proper one
  146. for (Subnet6Collection::iterator subnet = subnets6_.begin();
  147. subnet != subnets6_.end(); ++subnet) {
  148. if ((*subnet)->inRange(hint)) {
  149. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  150. DHCPSRV_CFGMGR_SUBNET6)
  151. .arg((*subnet)->toText()).arg(hint.toText());
  152. return (*subnet);
  153. }
  154. }
  155. // sorry, we don't support that subnet
  156. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_NO_SUBNET6)
  157. .arg(hint.toText());
  158. return (Subnet6Ptr());
  159. }
  160. Subnet6Ptr CfgMgr::getSubnet6(OptionPtr iface_id_option) {
  161. if (!iface_id_option) {
  162. return (Subnet6Ptr());
  163. }
  164. // Let's iterate over all subnets and for those that have interface-id
  165. // defined, check if the interface-id is equal to what we are looking for
  166. for (Subnet6Collection::iterator subnet = subnets6_.begin();
  167. subnet != subnets6_.end(); ++subnet) {
  168. if ( (*subnet)->getInterfaceId() &&
  169. ((*subnet)->getInterfaceId()->equal(iface_id_option))) {
  170. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  171. DHCPSRV_CFGMGR_SUBNET6_IFACE_ID)
  172. .arg((*subnet)->toText());
  173. return (*subnet);
  174. }
  175. }
  176. return (Subnet6Ptr());
  177. }
  178. void CfgMgr::addSubnet6(const Subnet6Ptr& subnet) {
  179. /// @todo: Check that this new subnet does not cross boundaries of any
  180. /// other already defined subnet.
  181. /// @todo: Check that there is no subnet with the same interface-id
  182. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_SUBNET6)
  183. .arg(subnet->toText());
  184. subnets6_.push_back(subnet);
  185. }
  186. Subnet4Ptr
  187. CfgMgr::getSubnet4(const isc::asiolink::IOAddress& hint) {
  188. // If there's only one subnet configured, let's just use it
  189. // The idea is to keep small deployments easy. In a small network - one
  190. // router that also runs DHCPv6 server. Users specifies a single pool and
  191. // expects it to just work. Without this, the server would complain that it
  192. // doesn't have IP address on its interfaces that matches that
  193. // configuration. Such requirement makes sense in IPv4, but not in IPv6.
  194. // The server does not need to have a global address (using just link-local
  195. // is ok for DHCPv6 server) from the pool it serves.
  196. if (subnets4_.size() == 1) {
  197. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  198. DHCPSRV_CFGMGR_ONLY_SUBNET4)
  199. .arg(subnets4_[0]->toText()).arg(hint.toText());
  200. return (subnets4_[0]);
  201. }
  202. // If there is more than one, we need to choose the proper one
  203. for (Subnet4Collection::iterator subnet = subnets4_.begin();
  204. subnet != subnets4_.end(); ++subnet) {
  205. if ((*subnet)->inRange(hint)) {
  206. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  207. DHCPSRV_CFGMGR_SUBNET4)
  208. .arg((*subnet)->toText()).arg(hint.toText());
  209. return (*subnet);
  210. }
  211. }
  212. // sorry, we don't support that subnet
  213. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_NO_SUBNET4)
  214. .arg(hint.toText());
  215. return (Subnet4Ptr());
  216. }
  217. void CfgMgr::addSubnet4(const Subnet4Ptr& subnet) {
  218. /// @todo: Check that this new subnet does not cross boundaries of any
  219. /// other already defined subnet.
  220. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_SUBNET4)
  221. .arg(subnet->toText());
  222. subnets4_.push_back(subnet);
  223. }
  224. void CfgMgr::deleteOptionDefs() {
  225. option_def_spaces_.clearItems();
  226. }
  227. void CfgMgr::deleteSubnets4() {
  228. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DELETE_SUBNET4);
  229. subnets4_.clear();
  230. }
  231. void CfgMgr::deleteSubnets6() {
  232. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DELETE_SUBNET6);
  233. subnets6_.clear();
  234. }
  235. std::string CfgMgr::getDataDir() {
  236. return (datadir_);
  237. }
  238. void
  239. CfgMgr::addActiveIface(const std::string& iface) {
  240. size_t pos = iface.find("/");
  241. std::string iface_copy = iface;
  242. if (pos != std::string::npos) {
  243. std::string addr_string = iface.substr(pos + 1);
  244. try {
  245. IOAddress addr(addr_string);
  246. iface_copy = iface.substr(0,pos);
  247. unicast_addrs_.insert(make_pair(iface_copy, addr));
  248. } catch (...) {
  249. isc_throw(BadValue, "Can't convert '" << addr_string
  250. << "' into address in interface defition ('"
  251. << iface << "')");
  252. }
  253. }
  254. if (isIfaceListedActive(iface_copy)) {
  255. isc_throw(DuplicateListeningIface,
  256. "attempt to add duplicate interface '" << iface_copy << "'"
  257. " to the set of interfaces on which server listens");
  258. }
  259. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_IFACE)
  260. .arg(iface_copy);
  261. active_ifaces_.push_back(iface_copy);
  262. }
  263. void
  264. CfgMgr::activateAllIfaces() {
  265. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  266. DHCPSRV_CFGMGR_ALL_IFACES_ACTIVE);
  267. all_ifaces_active_ = true;
  268. }
  269. void
  270. CfgMgr::deleteActiveIfaces() {
  271. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  272. DHCPSRV_CFGMGR_CLEAR_ACTIVE_IFACES);
  273. active_ifaces_.clear();
  274. all_ifaces_active_ = false;
  275. unicast_addrs_.clear();
  276. }
  277. bool
  278. CfgMgr::isActiveIface(const std::string& iface) const {
  279. // @todo Verify that the interface with the specified name is
  280. // present in the system.
  281. // If all interfaces are marked active, there is no need to check that
  282. // the name of this interface has been explicitly listed.
  283. if (all_ifaces_active_) {
  284. return (true);
  285. }
  286. return (isIfaceListedActive(iface));
  287. }
  288. bool
  289. CfgMgr::isIfaceListedActive(const std::string& iface) const {
  290. for (ActiveIfacesCollection::const_iterator it = active_ifaces_.begin();
  291. it != active_ifaces_.end(); ++it) {
  292. if (iface == *it) {
  293. return (true);
  294. }
  295. }
  296. return (false);
  297. }
  298. const isc::asiolink::IOAddress*
  299. CfgMgr::getUnicast(const std::string& iface) const {
  300. UnicastIfacesCollection::const_iterator addr = unicast_addrs_.find(iface);
  301. if (addr == unicast_addrs_.end()) {
  302. return (NULL);
  303. }
  304. return (&(*addr).second);
  305. }
  306. void
  307. CfgMgr::setD2ClientConfig(D2ClientConfigPtr& new_config) {
  308. d2_client_mgr_.setD2ClientConfig(new_config);
  309. }
  310. bool
  311. CfgMgr::ddnsEnabled() {
  312. return (d2_client_mgr_.ddnsEnabled());
  313. }
  314. const D2ClientConfigPtr&
  315. CfgMgr::getD2ClientConfig() const {
  316. return (d2_client_mgr_.getD2ClientConfig());
  317. }
  318. D2ClientMgr&
  319. CfgMgr::getD2ClientMgr() {
  320. return (d2_client_mgr_);
  321. }
  322. CfgMgr::CfgMgr()
  323. : datadir_(DHCP_DATA_DIR),
  324. all_ifaces_active_(false), echo_v4_client_id_(true),
  325. d2_client_mgr_() {
  326. // DHCP_DATA_DIR must be set set with -DDHCP_DATA_DIR="..." in Makefile.am
  327. // Note: the definition of DHCP_DATA_DIR needs to include quotation marks
  328. // See AM_CPPFLAGS definition in Makefile.am
  329. }
  330. CfgMgr::~CfgMgr() {
  331. }
  332. }; // end of isc::dhcp namespace
  333. }; // end of isc namespace