cfg_iface.cc 16 KB

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