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
Dhcp4o6IpcBase()33 Dhcp4o6IpcBase::Dhcp4o6IpcBase() : port_(0), socket_fd_(-1) {}
34
~Dhcp4o6IpcBase()35 Dhcp4o6IpcBase::~Dhcp4o6IpcBase() {
36 close();
37 }
38
open(uint16_t port,EndpointType endpoint_type)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);
57 isc_throw(Dhcp4o6IpcError,
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
close()118 void Dhcp4o6IpcBase::close() {
119 port_ = 0;
120 if (socket_fd_ != -1) {
121 IfaceMgr::instance().deleteExternalSocket(socket_fd_);
122 ::close(socket_fd_);
123 socket_fd_ = -1;
124 }
125 }
126
receive()127 Pkt6Ptr Dhcp4o6IpcBase::receive() {
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) {
158 LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
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) {
167 LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
168 .arg("no interface suboption");
169 isc_throw(Dhcp4o6IpcError,
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) {
177 LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
178 .arg("can't get interface " + ifname->getValue());
179 isc_throw(Dhcp4o6IpcError,
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) {
188 LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
189 .arg("no source address suboption");
190 isc_throw(Dhcp4o6IpcError,
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) {
199 LOG_WARN(dhcpsrv_logger, DHCPSRV_DHCP4O6_RECEIVED_BAD_PACKET)
200 .arg("no source port suboption");
201 isc_throw(Dhcp4o6IpcError,
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
send(const Pkt6Ptr & pkt)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) {
270 isc_throw(Dhcp4o6IpcError,
271 "failed to send DHCP4o6 message over the IPC: "
272 << strerror(errno));
273 }
274 }
275
276 }; // namespace dhcp
277
278 }; // namespace isc
279