cfg_iface.cc 15 KB

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