Kea  2.3.4-git
dhcp4o6_ipc.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-2019 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 
7 #include <config.h>
8 
9 #include <dhcp/dhcp6.h>
10 #include <dhcp/iface_mgr.h>
11 #include <dhcp/option6_addrlst.h>
12 #include <dhcp/option_custom.h>
13 #include <dhcp/option_int.h>
14 #include <dhcp/option_string.h>
15 #include <dhcp/option_vendor.h>
16 #include <dhcpsrv/dhcp4o6_ipc.h>
17 #include <dhcpsrv/dhcpsrv_log.h>
18 
19 #include <boost/pointer_cast.hpp>
20 
21 #include <errno.h>
22 #include <netinet/in.h>
23 #include <fcntl.h>
24 #include <string>
25 
26 using namespace isc::asiolink;
27 using namespace isc::util;
28 using namespace std;
29 
30 namespace isc {
31 namespace dhcp {
32 
33 Dhcp4o6IpcBase::Dhcp4o6IpcBase() : port_(0), socket_fd_(-1) {}
34 
36  close();
37 }
38 
39 int Dhcp4o6IpcBase::open(uint16_t port, EndpointType endpoint_type) {
40  // Don't check if the value is greater than 65534 as it is done
41  // by callers before they cast the value to 16 bits.
42 
43  if (port == port_) {
44  // No change: nothing to do
45  return (socket_fd_);
46  }
47 
48  // Open socket
49  int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
50  if (sock < 0) {
51  isc_throw(Dhcp4o6IpcError, "Failed to create DHCP4o6 socket.");
52  }
53 
54  // Set no blocking
55  if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
56  ::close(sock);
58  "Failed to set O_NONBLOCK on DHCP4o6 socket.");
59  }
60 
61  // Bind to the local address
62  struct sockaddr_in6 local6;
63  memset(&local6, 0, sizeof(local6));
64  local6.sin6_family = AF_INET6;
65 #ifdef HAVE_SA_LEN
66  local6.sin6_len = sizeof(local6);
67 #endif
68  if (endpoint_type == ENDPOINT_TYPE_V6) {
69  local6.sin6_port = htons(port);
70  } else {
71  local6.sin6_port = htons(port + 1);
72  }
73  // We'll connect to the loopback address so bind to it too.
74  local6.sin6_addr.s6_addr[15] = 1;
75  if (::bind(sock, (struct sockaddr *)&local6, sizeof(local6)) < 0) {
76  ::close(sock);
77  isc_throw(Dhcp4o6IpcError, "Failed to bind DHCP4o6 socket.");
78  }
79 
80  // Connect to the remote address
81  struct sockaddr_in6 remote6;
82  memset(&remote6, 0, sizeof(remote6));
83  remote6.sin6_family = AF_INET6;
84 #ifdef HAVE_SA_LEN
85  remote6.sin6_len = sizeof(remote6);
86 #endif
87  if (endpoint_type == ENDPOINT_TYPE_V6) {
88  remote6.sin6_port = htons(port + 1);
89  } else {
90  remote6.sin6_port = htons(port);
91  }
92  // At least OpenBSD requires the remote address to not be left
93  // unspecified, so we set it to the loopback address.
94  remote6.sin6_addr.s6_addr[15] = 1;
95  if (connect(sock, reinterpret_cast<const struct sockaddr*>(&remote6),
96  sizeof(remote6)) < 0) {
97  ::close(sock);
98  isc_throw(Dhcp4o6IpcError, "Failed to connect DHCP4o6 socket.");
99  }
100 
101  if (socket_fd_ != -1) {
102  if (dup2(sock, socket_fd_) == -1) {
103  ::close(sock);
104  isc_throw(Dhcp4o6IpcError, "Failed to duplicate DHCP4o6 socket.");
105  }
106  if (sock != socket_fd_) {
107  ::close(sock);
108  sock = socket_fd_;
109  }
110  }
111 
112  // Success
113  port_ = port;
114  socket_fd_ = sock;
115  return (socket_fd_);
116 }
117 
119  port_ = 0;
120  if (socket_fd_ != -1) {
123  socket_fd_ = -1;
124  }
125 }
126 
128  uint8_t buf[65536];
129  ssize_t cc = recv(socket_fd_, buf, sizeof(buf), 0);
130  if (cc < 0) {
131  isc_throw(Dhcp4o6IpcError, "Failed to receive on DHCP4o6 socket.");
132  }
133  Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(buf, cc));
134  pkt->updateTimestamp();
135 
136  // Get interface name and remote address
137  pkt->unpack();
138 
139  // Vendor option is initially NULL. If we find the instance of the vendor
140  // option with the ISC enterprise id this pointer will point to it.
141  OptionVendorPtr option_vendor;
142 
143  // Get all vendor option and look for the one with the ISC enterprise id.
144  OptionCollection vendor_options = pkt->getOptions(D6O_VENDOR_OPTS);
145  for (OptionCollection::const_iterator opt = vendor_options.begin();
146  opt != vendor_options.end(); ++opt) {
147  option_vendor = boost::dynamic_pointer_cast<OptionVendor>(opt->second);
148  if (option_vendor) {
149  if (option_vendor->getVendorId() == ENTERPRISE_ID_ISC) {
150  break;
151  }
152  option_vendor.reset();
153  }
154  }
155 
156  // Vendor option must exist.
157  if (!option_vendor) {
159  .arg("no ISC vendor option");
160  isc_throw(Dhcp4o6IpcError, "malformed packet (no ISC vendor option)");
161  }
162 
163  // The option carrying interface name is required.
164  OptionStringPtr ifname = boost::dynamic_pointer_cast<
165  OptionString>(option_vendor->getOption(ISC_V6_4O6_INTERFACE));
166  if (!ifname) {
168  .arg("no interface suboption");
170  "malformed packet (interface suboption missing "
171  "or has incorrect type)");
172  }
173 
174  // Check if this interface is present in the system.
175  IfacePtr iface = IfaceMgr::instance().getIface(ifname->getValue());
176  if (!iface) {
178  .arg("can't get interface " + ifname->getValue());
180  "malformed packet (unknown interface "
181  + ifname->getValue() + ")");
182  }
183 
184  // Get the option holding source IPv6 address.
185  OptionCustomPtr srcs = boost::dynamic_pointer_cast<
186  OptionCustom>(option_vendor->getOption(ISC_V6_4O6_SRC_ADDRESS));
187  if (!srcs) {
189  .arg("no source address suboption");
191  "malformed packet (source address suboption missing "
192  "or has incorrect type)");
193  }
194 
195  // Get the option holding source port.
196  OptionUint16Ptr sport = boost::dynamic_pointer_cast<
197  OptionUint16>(option_vendor->getOption(ISC_V6_4O6_SRC_PORT));
198  if (!sport) {
200  .arg("no source port suboption");
202  "malformed packet (source port suboption missing "
203  "or has incorrect type)");
204  }
205 
206  // Update the packet.
207  pkt->setRemoteAddr(srcs->readAddress());
208  pkt->setRemotePort(sport->getValue());
209  pkt->setIface(iface->getName());
210  pkt->setIndex(iface->getIndex());
211 
212  // Remove options that have been added by the IPC sender.
213  static_cast<void>(option_vendor->delOption(ISC_V6_4O6_INTERFACE));
214  static_cast<void>(option_vendor->delOption(ISC_V6_4O6_SRC_ADDRESS));
215  static_cast<void>(option_vendor->delOption(ISC_V6_4O6_SRC_PORT));
216 
217  // If there are no more options, the IPC sender has probably created the
218  // vendor option, in which case we should remove it here.
219  if (option_vendor->getOptions().empty()) {
220  static_cast<void>(pkt->delOption(D6O_VENDOR_OPTS));
221  }
222 
223  return (pkt);
224 }
225 
226 void Dhcp4o6IpcBase::send(const Pkt6Ptr& pkt) {
227  // This shouldn't happen, i.e. send() shouldn't be called if there is
228  // no message.
229  if (!pkt) {
230  isc_throw(Dhcp4o6IpcError, "DHCP4o6 message must not be NULL while"
231  " trying to send it over the IPC");
232  }
233 
234  // Disabled: nowhere to send
235  if (socket_fd_ == -1) {
236  isc_throw(Dhcp4o6IpcError, "unable to send DHCP4o6 message because"
237  " IPC socket is closed");
238  }
239 
240  // Check if vendor option exists.
241  OptionVendorPtr option_vendor = boost::dynamic_pointer_cast<
242  OptionVendor>(pkt->getOption(D6O_VENDOR_OPTS));
243 
244  // If vendor option doesn't exist or its enterprise id is not ISC's
245  // enterprise id, let's create it.
246  if (!option_vendor ||
247  (option_vendor->getVendorId() != ENTERPRISE_ID_ISC)) {
248  option_vendor.reset(new OptionVendor(Option::V6, ENTERPRISE_ID_ISC));
249  pkt->addOption(option_vendor);
250 
251  }
252 
253  // Push interface name and source address in it
254  option_vendor->addOption(OptionStringPtr(new OptionString(Option::V6,
255  ISC_V6_4O6_INTERFACE,
256  pkt->getIface())));
257  option_vendor->addOption(Option6AddrLstPtr(new Option6AddrLst(
258  ISC_V6_4O6_SRC_ADDRESS,
259  pkt->getRemoteAddr())));
260  option_vendor->addOption(OptionUint16Ptr(new OptionUint16(Option::V6,
261  ISC_V6_4O6_SRC_PORT,
262  pkt->getRemotePort())));
263  // Get packet content
264  OutputBuffer& buf = pkt->getBuffer();
265  buf.clear();
266  pkt->pack();
267 
268  // Try to send the message.
269  if (::send(socket_fd_, buf.getData(), buf.getLength(), 0) < 0) {
271  "failed to send DHCP4o6 message over the IPC: "
272  << strerror(errno));
273  }
274 }
275 
276 }; // namespace dhcp
277 
278 }; // namespace isc
IfacePtr getIface(int ifindex)
Returns interface specified interface index.
Definition: iface_mgr.cc:902
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
boost::shared_ptr< OptionCustom > OptionCustomPtr
A pointer to the OptionCustom object.
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
Definition: iface_mgr.h:487
virtual void open()=0
Open communication socket (for derived classes).
Represents a DHCPv6 packet.
Definition: pkt6.h:44
boost::shared_ptr< OptionString > OptionStringPtr
Pointer to the OptionString object.
STL namespace.
DHCPv6 Option class for handling list of IPv6 addresses.
Forward declaration to OptionInt.
OptionInt< uint16_t > OptionUint16
Definition: option_int.h:32
const isc::log::MessageID DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET
boost::shared_ptr< Option6AddrLst > Option6AddrLstPtr
Pointer to the Option6AddrLst object.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the Dhcp4o6IpcBase class.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
boost::shared_ptr< OptionUint16 > OptionUint16Ptr
Definition: option_int.h:33
Definition: edns.h:19
int socket_fd_
Socket descriptor.
Definition: dhcp4o6_ipc.h:125
uint16_t port_
Port number configured for IPC communication.
Definition: dhcp4o6_ipc.h:122
void deleteExternalSocket(int socketfd)
Deletes external socket.
Definition: iface_mgr.cc:348
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
Definition: buffer.h:401
Exception thrown when error occurs as a result of use of IPC.
Definition: dhcp4o6_ipc.h:24
void clear()
Clear buffer content.
Definition: buffer.h:451
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:331
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
size_t getLength() const
Return the length of data written in the buffer.
Definition: buffer.h:403
Defines the logger used by the top-level component of kea-lfc.
virtual ~Dhcp4o6IpcBase()
Destructor.
Definition: dhcp4o6_ipc.cc:35
EndpointType
Endpoint type: DHCPv4 or DHCPv6 server.
Definition: dhcp4o6_ipc.h:65
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:53
Pkt6Ptr receive()
Receive message over IPC.
Definition: dhcp4o6_ipc.cc:127
void close()
Close communication socket.
Definition: dhcp4o6_ipc.cc:118
void send(const Pkt6Ptr &pkt)
Send message over IPC.
Definition: dhcp4o6_ipc.cc:226
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
Option with defined data fields represented as buffers that can be accessed using data field index...
Definition: option_custom.h:32
boost::shared_ptr< OptionVendor > OptionVendorPtr
Pointer to a vendor option.
Class which represents an option carrying a single string value.
Definition: option_string.h:28
This class represents vendor-specific information option.
Definition: option_vendor.h:30