dhcp4o6_ipc.cc 8.5 KB

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