cfgmgr.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/iface_mgr.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcpsrv/cfgmgr.h>
  18. #include <dhcpsrv/dhcpsrv_log.h>
  19. #include <string>
  20. using namespace isc::asiolink;
  21. using namespace isc::util;
  22. namespace isc {
  23. namespace dhcp {
  24. CfgMgr&
  25. CfgMgr::instance() {
  26. static CfgMgr cfg_mgr;
  27. return (cfg_mgr);
  28. }
  29. void
  30. CfgMgr::addOptionSpace4(const OptionSpacePtr& space) {
  31. if (!space) {
  32. isc_throw(InvalidOptionSpace,
  33. "provided option space object is NULL.");
  34. }
  35. OptionSpaceCollection::iterator it = spaces4_.find(space->getName());
  36. if (it != spaces4_.end()) {
  37. isc_throw(InvalidOptionSpace, "option space " << space->getName()
  38. << " already added.");
  39. }
  40. spaces4_.insert(make_pair(space->getName(), space));
  41. }
  42. void
  43. CfgMgr::addOptionSpace6(const OptionSpacePtr& space) {
  44. if (!space) {
  45. isc_throw(InvalidOptionSpace,
  46. "provided option space object is NULL.");
  47. }
  48. OptionSpaceCollection::iterator it = spaces6_.find(space->getName());
  49. if (it != spaces6_.end()) {
  50. isc_throw(InvalidOptionSpace, "option space " << space->getName()
  51. << " already added.");
  52. }
  53. spaces6_.insert(make_pair(space->getName(), space));
  54. }
  55. void
  56. CfgMgr::addOptionDef(const OptionDefinitionPtr& def,
  57. const std::string& option_space) {
  58. // @todo we need better validation of the provided option space name here.
  59. // This will be implemented when #2313 is merged.
  60. if (option_space.empty()) {
  61. isc_throw(BadValue, "option space name must not be empty");
  62. } else if (!def) {
  63. // Option definition must point to a valid object.
  64. isc_throw(MalformedOptionDefinition, "option definition must not be NULL");
  65. } else if (getOptionDef(option_space, def->getCode())) {
  66. // Option definition must not be overriden.
  67. isc_throw(DuplicateOptionDefinition, "option definition already added"
  68. << " to option space " << option_space);
  69. // We must not override standard (assigned) option for which there is a
  70. // definition in libdhcp++. The standard options belong to dhcp4 or dhcp6
  71. // option space.
  72. } else if ((option_space == "dhcp4" &&
  73. LibDHCP::isStandardOption(Option::V4, def->getCode()) &&
  74. LibDHCP::getOptionDef(Option::V4, def->getCode())) ||
  75. (option_space == "dhcp6" &&
  76. LibDHCP::isStandardOption(Option::V6, def->getCode()) &&
  77. LibDHCP::getOptionDef(Option::V6, def->getCode()))) {
  78. isc_throw(BadValue, "unable to override definition of option '"
  79. << def->getCode() << "' in standard option space '"
  80. << option_space << "'.");
  81. }
  82. // Actually add a new item.
  83. option_def_spaces_.addItem(def, option_space);
  84. }
  85. OptionDefContainerPtr
  86. CfgMgr::getOptionDefs(const std::string& option_space) const {
  87. // @todo Validate the option space once the #2313 is implemented.
  88. return (option_def_spaces_.getItems(option_space));
  89. }
  90. OptionDefinitionPtr
  91. CfgMgr::getOptionDef(const std::string& option_space,
  92. const uint16_t option_code) const {
  93. // @todo Validate the option space once the #2313 is implemented.
  94. // Get a reference to option definitions for a particular option space.
  95. OptionDefContainerPtr defs = getOptionDefs(option_space);
  96. // If there are no matching option definitions then return the empty pointer.
  97. if (!defs || defs->empty()) {
  98. return (OptionDefinitionPtr());
  99. }
  100. // If there are some option definitions for a particular option space
  101. // use an option code to get the one we want.
  102. const OptionDefContainerTypeIndex& idx = defs->get<1>();
  103. const OptionDefContainerTypeRange& range = idx.equal_range(option_code);
  104. // If there is no definition that matches option code, return empty pointer.
  105. if (std::distance(range.first, range.second) == 0) {
  106. return (OptionDefinitionPtr());
  107. }
  108. // If there is more than one definition matching an option code, return
  109. // the first one. This should not happen because we check for duplicates
  110. // when addOptionDef is called.
  111. return (*range.first);
  112. }
  113. Subnet6Ptr
  114. CfgMgr::getSubnet6(const std::string& iface,
  115. const isc::dhcp::ClientClasses& classes) {
  116. if (!iface.length()) {
  117. return (Subnet6Ptr());
  118. }
  119. // If there is more than one, we need to choose the proper one
  120. for (Subnet6Collection::iterator subnet = subnets6_.begin();
  121. subnet != subnets6_.end(); ++subnet) {
  122. // If client is rejected because of not meeting client class criteria...
  123. if (!(*subnet)->clientSupported(classes)) {
  124. continue;
  125. }
  126. if (iface == (*subnet)->getIface()) {
  127. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  128. DHCPSRV_CFGMGR_SUBNET6_IFACE)
  129. .arg((*subnet)->toText()).arg(iface);
  130. return (*subnet);
  131. }
  132. }
  133. return (Subnet6Ptr());
  134. }
  135. Subnet6Ptr
  136. CfgMgr::getSubnet6(const isc::asiolink::IOAddress& hint,
  137. const isc::dhcp::ClientClasses& classes,
  138. const bool relay) {
  139. // If there is more than one, we need to choose the proper one
  140. for (Subnet6Collection::iterator subnet = subnets6_.begin();
  141. subnet != subnets6_.end(); ++subnet) {
  142. // If client is rejected because of not meeting client class criteria...
  143. if (!(*subnet)->clientSupported(classes)) {
  144. continue;
  145. }
  146. // If the hint is a relay address, and there is relay info specified
  147. // for this subnet and those two match, then use this subnet.
  148. if (relay && ((*subnet)->getRelayInfo().addr_ == hint) ) {
  149. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  150. DHCPSRV_CFGMGR_SUBNET6_RELAY)
  151. .arg((*subnet)->toText()).arg(hint.toText());
  152. return (*subnet);
  153. }
  154. if ((*subnet)->inRange(hint)) {
  155. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_SUBNET6)
  156. .arg((*subnet)->toText()).arg(hint.toText());
  157. return (*subnet);
  158. }
  159. }
  160. // sorry, we don't support that subnet
  161. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_NO_SUBNET6)
  162. .arg(hint.toText());
  163. return (Subnet6Ptr());
  164. }
  165. Subnet6Ptr CfgMgr::getSubnet6(OptionPtr iface_id_option,
  166. const isc::dhcp::ClientClasses& classes) {
  167. if (!iface_id_option) {
  168. return (Subnet6Ptr());
  169. }
  170. // Let's iterate over all subnets and for those that have interface-id
  171. // defined, check if the interface-id is equal to what we are looking for
  172. for (Subnet6Collection::iterator subnet = subnets6_.begin();
  173. subnet != subnets6_.end(); ++subnet) {
  174. // If client is rejected because of not meeting client class criteria...
  175. if (!(*subnet)->clientSupported(classes)) {
  176. continue;
  177. }
  178. if ( (*subnet)->getInterfaceId() &&
  179. ((*subnet)->getInterfaceId()->equal(iface_id_option))) {
  180. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  181. DHCPSRV_CFGMGR_SUBNET6_IFACE_ID)
  182. .arg((*subnet)->toText());
  183. return (*subnet);
  184. }
  185. }
  186. return (Subnet6Ptr());
  187. }
  188. void CfgMgr::addSubnet6(const Subnet6Ptr& subnet) {
  189. /// @todo: Check that this new subnet does not cross boundaries of any
  190. /// other already defined subnet.
  191. /// @todo: Check that there is no subnet with the same interface-id
  192. if (isDuplicate(*subnet)) {
  193. isc_throw(isc::dhcp::DuplicateSubnetID, "ID of the new IPv6 subnet '"
  194. << subnet->getID() << "' is already in use");
  195. }
  196. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_SUBNET6)
  197. .arg(subnet->toText());
  198. subnets6_.push_back(subnet);
  199. }
  200. Subnet4Ptr
  201. CfgMgr::getSubnet4(const isc::asiolink::IOAddress& hint,
  202. const isc::dhcp::ClientClasses& classes,
  203. bool relay) const {
  204. // Iterate over existing subnets to find a suitable one for the
  205. // given address.
  206. for (Subnet4Collection::const_iterator subnet = subnets4_.begin();
  207. subnet != subnets4_.end(); ++subnet) {
  208. // If client is rejected because of not meeting client class criteria...
  209. if (!(*subnet)->clientSupported(classes)) {
  210. continue;
  211. }
  212. // If the hint is a relay address, and there is relay info specified
  213. // for this subnet and those two match, then use this subnet.
  214. if (relay && ((*subnet)->getRelayInfo().addr_ == hint) ) {
  215. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  216. DHCPSRV_CFGMGR_SUBNET4_RELAY)
  217. .arg((*subnet)->toText()).arg(hint.toText());
  218. return (*subnet);
  219. }
  220. // Let's check if the client belongs to the given subnet
  221. if ((*subnet)->inRange(hint)) {
  222. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  223. DHCPSRV_CFGMGR_SUBNET4)
  224. .arg((*subnet)->toText()).arg(hint.toText());
  225. return (*subnet);
  226. }
  227. }
  228. // sorry, we don't support that subnet
  229. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_NO_SUBNET4)
  230. .arg(hint.toText());
  231. return (Subnet4Ptr());
  232. }
  233. Subnet4Ptr
  234. CfgMgr::getSubnet4(const std::string& iface_name,
  235. const isc::dhcp::ClientClasses& classes) const {
  236. Iface* iface = IfaceMgr::instance().getIface(iface_name);
  237. // This should never happen in the real life. Hence we throw an exception.
  238. if (iface == NULL) {
  239. isc_throw(isc::BadValue, "interface " << iface_name <<
  240. " doesn't exist and therefore it is impossible"
  241. " to find a suitable subnet for its IPv4 address");
  242. }
  243. IOAddress addr("0.0.0.0");
  244. // If IPv4 address assigned to the interface exists, find a suitable
  245. // subnet for it, else return NULL pointer to indicate that no subnet
  246. // could be found.
  247. return (iface->getAddress4(addr) ? getSubnet4(addr, classes) : Subnet4Ptr());
  248. }
  249. void CfgMgr::addSubnet4(const Subnet4Ptr& subnet) {
  250. /// @todo: Check that this new subnet does not cross boundaries of any
  251. /// other already defined subnet.
  252. if (isDuplicate(*subnet)) {
  253. isc_throw(isc::dhcp::DuplicateSubnetID, "ID of the new IPv4 subnet '"
  254. << subnet->getID() << "' is already in use");
  255. }
  256. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_ADD_SUBNET4)
  257. .arg(subnet->toText());
  258. subnets4_.push_back(subnet);
  259. }
  260. void CfgMgr::deleteOptionDefs() {
  261. option_def_spaces_.clearItems();
  262. }
  263. void CfgMgr::deleteSubnets4() {
  264. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DELETE_SUBNET4);
  265. subnets4_.clear();
  266. }
  267. void CfgMgr::deleteSubnets6() {
  268. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DELETE_SUBNET6);
  269. subnets6_.clear();
  270. }
  271. std::string CfgMgr::getDataDir() {
  272. return (datadir_);
  273. }
  274. bool
  275. CfgMgr::isDuplicate(const Subnet4& subnet) const {
  276. for (Subnet4Collection::const_iterator subnet_it = subnets4_.begin();
  277. subnet_it != subnets4_.end(); ++subnet_it) {
  278. if ((*subnet_it)->getID() == subnet.getID()) {
  279. return (true);
  280. }
  281. }
  282. return (false);
  283. }
  284. bool
  285. CfgMgr::isDuplicate(const Subnet6& subnet) const {
  286. for (Subnet6Collection::const_iterator subnet_it = subnets6_.begin();
  287. subnet_it != subnets6_.end(); ++subnet_it) {
  288. if ((*subnet_it)->getID() == subnet.getID()) {
  289. return (true);
  290. }
  291. }
  292. return (false);
  293. }
  294. void
  295. CfgMgr::setD2ClientConfig(D2ClientConfigPtr& new_config) {
  296. d2_client_mgr_.setD2ClientConfig(new_config);
  297. }
  298. bool
  299. CfgMgr::ddnsEnabled() {
  300. return (d2_client_mgr_.ddnsEnabled());
  301. }
  302. const D2ClientConfigPtr&
  303. CfgMgr::getD2ClientConfig() const {
  304. return (d2_client_mgr_.getD2ClientConfig());
  305. }
  306. D2ClientMgr&
  307. CfgMgr::getD2ClientMgr() {
  308. return (d2_client_mgr_);
  309. }
  310. ConfigurationPtr
  311. CfgMgr::getConfiguration() {
  312. return (configuration_);
  313. }
  314. CfgMgr::CfgMgr()
  315. : datadir_(DHCP_DATA_DIR), echo_v4_client_id_(true),
  316. d2_client_mgr_(), configuration_(new Configuration()) {
  317. // DHCP_DATA_DIR must be set set with -DDHCP_DATA_DIR="..." in Makefile.am
  318. // Note: the definition of DHCP_DATA_DIR needs to include quotation marks
  319. // See AM_CPPFLAGS definition in Makefile.am
  320. }
  321. CfgMgr::~CfgMgr() {
  322. }
  323. }; // end of isc::dhcp namespace
  324. }; // end of isc namespace