dhcp4o6_ipc.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (C) 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/dhcp6.h>
  16. #include <dhcp/iface_mgr.h>
  17. #include <dhcp/option6_addrlst.h>
  18. #include <dhcp/option_custom.h>
  19. #include <dhcp/option_string.h>
  20. #include <dhcp/option_vendor.h>
  21. #include <dhcpsrv/dhcp4o6_ipc.h>
  22. #include <boost/pointer_cast.hpp>
  23. #include <errno.h>
  24. #include <netinet/in.h>
  25. #include <string>
  26. using namespace isc::asiolink;
  27. using namespace isc::util;
  28. using namespace std;
  29. namespace isc {
  30. namespace dhcp {
  31. Dhcp4o6IpcBase::Dhcp4o6IpcBase() : port_(0), socket_fd_(-1) {}
  32. Dhcp4o6IpcBase::~Dhcp4o6IpcBase() {
  33. close();
  34. }
  35. int Dhcp4o6IpcBase::open(const uint16_t port, const EndpointType& endpoint_type) {
  36. // Check if the port value is correct.
  37. if (port > 65534) {
  38. isc_throw(Dhcp4o6IpcError, "specified port " << port << " is out of"
  39. " range. The port value must not be greater than 65534 ");
  40. }
  41. if (port == port_) {
  42. // No change: nothing to do
  43. return (socket_fd_);
  44. }
  45. // Open socket
  46. int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  47. if (sock < 0) {
  48. isc_throw(Dhcp4o6IpcError, "Failed to create DHCP4o6 socket.");
  49. }
  50. // Set reuse address
  51. int flag = 1;
  52. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, static_cast<const void*>(&flag),
  53. sizeof(flag)) < 0) {
  54. ::close(sock);
  55. isc_throw(Dhcp4o6IpcError, "Failed to set SO_REUSEADDR on DHCP4o6 socket.");
  56. }
  57. // Set no blocking
  58. if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
  59. ::close(sock);
  60. isc_throw(Dhcp4o6IpcError, "Failed to set O_NONBLOCK on DHCP4o6 socket.");
  61. }
  62. // Bind to the local address
  63. struct sockaddr_in6 local6;
  64. memset(&local6, 0, sizeof(local6));
  65. local6.sin6_family = AF_INET6;
  66. #ifdef HAVE_SA_LEN
  67. local6.sin6_len = sizeof(local6);
  68. #endif
  69. if (endpoint_type == ENDPOINT_TYPE_V6) {
  70. local6.sin6_port = htons(port);
  71. } else {
  72. local6.sin6_port = htons(port + 1);
  73. }
  74. if (bind(sock, (struct sockaddr *)&local6, sizeof(local6)) < 0) {
  75. ::close(sock);
  76. isc_throw(Dhcp4o6IpcError, "Failed to bind DHCP4o6 socket.");
  77. }
  78. // Connect to the remote address
  79. struct sockaddr_in6 remote6;
  80. memset(&remote6, 0, sizeof(remote6));
  81. remote6.sin6_family = AF_INET6;
  82. #ifdef HAVE_SA_LEN
  83. remote6.sin6_len = sizeof(remote6);
  84. #endif
  85. if (endpoint_type == ENDPOINT_TYPE_V6) {
  86. remote6.sin6_port = htons(port + 1);
  87. } else {
  88. remote6.sin6_port = htons(port);
  89. }
  90. if (connect(sock, reinterpret_cast<const struct sockaddr*>(&remote6),
  91. sizeof(remote6)) < 0) {
  92. ::close(sock);
  93. isc_throw(Dhcp4o6IpcError, "Failed to connect DHCP4o6 socket.");
  94. }
  95. if (socket_fd_ != -1) {
  96. if (dup2(sock, socket_fd_) == -1) {
  97. ::close(sock);
  98. isc_throw(Dhcp4o6IpcError, "Failed to duplicate DHCP4o6 socket.");
  99. }
  100. if (sock != socket_fd_) {
  101. ::close(sock);
  102. sock = socket_fd_;
  103. }
  104. }
  105. // Success
  106. port_ = port;
  107. socket_fd_ = sock;
  108. return (socket_fd_);
  109. }
  110. void Dhcp4o6IpcBase::close() {
  111. port_ = 0;
  112. if (socket_fd_ != -1) {
  113. IfaceMgr::instance().deleteExternalSocket(socket_fd_);
  114. ::close(socket_fd_);
  115. socket_fd_ = -1;
  116. }
  117. }
  118. Pkt6Ptr Dhcp4o6IpcBase::receive() {
  119. uint8_t buf[65536];
  120. ssize_t cc = recv(socket_fd_, buf, sizeof(buf), 0);
  121. if (cc < 0) {
  122. isc_throw(Dhcp4o6IpcError, "Failed to receive on DHCP4o6 socket.");
  123. }
  124. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(buf, cc));
  125. pkt->updateTimestamp();
  126. // Get interface name and remote address
  127. pkt->unpack();
  128. // Vendor option is initially NULL. If we find the instance of the vendor
  129. // option with the ISC enterprise id this pointer will point to it.
  130. OptionVendorPtr option_vendor;
  131. // Get all vendor option and look for the one with the ISC enterprise id.
  132. OptionCollection vendor_options = pkt->getOptions(D6O_VENDOR_OPTS);
  133. for (OptionCollection::const_iterator opt = vendor_options.begin();
  134. opt != vendor_options.end(); ++opt) {
  135. option_vendor = boost::dynamic_pointer_cast<OptionVendor>(opt->second);
  136. if (option_vendor) {
  137. if (option_vendor->getVendorId() == ENTERPRISE_ID_ISC) {
  138. break;
  139. }
  140. } else {
  141. option_vendor.reset();
  142. }
  143. }
  144. // Vendor option must exist.
  145. if (!option_vendor) {
  146. isc_throw(Dhcp4o6IpcError, "option " << D6O_VENDOR_OPTS
  147. << " with ISC enterprise id is not present in the DHCP4o6"
  148. " message sent between the servers");
  149. }
  150. // The option carrying interface name is required.
  151. OptionStringPtr ifname = boost::dynamic_pointer_cast<
  152. OptionString>(option_vendor->getOption(ISC_V6_4O6_INTERFACE));
  153. if (!ifname) {
  154. isc_throw(Dhcp4o6IpcError, "option " << D6O_VENDOR_OPTS
  155. << " doesn't contain the " << ISC_V6_4O6_INTERFACE
  156. << " option required in the DHCP4o6 message sent"
  157. " between Kea servers");
  158. }
  159. // Check if this interface is present in the system.
  160. IfacePtr iface = IfaceMgr::instance().getIface(ifname->getValue());
  161. if (!iface) {
  162. isc_throw(Dhcp4o6IpcError, "option " << ISC_V6_4O6_INTERFACE
  163. << " sent in the DHCP4o6 message contains non-existing"
  164. " interface name '" << ifname->getValue() << "'");
  165. }
  166. // Get the option holding source IPv6 address.
  167. OptionCustomPtr srcs = boost::dynamic_pointer_cast<
  168. OptionCustom>(option_vendor->getOption(ISC_V6_4O6_SRC_ADDRESS));
  169. if (!srcs) {
  170. isc_throw(Dhcp4o6IpcError, "option " << D6O_VENDOR_OPTS
  171. << " doesn't contain the " << ISC_V6_4O6_SRC_ADDRESS
  172. << " option required in the DHCP4o6 message sent"
  173. " between Kea servers");
  174. }
  175. // Update the packet.
  176. pkt->setRemoteAddr(srcs->readAddress());
  177. pkt->setIface(iface->getName());
  178. pkt->setIndex(iface->getIndex());
  179. // Remove options that have been added by the IPC sender.
  180. static_cast<void>(option_vendor->delOption(ISC_V6_4O6_INTERFACE));
  181. static_cast<void>(option_vendor->delOption(ISC_V6_4O6_SRC_ADDRESS));
  182. // If there are no more options, the IPC sender has probably created the
  183. // vendor option, in which case we should remove it here.
  184. if (option_vendor->getOptions().empty()) {
  185. static_cast<void>(pkt->delOption(D6O_VENDOR_OPTS));
  186. }
  187. return (pkt);
  188. }
  189. void Dhcp4o6IpcBase::send(const Pkt6Ptr& pkt) {
  190. // This shouldn't happen, i.e. send() shouldn't be called if there is
  191. // no message.
  192. if (!pkt) {
  193. isc_throw(Dhcp4o6IpcError, "DHCP4o6 message must not be NULL while"
  194. " trying to send it over the IPC");
  195. }
  196. // Disabled: nowhere to send
  197. if (socket_fd_ == -1) {
  198. isc_throw(Dhcp4o6IpcError, "unable to send DHCP4o6 message because"
  199. " IPC socket is closed");
  200. }
  201. // Check if vendor option exists.
  202. OptionVendorPtr option_vendor = dynamic_pointer_cast<
  203. OptionVendor>(pkt->getOption(D6O_VENDOR_OPTS));
  204. // If vendor option doesn't exist or its enterprise id is not ISC's
  205. // enterprise id, let's create it.
  206. if (!option_vendor || (option_vendor->getVendorId() != ENTERPRISE_ID_ISC)) {
  207. option_vendor.reset(new OptionVendor(Option::V6, ENTERPRISE_ID_ISC));
  208. pkt->addOption(option_vendor);
  209. }
  210. // Push interface name and source address in it
  211. option_vendor->addOption(OptionStringPtr(new OptionString(Option::V6,
  212. ISC_V6_4O6_INTERFACE,
  213. pkt->getIface())));
  214. option_vendor->addOption(Option6AddrLstPtr(new Option6AddrLst(ISC_V6_4O6_SRC_ADDRESS,
  215. pkt->getRemoteAddr())));
  216. // Get packet content
  217. OutputBuffer& buf = pkt->getBuffer();
  218. buf.clear();
  219. pkt->pack();
  220. // Try to send the message.
  221. if (::send(socket_fd_, buf.getData(), buf.getLength(), 0) < 0) {
  222. isc_throw(Dhcp4o6IpcError, "failed to send DHCP4o6 message over the IPC: "
  223. << strerror(errno));
  224. }
  225. }
  226. }; // namespace dhcp
  227. }; // namespace isc