Kea 2.5.5
dhcp4o6_ipc.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2023 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>
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
26using namespace isc::asiolink;
27using namespace isc::util;
28using namespace std;
29
30namespace isc {
31namespace dhcp {
32
33Dhcp4o6IpcBase::Dhcp4o6IpcBase() : port_(0), socket_fd_(-1) {}
34
36 close();
37}
38
39int 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
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 // Vendor option is initially NULL. If we find the instance of the vendor
242 // option with the ISC enterprise id this pointer will point to it.
243 OptionVendorPtr option_vendor;
244
245 // Get all vendor option and look for the one with the ISC enterprise id.
246 OptionCollection vendor_options = pkt->getOptions(D6O_VENDOR_OPTS);
247 for (OptionCollection::const_iterator opt = vendor_options.begin();
248 opt != vendor_options.end(); ++opt) {
249 option_vendor = boost::dynamic_pointer_cast<OptionVendor>(opt->second);
250 if (option_vendor) {
251 if (option_vendor->getVendorId() == ENTERPRISE_ID_ISC) {
252 break;
253 }
254 option_vendor.reset();
255 }
256 }
257
258 // If vendor option doesn't exist or its enterprise id is not ISC's
259 // enterprise id, let's create it.
260 if (!option_vendor) {
261 option_vendor.reset(new OptionVendor(Option::V6, ENTERPRISE_ID_ISC));
262 pkt->addOption(option_vendor);
263 }
264
265 // Push interface name and source address in it
266 option_vendor->addOption(OptionStringPtr(new OptionString(Option::V6,
267 ISC_V6_4O6_INTERFACE,
268 pkt->getIface())));
269 option_vendor->addOption(Option6AddrLstPtr(new Option6AddrLst(ISC_V6_4O6_SRC_ADDRESS,
270 pkt->getRemoteAddr())));
271 option_vendor->addOption(OptionUint16Ptr(new OptionUint16(Option::V6,
272 ISC_V6_4O6_SRC_PORT,
273 pkt->getRemotePort())));
274 // Get packet content
275 OutputBuffer& buf = pkt->getBuffer();
276 buf.clear();
277 pkt->pack();
278
279 // Try to send the message.
280 if (::send(socket_fd_, buf.getData(), buf.getLength(), 0) < 0) {
282 "failed to send DHCP4o6 message over the IPC: "
283 << strerror(errno));
284 }
285}
286
287} // namespace dhcp
288} // namespace isc
void send(const Pkt6Ptr &pkt)
Send message over IPC.
Definition: dhcp4o6_ipc.cc:226
virtual ~Dhcp4o6IpcBase()
Destructor.
Definition: dhcp4o6_ipc.cc:35
Pkt6Ptr receive()
Receive message over IPC.
Definition: dhcp4o6_ipc.cc:127
uint16_t port_
Port number configured for IPC communication.
Definition: dhcp4o6_ipc.h:122
EndpointType
Endpoint type: DHCPv4 or DHCPv6 server.
Definition: dhcp4o6_ipc.h:65
void close()
Close communication socket.
Definition: dhcp4o6_ipc.cc:118
virtual void open()=0
Open communication socket (for derived classes).
Dhcp4o6IpcBase()
Constructor.
Definition: dhcp4o6_ipc.cc:33
int socket_fd_
Socket descriptor.
Definition: dhcp4o6_ipc.h:125
Exception thrown when error occurs as a result of use of IPC.
Definition: dhcp4o6_ipc.h:24
void deleteExternalSocket(int socketfd)
Deletes external socket.
Definition: iface_mgr.cc:350
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
Definition: iface_mgr.cc:866
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:54
DHCPv6 Option class for handling list of IPv6 addresses.
Option with defined data fields represented as buffers that can be accessed using data field index.
Definition: option_custom.h:32
Forward declaration to OptionInt.
Definition: option_int.h:49
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
Represents a DHCPv6 packet.
Definition: pkt6.h:44
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
void clear()
Clear buffer content.
Definition: buffer.h:451
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
Definition: buffer.h:401
Defines the Dhcp4o6IpcBase class.
@ D6O_VENDOR_OPTS
Definition: dhcp6.h:37
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
OptionInt< uint16_t > OptionUint16
Definition: option_int.h:32
boost::shared_ptr< OptionUint16 > OptionUint16Ptr
Definition: option_int.h:33
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
boost::shared_ptr< OptionVendor > OptionVendorPtr
Pointer to a vendor option.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
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
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
boost::shared_ptr< Option6AddrLst > Option6AddrLstPtr
Pointer to the Option6AddrLst object.
const isc::log::MessageID DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET
boost::shared_ptr< OptionString > OptionStringPtr
Pointer to the OptionString object.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:31
Definition: edns.h:19
Defines the logger used by the top-level component of kea-lfc.