1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 | // Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <dhcp/dhcp6.h>
#include <dhcp/iface_mgr.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_int.h>
#include <dhcp/option_string.h>
#include <dhcp/option_vendor.h>
#include <dhcpsrv/dhcp4o6_ipc.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <boost/pointer_cast.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <errno.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <netinet/in.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fcntl.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::asiolink;
using namespace isc::util;
using namespace std;
namespace isc {
namespace dhcp {
Dhcp4o6IpcBase::Dhcp4o6IpcBase() : port_(0), socket_fd_(-1) {}
Dhcp4o6IpcBase::~Dhcp4o6IpcBase() {
close();
}
int Dhcp4o6IpcBase::open(uint16_t port, EndpointType endpoint_type) {
// Don't check if the value is greater than 65534 as it is done
// by callers before they cast the value to 16 bits.
if (port == port_) {
// No change: nothing to do
return (socket_fd_);
}
// Open socket
int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
isc_throw(Dhcp4o6IpcError, "Failed to create DHCP4o6 socket.");
}
// Set no blocking
if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
::close(sock);
isc_throw(Dhcp4o6IpcError,
"Failed to set O_NONBLOCK on DHCP4o6 socket.");
}
// Bind to the local address
struct sockaddr_in6 local6;
memset(&local6, 0, sizeof(local6));
local6.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
local6.sin6_len = sizeof(local6);
#endif
if (endpoint_type == ENDPOINT_TYPE_V6) {
local6.sin6_port = htons(port);
} else {
local6.sin6_port = htons(port + 1);
}
// We'll connect to the loopback address so bind to it too.
local6.sin6_addr.s6_addr[15] = 1;
if (::bind(sock, (struct sockaddr *)&local6, sizeof(local6)) < 0) {<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
::close(sock);
isc_throw(Dhcp4o6IpcError, "Failed to bind DHCP4o6 socket.");
}
// Connect to the remote address
struct sockaddr_in6 remote6;
memset(&remote6, 0, sizeof(remote6));
remote6.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
remote6.sin6_len = sizeof(remote6);
#endif
if (endpoint_type == ENDPOINT_TYPE_V6) {
remote6.sin6_port = htons(port + 1);
} else {
remote6.sin6_port = htons(port);
}
// At least OpenBSD requires the remote address to not be left
// unspecified, so we set it to the loopback address.
remote6.sin6_addr.s6_addr[15] = 1;
if (connect(sock, reinterpret_cast<const struct sockaddr*>(&remote6),
sizeof(remote6)) < 0) {
::close(sock);
isc_throw(Dhcp4o6IpcError, "Failed to connect DHCP4o6 socket.");
}
if (socket_fd_ != -1) {
if (dup2(sock, socket_fd_) == -1) {
::close(sock);
isc_throw(Dhcp4o6IpcError, "Failed to duplicate DHCP4o6 socket.");
}
if (sock != socket_fd_) {
::close(sock);
sock = socket_fd_;
}
}
// Success
port_ = port;
socket_fd_ = sock;
return (socket_fd_);
}
void Dhcp4o6IpcBase::close() {
port_ = 0;
if (socket_fd_ != -1) {
IfaceMgr::instance().deleteExternalSocket(socket_fd_);
::close(socket_fd_);
socket_fd_ = -1;
}
}
Pkt6Ptr Dhcp4o6IpcBase::receive() {
uint8_t buf[65536];
ssize_t cc = recv(socket_fd_, buf, sizeof(buf), 0);
if (cc < 0) {
isc_throw(Dhcp4o6IpcError, "Failed to receive on DHCP4o6 socket.");
}
Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(buf, cc));
pkt->updateTimestamp();
// Get interface name and remote address
pkt->unpack();
// Vendor option is initially NULL. If we find the instance of the vendor
// option with the ISC enterprise id this pointer will point to it.
OptionVendorPtr option_vendor;
// Get all vendor option and look for the one with the ISC enterprise id.
OptionCollection vendor_options = pkt->getOptions(D6O_VENDOR_OPTS);
for (auto const& opt : vendor_options) {
option_vendor = boost::dynamic_pointer_cast<OptionVendor>(opt.second);
if (option_vendor) {
if (option_vendor->getVendorId() == ENTERPRISE_ID_ISC) {
break;
}
option_vendor.reset();
}
}
// Vendor option must exist.
if (!option_vendor) {
LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
.arg("no ISC vendor option");
isc_throw(Dhcp4o6IpcError, "malformed packet (no ISC vendor option)");
}
// The option carrying interface name is required.
OptionStringPtr ifname = boost::dynamic_pointer_cast<
OptionString>(option_vendor->getOption(ISC_V6_4O6_INTERFACE));
if (!ifname) {
LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
.arg("no interface suboption");
isc_throw(Dhcp4o6IpcError,
"malformed packet (interface suboption missing "
"or has incorrect type)");
}
// Check if this interface is present in the system.
IfacePtr iface = IfaceMgr::instance().getIface(ifname->getValue());
if (!iface) {
LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
.arg("can't get interface " + ifname->getValue());
isc_throw(Dhcp4o6IpcError,
"malformed packet (unknown interface "
+ ifname->getValue() + ")");
}
// Get the option holding source IPv6 address.
OptionCustomPtr srcs = boost::dynamic_pointer_cast<
OptionCustom>(option_vendor->getOption(ISC_V6_4O6_SRC_ADDRESS));
if (!srcs) {
LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
.arg("no source address suboption");
isc_throw(Dhcp4o6IpcError,
"malformed packet (source address suboption missing "
"or has incorrect type)");
}
// Get the option holding source port.
OptionUint16Ptr sport = boost::dynamic_pointer_cast<
OptionUint16>(option_vendor->getOption(ISC_V6_4O6_SRC_PORT));
if (!sport) {
LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
.arg("no source port suboption");
isc_throw(Dhcp4o6IpcError,
"malformed packet (source port suboption missing "
"or has incorrect type)");
}
// Update the packet.
pkt->setRemoteAddr(srcs->readAddress());
pkt->setRemotePort(sport->getValue());
pkt->setIface(iface->getName());
pkt->setIndex(iface->getIndex());
// Remove options that have been added by the IPC sender.
static_cast<void>(option_vendor->delOption(ISC_V6_4O6_INTERFACE));
static_cast<void>(option_vendor->delOption(ISC_V6_4O6_SRC_ADDRESS));
static_cast<void>(option_vendor->delOption(ISC_V6_4O6_SRC_PORT));
// If there are no more options, the IPC sender has probably created the
// vendor option, in which case we should remove it here.
if (option_vendor->getOptions().empty()) {
static_cast<void>(pkt->delOption(D6O_VENDOR_OPTS));
}
return (pkt);
}
void Dhcp4o6IpcBase::send(const Pkt6Ptr& pkt) {
// This shouldn't happen, i.e. send() shouldn't be called if there is
// no message.
if (!pkt) {
isc_throw(Dhcp4o6IpcError, "DHCP4o6 message must not be NULL while"
" trying to send it over the IPC");
}
// Disabled: nowhere to send
if (socket_fd_ == -1) {
isc_throw(Dhcp4o6IpcError, "unable to send DHCP4o6 message because"
" IPC socket is closed");
}
// Check if vendor option exists.
// Vendor option is initially NULL. If we find the instance of the vendor
// option with the ISC enterprise id this pointer will point to it.
OptionVendorPtr option_vendor;
// Get all vendor option and look for the one with the ISC enterprise id.
OptionCollection vendor_options = pkt->getOptions(D6O_VENDOR_OPTS);
for (auto const& opt : vendor_options) {
option_vendor = boost::dynamic_pointer_cast<OptionVendor>(opt.second);
if (option_vendor) {
if (option_vendor->getVendorId() == ENTERPRISE_ID_ISC) {
break;
}
option_vendor.reset();
}
}
// If vendor option doesn't exist or its enterprise id is not ISC's
// enterprise id, let's create it.
if (!option_vendor) {
option_vendor.reset(new OptionVendor(Option::V6, ENTERPRISE_ID_ISC));
pkt->addOption(option_vendor);
}
// Push interface name and source address in it
option_vendor->addOption(OptionStringPtr(new OptionString(Option::V6,
ISC_V6_4O6_INTERFACE,
pkt->getIface())));
option_vendor->addOption(Option6AddrLstPtr(new Option6AddrLst(ISC_V6_4O6_SRC_ADDRESS,
pkt->getRemoteAddr())));
option_vendor->addOption(OptionUint16Ptr(new OptionUint16(Option::V6,
ISC_V6_4O6_SRC_PORT,
pkt->getRemotePort())));
// Get packet content
OutputBuffer& buf = pkt->getBuffer();
buf.clear();
pkt->pack();
// Try to send the message.
if (::send(socket_fd_, buf.getData(), buf.getLength(), 0) < 0) {
isc_throw(Dhcp4o6IpcError,
"failed to send DHCP4o6 message over the IPC: "
<< strerror(errno));
}
}
} // namespace dhcp
} // namespace isc
|