cfg_iface.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright (C) 2014-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. #include <config.h>
  7. #include <dhcp/iface_mgr.h>
  8. #include <dhcpsrv/dhcpsrv_log.h>
  9. #include <dhcpsrv/cfg_iface.h>
  10. #include <util/strutil.h>
  11. #include <boost/bind.hpp>
  12. #include <boost/foreach.hpp>
  13. #include <algorithm>
  14. using namespace isc::asiolink;
  15. namespace isc {
  16. namespace dhcp {
  17. const char* CfgIface::ALL_IFACES_KEYWORD = "*";
  18. CfgIface::CfgIface()
  19. : wildcard_used_(false), socket_type_(SOCKET_RAW) {
  20. }
  21. void
  22. CfgIface::closeSockets() const {
  23. IfaceMgr::instance().closeSockets();
  24. }
  25. bool
  26. CfgIface::equals(const CfgIface& other) const {
  27. return (iface_set_ == other.iface_set_ &&
  28. address_map_ == other.address_map_ &&
  29. wildcard_used_ == other.wildcard_used_ &&
  30. socket_type_ == other.socket_type_);
  31. }
  32. bool
  33. CfgIface::multipleAddressesPerInterfaceActive() const {
  34. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  35. BOOST_FOREACH(IfacePtr iface, ifaces) {
  36. if (iface->countActive4() > 1) {
  37. return (true);
  38. }
  39. }
  40. return (false);
  41. }
  42. void
  43. CfgIface::openSockets(const uint16_t family, const uint16_t port,
  44. const bool use_bcast) const {
  45. // Close any open sockets because we're going to modify some properties
  46. // of the IfaceMgr. Those modifications require that sockets are closed.
  47. closeSockets();
  48. // If wildcard interface '*' was not specified, set all interfaces to
  49. // inactive state. We will later enable them selectively using the
  50. // interface names specified by the user. If wildcard interface was
  51. // specified, mark all interfaces active. In all cases, mark loopback
  52. // inactive.
  53. setState(family, !wildcard_used_, true);
  54. IfaceMgr& iface_mgr = IfaceMgr::instance();
  55. // Remove selection of unicast addresses from all interfaces.
  56. iface_mgr.clearUnicasts();
  57. // For the DHCPv4 server, if the user has selected that raw sockets
  58. // should be used, we will try to configure the Interface Manager to
  59. // support the direct responses to the clients that don't have the
  60. // IP address. This should effectively turn on the use of raw
  61. // sockets. However, this may be unsupported on some operating
  62. // systems, so there is no guarantee.
  63. if ((family == AF_INET) && (!IfaceMgr::instance().isTestMode())) {
  64. iface_mgr.setMatchingPacketFilter(socket_type_ == SOCKET_RAW);
  65. if ((socket_type_ == SOCKET_RAW) &&
  66. !iface_mgr.isDirectResponseSupported()) {
  67. LOG_WARN(dhcpsrv_logger, DHCPSRV_CFGMGR_SOCKET_RAW_UNSUPPORTED);
  68. }
  69. }
  70. // If there is no wildcard interface specified, we will have to iterate
  71. // over the names specified by the caller and enable them.
  72. if (!wildcard_used_) {
  73. for (IfaceSet::const_iterator iface_name = iface_set_.begin();
  74. iface_name != iface_set_.end(); ++iface_name) {
  75. IfacePtr iface = IfaceMgr::instance().getIface(*iface_name);
  76. // This shouldn't really happen because we are checking the
  77. // names of interfaces when they are being added (use()
  78. // function). But, if someone has triggered detection of
  79. // interfaces since then, some interfaces may have disappeared.
  80. if (iface == NULL) {
  81. isc_throw(Unexpected,
  82. "fail to open socket on interface '"
  83. << *iface_name << "' as this interface doesn't"
  84. " exist");
  85. } else if (family == AF_INET) {
  86. iface->inactive4_ = false;
  87. setIfaceAddrsState(family, true, *iface);
  88. } else {
  89. iface->inactive6_ = false;
  90. }
  91. }
  92. }
  93. // Select unicast sockets for DHCPv6 or activate specific IPv4 addresses
  94. // for DHCPv4.
  95. for (ExplicitAddressMap::const_iterator unicast = address_map_.begin();
  96. unicast != address_map_.end(); ++unicast) {
  97. IfacePtr iface = IfaceMgr::instance().getIface(unicast->first);
  98. if (iface == NULL) {
  99. isc_throw(Unexpected,
  100. "fail to open unicast socket on interface '"
  101. << unicast->first << "' as this interface doesn't"
  102. " exist");
  103. }
  104. if (family == AF_INET6) {
  105. iface->addUnicast(unicast->second);
  106. iface->inactive6_ = false;
  107. } else {
  108. iface->setActive(unicast->second, true);
  109. iface->inactive4_ = false;
  110. }
  111. }
  112. // Set the callback which is called when the socket fails to open
  113. // for some specific interface. This callback will simply log a
  114. // warning message.
  115. IfaceMgrErrorMsgCallback error_callback =
  116. boost::bind(&CfgIface::socketOpenErrorHandler, _1);
  117. bool sopen;
  118. if (family == AF_INET) {
  119. // Use broadcast only if we're using raw sockets. For the UDP sockets,
  120. // we only handle the relayed (unicast) traffic.
  121. const bool can_use_bcast = use_bcast && (socket_type_ == SOCKET_RAW);
  122. // Opening multiple raw sockets handling brodcast traffic on the single
  123. // interface may lead to processing the same message multiple times.
  124. // We don't prohibit such configuration because raw sockets can as well
  125. // handle the relayed traffic. We have to issue a warning, however, to
  126. // draw administrator's attention.
  127. if (can_use_bcast && multipleAddressesPerInterfaceActive()) {
  128. LOG_WARN(dhcpsrv_logger, DHCPSRV_MULTIPLE_RAW_SOCKETS_PER_IFACE);
  129. }
  130. sopen = IfaceMgr::instance().openSockets4(port, can_use_bcast, error_callback);
  131. } else {
  132. // use_bcast is ignored for V6.
  133. sopen = IfaceMgr::instance().openSockets6(port, error_callback);
  134. }
  135. // If no socket were opened, log a warning because the server will
  136. // not respond to any queries.
  137. if (!sopen) {
  138. LOG_WARN(dhcpsrv_logger, DHCPSRV_NO_SOCKETS_OPEN);
  139. }
  140. }
  141. void
  142. CfgIface::reset() {
  143. wildcard_used_ = false;
  144. iface_set_.clear();
  145. address_map_.clear();
  146. useSocketType(AF_INET, SOCKET_RAW);
  147. }
  148. void
  149. CfgIface::setState(const uint16_t family, const bool inactive,
  150. const bool loopback_inactive) const {
  151. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  152. BOOST_FOREACH(IfacePtr iface, ifaces) {
  153. bool iface_inactive = iface->flag_loopback_ ? loopback_inactive : inactive;
  154. if (family == AF_INET) {
  155. iface->inactive4_ = iface_inactive;
  156. } else {
  157. iface->inactive6_ = iface_inactive;
  158. }
  159. // Activate/deactivate all addresses.
  160. setIfaceAddrsState(family, !inactive, *iface);
  161. }
  162. }
  163. void
  164. CfgIface::setIfaceAddrsState(const uint16_t family, const bool active,
  165. Iface& iface) const {
  166. // Activate/deactivate all addresses.
  167. BOOST_FOREACH(Iface::Address addr, iface.getAddresses()) {
  168. if (addr.get().getFamily() == family) {
  169. iface.setActive(addr.get(), active);
  170. }
  171. }
  172. }
  173. void
  174. CfgIface::socketOpenErrorHandler(const std::string& errmsg) {
  175. LOG_WARN(dhcpsrv_logger, DHCPSRV_OPEN_SOCKET_FAIL).arg(errmsg);
  176. }
  177. std::string
  178. CfgIface::socketTypeToText() const {
  179. switch (socket_type_) {
  180. case SOCKET_RAW:
  181. return ("raw");
  182. case SOCKET_UDP:
  183. return ("udp");
  184. default:
  185. ;
  186. }
  187. isc_throw(Unexpected, "unsupported socket type " << socket_type_);
  188. }
  189. CfgIface::SocketType
  190. CfgIface::textToSocketType(const std::string& socket_type_name) const {
  191. if (socket_type_name == "udp") {
  192. return (SOCKET_UDP);
  193. } else if (socket_type_name == "raw") {
  194. return (SOCKET_RAW);
  195. } else {
  196. isc_throw(InvalidSocketType, "unsupported socket type '"
  197. << socket_type_name << "'");
  198. }
  199. }
  200. void
  201. CfgIface::use(const uint16_t family, const std::string& iface_name) {
  202. // The interface name specified may have two formats:
  203. // - "interface-name", e.g. eth0
  204. // - "interface-name/address", e.g. eth0/10.0.0.1 or eth/2001:db8:1::1
  205. // The latter format is used to open unicast socket on the specified
  206. // interface. Here we are detecting which format was used and we strip
  207. // all extraneous spaces.
  208. size_t pos = iface_name.find("/");
  209. std::string name;
  210. std::string addr_str;
  211. // There is no unicast address so the whole string is an interface name.
  212. if (pos == std::string::npos) {
  213. name = util::str::trim(iface_name);
  214. if (name.empty()) {
  215. isc_throw(InvalidIfaceName,
  216. "empty interface name used in configuration");
  217. } else if (name != ALL_IFACES_KEYWORD) {
  218. if (IfaceMgr::instance().getIface(name) == NULL) {
  219. isc_throw(NoSuchIface, "interface '" << name
  220. << "' doesn't exist in the system");
  221. }
  222. } else if (wildcard_used_) {
  223. isc_throw(DuplicateIfaceName, "the wildcard interface '"
  224. << ALL_IFACES_KEYWORD << "' can only be specified once");
  225. } else {
  226. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
  227. DHCPSRV_CFGMGR_ALL_IFACES_ACTIVE);
  228. wildcard_used_ = true;
  229. }
  230. } else {
  231. // The interface name includes the address on which the socket should
  232. // be opened, and we need to split interface name and the address to
  233. // two variables.
  234. name = util::str::trim(iface_name.substr(0, pos));
  235. addr_str = util::str::trim(iface_name.substr(pos + 1));
  236. // Interface name must not be empty.
  237. if (name.empty()) {
  238. isc_throw(InvalidIfaceName,
  239. "empty interface name specified in the"
  240. " interface configuration");
  241. }
  242. // An address following the interface name must not be empty.
  243. if (addr_str.empty()) {
  244. isc_throw(InvalidIfaceName,
  245. "empty address specified in the interface"
  246. << " configuration");
  247. }
  248. // Interface name must not be the wildcard name.
  249. if (name == ALL_IFACES_KEYWORD) {
  250. isc_throw(InvalidIfaceName,
  251. "wildcard interface name '" << ALL_IFACES_KEYWORD
  252. << "' must not be used in conjunction with an"
  253. " address");
  254. }
  255. // Interface must exist.
  256. IfacePtr iface = IfaceMgr::instance().getIface(name);
  257. if (!iface) {
  258. isc_throw(NoSuchIface, "interface '" << name
  259. << "' doesn't exist in the system");
  260. }
  261. // Convert address string. This may throw an exception if the address
  262. // is invalid.
  263. IOAddress addr(addr_str);
  264. // Validate V6 address.
  265. if (family == AF_INET6) {
  266. // Check that the address is a valid unicast address.
  267. if (!addr.isV6() || addr.isV6Multicast()) {
  268. isc_throw(InvalidIfaceName, "address '" << addr << "' is not"
  269. " a valid IPv6 unicast address");
  270. }
  271. // There are valid cases where link local address can be specified to
  272. // receive unicast traffic, e.g. sent by relay agent.
  273. if (addr.isV6LinkLocal()) {
  274. LOG_WARN(dhcpsrv_logger, DHCPSRV_CFGMGR_UNICAST_LINK_LOCAL)
  275. .arg(addr.toText()).arg(name);
  276. }
  277. } else {
  278. if (!addr.isV4()) {
  279. isc_throw(InvalidIfaceName, "address '" << addr << "' is not"
  280. " a valid IPv4 address");
  281. }
  282. }
  283. // Interface must have this address assigned.
  284. if (!iface->hasAddress(addr)) {
  285. isc_throw(NoSuchAddress,
  286. "interface '" << name << "' doesn't have address '"
  287. << addr << "' assigned");
  288. }
  289. // For the IPv4, if the interface name was specified (instead of the interface-
  290. // address tuple) all addresses are already activated. Adding an explicit address
  291. // for the interface should result in error.
  292. if ((family == AF_INET) && (iface_set_.find(iface->getName()) != iface_set_.end())) {
  293. isc_throw(DuplicateIfaceName, "interface '" << iface->getName()
  294. << "' has already been selected");
  295. }
  296. // Check if the address hasn't been selected already.
  297. std::pair<const std::string, IOAddress> iface_address_tuple(name, addr);
  298. if (std::find(address_map_.begin(), address_map_.end(),
  299. iface_address_tuple) != address_map_.end()) {
  300. isc_throw(DuplicateAddress, "must not select address '"
  301. << addr << "' for interface '" << name << "' "
  302. "because this address is already selected");
  303. }
  304. if (family == AF_INET6) {
  305. LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_USE_UNICAST)
  306. .arg(addr.toText()).arg(name);
  307. } else {
  308. LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_USE_ADDRESS)
  309. .arg(addr.toText()).arg(name);
  310. }
  311. address_map_.insert(std::pair<std::string, IOAddress>(name, addr));
  312. }
  313. // If interface name was explicitly specified without an address, we will
  314. // insert the interface name to the set of enabled interfaces.
  315. if ((name != ALL_IFACES_KEYWORD) && addr_str.empty()) {
  316. // An interface has been selected or an IPv4 address on this interface
  317. // has been selected it is not allowed to select the whole interface.
  318. if ((iface_set_.find(name) != iface_set_.end()) ||
  319. ((family == AF_INET) && address_map_.count(name) > 0)) {
  320. isc_throw(DuplicateIfaceName, "interface '" << name
  321. << "' has already been specified");
  322. }
  323. // Log that we're listening on the specific interface and that the
  324. // address is not explicitly specified.
  325. if (addr_str.empty()) {
  326. LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_ADD_IFACE).arg(name);
  327. }
  328. iface_set_.insert(name);
  329. }
  330. }
  331. void
  332. CfgIface::useSocketType(const uint16_t family,
  333. const SocketType& socket_type) {
  334. if (family != AF_INET) {
  335. isc_throw(InvalidSocketType, "socket type must not be specified for"
  336. " the DHCPv6 server");
  337. }
  338. socket_type_ = socket_type;
  339. LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_SOCKET_TYPE_SELECT)
  340. .arg(socketTypeToText());
  341. }
  342. void
  343. CfgIface::useSocketType(const uint16_t family,
  344. const std::string& socket_type_name) {
  345. useSocketType(family, textToSocketType(socket_type_name));
  346. }
  347. } // end of isc::dhcp namespace
  348. } // end of isc namespace