dhcp6to4_ipc.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <util/buffer.h>
  8. #include <dhcp/iface_mgr.h>
  9. #include <dhcp/pkt6.h>
  10. #include <dhcpsrv/callout_handle_store.h>
  11. #include <dhcpsrv/cfgmgr.h>
  12. #include <dhcp6/dhcp6to4_ipc.h>
  13. #include <dhcp6/dhcp6_log.h>
  14. #include <dhcp6/dhcp6_srv.h>
  15. #include <exceptions/exceptions.h>
  16. #include <hooks/callout_handle.h>
  17. #include <hooks/hooks_log.h>
  18. #include <hooks/hooks_manager.h>
  19. #include <stats/stats_mgr.h>
  20. using namespace std;
  21. using namespace isc::hooks;
  22. namespace isc {
  23. namespace dhcp {
  24. Dhcp6to4Ipc::Dhcp6to4Ipc() : Dhcp4o6IpcBase() {}
  25. Dhcp6to4Ipc& Dhcp6to4Ipc::instance() {
  26. static Dhcp6to4Ipc dhcp6to4_ipc;
  27. return (dhcp6to4_ipc);
  28. }
  29. void Dhcp6to4Ipc::open() {
  30. uint32_t port = CfgMgr::instance().getStagingCfg()->getDhcp4o6Port();
  31. if (port == 0) {
  32. Dhcp4o6IpcBase::close();
  33. return;
  34. }
  35. if (port > 65534) {
  36. isc_throw(OutOfRange, "DHCP4o6 port " << port);
  37. }
  38. int old_fd = socket_fd_;
  39. socket_fd_ = Dhcp4o6IpcBase::open(static_cast<uint16_t>(port),
  40. ENDPOINT_TYPE_V6);
  41. if ((old_fd == -1) && (socket_fd_ != old_fd)) {
  42. IfaceMgr::instance().addExternalSocket(socket_fd_,
  43. Dhcp6to4Ipc::handler);
  44. }
  45. }
  46. void Dhcp6to4Ipc::handler() {
  47. Dhcp6to4Ipc& ipc = Dhcp6to4Ipc::instance();
  48. Pkt6Ptr pkt;
  49. try {
  50. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL, DHCP6_DHCP4O6_RECEIVING);
  51. // Receive message from IPC.
  52. pkt = ipc.receive();
  53. if (pkt) {
  54. LOG_DEBUG(packet6_logger, DBG_DHCP6_BASIC, DHCP6_DHCP4O6_PACKET_RECEIVED)
  55. .arg(static_cast<int>(pkt->getType()))
  56. .arg(pkt->getRemoteAddr().toText())
  57. .arg(pkt->getIface());
  58. }
  59. } catch (const std::exception& e) {
  60. LOG_DEBUG(packet6_logger,DBG_DHCP6_DETAIL, DHCP6_DHCP4O6_RECEIVE_FAIL)
  61. .arg(e.what());
  62. }
  63. if (!pkt) {
  64. return;
  65. }
  66. // Should we check it is a DHCPV6_DHCPV4_RESPONSE?
  67. // The received message has been unpacked by the receive() function. This
  68. // method could have modified the message so it's better to pack() it
  69. // again because we'll be forwarding it to a client.
  70. isc::util::OutputBuffer& buf = pkt->getBuffer();
  71. buf.clear();
  72. pkt->pack();
  73. // Don't use getType(): get the message type from the buffer as we
  74. // want to know if it is a relayed message (vs. internal message type).
  75. // getType() always returns the type of internal message.
  76. uint8_t msg_type = buf[0];
  77. if ((msg_type == DHCPV6_RELAY_FORW) || (msg_type == DHCPV6_RELAY_REPL)) {
  78. pkt->setRemotePort(DHCP6_SERVER_PORT);
  79. } else {
  80. pkt->setRemotePort(DHCP6_CLIENT_PORT);
  81. }
  82. // Can't call the pkt6_send callout because we don't have the query
  83. // Copied from Dhcpv6Srv::run_one() sending part
  84. try {
  85. // Let's execute all callouts registered for buffer6_send
  86. if (HooksManager::calloutsPresent(Dhcpv6Srv::getHookIndexBuffer6Send())) {
  87. CalloutHandlePtr callout_handle = getCalloutHandle(pkt);
  88. // Delete previously set arguments
  89. callout_handle->deleteAllArguments();
  90. // Enable copying options from the packet within hook library.
  91. ScopedEnableOptionsCopy<Pkt6> response6_options_copy(pkt);
  92. // Pass incoming packet as argument
  93. callout_handle->setArgument("response6", pkt);
  94. // Call callouts
  95. HooksManager::callCallouts(Dhcpv6Srv::getHookIndexBuffer6Send(),
  96. *callout_handle);
  97. // Callouts decided to skip the next processing step. The next
  98. // processing step would to parse the packet, so skip at this
  99. // stage means drop.
  100. if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP) {
  101. LOG_DEBUG(hooks_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_SEND_SKIP)
  102. .arg(pkt->getLabel());
  103. return;
  104. }
  105. /// @todo: Add support for DROP status
  106. callout_handle->getArgument("response6", pkt);
  107. }
  108. LOG_DEBUG(packet6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_RESPONSE_DATA)
  109. .arg(static_cast<int>(pkt->getType())).arg(pkt->toText());
  110. // Forward packet to the client.
  111. IfaceMgr::instance().send(pkt);
  112. // Update statistics accordingly for sent packet.
  113. Dhcpv6Srv::processStatsSent(pkt);
  114. } catch (const std::exception& e) {
  115. LOG_ERROR(packet6_logger, DHCP6_DHCP4O6_SEND_FAIL).arg(e.what());
  116. }
  117. }
  118. }; // namespace dhcp
  119. }; // namespace isc