-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndhcpd_p.cc
427 lines (368 loc) · 14.3 KB
/
ndhcpd_p.cc
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "ndhcpd_p.hpp"
#include "config.h"
#include <netdb.h>
#include <net/if.h>
#include <sys/eventfd.h>
#include <poll.h>
#include <unistd.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>
#include "dhcp_error.hpp"
#include <syslog.h>
#include <arpa/inet.h>
#include <log4cpp/PropertyConfigurator.hh>
namespace std {
// std::size from C++17
template <class C>
constexpr auto size(const C& c) -> decltype(c.size())
{
return c.size();
}
template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}
}
static std::string mac_to_string(const uint8_t *mac)
{
std::ostringstream str;
for(int i=0; i<6; ++i) {
if(i != 0) {
str << ':';
}
str << std::hex << std::setfill('0') << std::setw(2) << (uint)mac[i];
}
return str.str();
}
ndhcpd_private::ndhcpd_private()
: stop_server(false)
, log(log4cpp::Category::getInstance("ndhcpd.lib"))
{
std::vector<std::string> logFileNames;
const char* envLogConfigFileName = getenv("LIBNDHCPD_LOG");
if(envLogConfigFileName) {
logFileNames.push_back(envLogConfigFileName);
}
for(auto logConfigFileName : logFileNames) {
try {
log4cpp::PropertyConfigurator::configure(logConfigFileName);
log4cpp::Category::getRoot().debugStream() << "LIBNDHCPD loaded log properties from " << logConfigFileName;
break;
}
catch(const log4cpp::ConfigureFailure &ex) {
log4cpp::Category::getRoot().debugStream() << logConfigFileName << " error: " << ex.what();
}
}
struct servent *port;
port = getservbyname("bootps", "udp");
srcAddr.sin_family = AF_INET;
srcAddr.sin_port = port->s_port;
srcAddr.sin_addr.s_addr = INADDR_ANY;
port = getservbyname("bootpc", "udp");
dstAddr.sin_family = AF_INET;
dstAddr.sin_port = port->s_port;
endservent();
server_id.s_addr = INADDR_NONE;
}
ndhcpd_private::~ndhcpd_private()
{
stop(true);
}
bool ndhcpd_private::lease_is_mac_equal::operator ()(const leases_t::value_type &lease)
{
if(!lease.second)
return false;
return memcmp(lease.second->mac.data(), mac, lease.second->mac.size()) == 0;
}
bool ndhcpd_private::lease_is_overdue::operator ()(const leases_t::value_type &lease)
{
if(!lease.second) {
// not allocated lease is ok
return true;
}
return (lease.second->lease_start + lease.second->lease_time < now);
}
void ndhcpd_private::get_server_id(const Socket &_server)
{
struct ifreq ifr;
ifaceName.copy(ifr.ifr_name, std::size(ifr.ifr_name));
if(ioctl(_server, SIOCGIFADDR, &ifr) == 0) {
server_id = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
log.infoStream() << "Bind DHCP server to " << inet_ntoa(server_id);
}
else {
server_id.s_addr = INADDR_NONE;
log.info("Starting DHCP without server IP");
}
}
void ndhcpd_private::start()
{
try {
if(serverThread.joinable()) {
log.notice("Server thread already started");
return;
}
stop_server = false;
Socket _server(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
_server.setsockopt(SOL_SOCKET, SO_REUSEADDR, true);
_server.setsockopt(SOL_SOCKET, SO_BROADCAST, true);
if(!ifaceName.empty()) {
log.infoStream() << "Starting bound to interface " << ifaceName;
struct ifreq ifr;
memset(ifr.ifr_name, 0, std::size(ifr.ifr_name));
ifaceName.copy(ifr.ifr_name, std::size(ifr.ifr_name));
_server.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, ifr);
get_server_id(_server);
}
else {
log.infoStream() << "Starting unbound";
}
sockaddr_in addr = srcAddr;
_server.bind(addr);
File _event(eventfd(0,0));
std::swap(server, _server);
std::swap(event, _event);
serverThread = std::thread(std::mem_fn(&ndhcpd_private::process_dhcp), this);
log.notice("Service started");
}
catch(const std::system_error &err) {
log.error(err.what());
throw;
}
}
void ndhcpd_private::stop(bool silent)
{
if(!silent) {
log.info("Stoping service");
}
stop_server = true;
eventfd_write(event, 1);
if(serverThread.joinable()) {
serverThread.join();
log.notice("Service stoped");
}
else {
if(!silent) {
log.info("Service already stoped");
}
}
leases.clear();
server.close();
event.close();
}
void ndhcpd_private::process_dhcp()
{
try {
std::vector<struct pollfd> pollFds = {
{ event, POLLIN|POLLERR },
{ server, POLLIN|POLLERR }
};
while(!stop_server) {
auto ret = poll(pollFds.data(), pollFds.size(), -1);
if(ret < 0) {
throw std::system_error(errno, std::system_category(), "poll()");
}
if(pollFds[0].revents != 0) {
// event handle has been raised
eventfd_t val;
eventfd_read(event, &val);
// check stop_server variable
continue;
}
std::for_each(pollFds.cbegin()+1, pollFds.cend(), [this](const pollfd& fd){
if(fd.revents & (POLLERR|POLLHUP|POLLNVAL)) {
log.errorStream() << "Socket " << fd.fd << " in error state";
}
else if(fd.revents & POLLIN) {
try {
if(server_id.s_addr == INADDR_NONE) {
get_server_id(server);
char server_id_str[256];
log.infoStream() << "Got server_id: " << inet_ntop(AF_INET, &server_id, server_id_str, sizeof(server_id_str));
}
struct dhcp_packet in_packet;
in_packet = recieve_packet(fd.fd);
struct dhcp_packet out_packet = process_packet(in_packet);
send_packet(fd.fd, out_packet);
}
catch(const std::system_error &err) {
log.error(err.what());
}
}
});
}
}
catch(const std::exception &err) {
log.error(err.what());
}
}
dhcp_packet ndhcpd_private::recieve_packet(int fd)
{
dhcp_packet packet;
ssize_t len = read(fd, &packet, sizeof(packet));
if(len < 0) {
throw std::system_error(errno, std::system_category(), "read()");
}
if(len < (ssize_t)offsetof(dhcp_packet, options) ||
packet.cookie != htonl(dhcp_packet::cookie_value_he)) {
throw std::system_error(make_error_code(dhcp_error::invalid_packet), "recieve_packet()");
}
if(packet.htype != ARPHRD_ETHER ||
packet.hlen != 6) {
throw std::system_error(make_error_code(dhcp_error::invalid_hwtype), "recieve_packet()");
}
if(packet.op != dhcp_packet::_op::BOOTREQUEST) {
throw std::system_error(make_error_code(dhcp_error::unexpected_packet_type), "recieve_packet()");
}
return packet;
}
dhcp_packet ndhcpd_private::process_packet(const dhcp_packet &packet)
{
const dhcp_message_type *msgType = static_cast<const dhcp_message_type *>(dhcp_get_option(packet, dhcp_option::_code::message_type));
if(!msgType) {
throw std::system_error(make_error_code(dhcp_error::invalid_packet), "process_packet()");
}
if(*msgType < dhcp_message_type::minval
|| *msgType > dhcp_message_type::maxval) {
throw std::system_error(make_error_code(dhcp_error::invalid_packet), "process_packet()");
}
log.infoStream() << "Recieved " << dhcp_message_type_name(*msgType) << " from " << mac_to_string(packet.chaddr);
switch(*msgType) {
case dhcp_message_type::discover:
return make_offer(packet);
case dhcp_message_type::request:
return process_ip_request(packet);
default:
throw std::system_error(make_error_code(dhcp_error::unexpected_packet_type), "process_packet()");
}
}
void ndhcpd_private::send_packet(int fd, const dhcp_packet &packet)
{
uint32_t ciaddr;
if((packet.flags & htons(BROADCAST_FLAG))
|| packet.ciaddr == 0
) {
ciaddr = INADDR_BROADCAST;
}
else {
ciaddr = packet.ciaddr;
}
struct sockaddr_in addr = dstAddr;
addr.sin_addr.s_addr = ciaddr;
ssize_t ret = sendto(fd, &packet, sizeof(packet), 0, (const sockaddr*)&addr, sizeof(addr));
if(ret < 0)
throw std::system_error(errno, std::system_category(), "sendto()");
log.infoStream() << "Sent " << dhcp_message_type_name(*static_cast<const dhcp_message_type *>(dhcp_get_option(packet, dhcp_option::_code::message_type))) << " to " << mac_to_string(packet.chaddr);
}
dhcp_packet ndhcpd_private::make_offer(const dhcp_packet &packet)
{
dhcp_packet out_packet;
memset(&out_packet, 0, sizeof(out_packet));
out_packet.op = dhcp_packet::_op::BOOTREPLY;
out_packet.htype = ARPHRD_ETHER;
out_packet.hlen = 6;
out_packet.xid = packet.xid;
memcpy(out_packet.chaddr, packet.chaddr, sizeof(out_packet.chaddr));
out_packet.flags = packet.flags;
out_packet.ciaddr = packet.ciaddr;
out_packet.cookie = (dhcp_packet::_cookie)htonl(dhcp_packet::cookie_value_he);
out_packet.options[0] = (uint8_t)dhcp_option::_code::end;
dhcp_add_option(&out_packet, dhcp_option::_code::message_type, dhcp_message_type::offer);
dhcp_add_option(&out_packet, dhcp_option::_code::server_id, server_id);
// Find lease with same MAC-address
leases_t::iterator leaseIter = std::find_if(leases.begin(), leases.end(), lease_is_mac_equal(packet.chaddr));
if(leaseIter == leases.end()) {
leaseIter = std::find_if(leases.begin(), leases.end(), lease_is_overdue(std::chrono::steady_clock::now()));
}
if(leaseIter == leases.end()) {
throw std::system_error(make_error_code(dhcp_error::no_more_leases), "make_offer()");
}
leaseIter->second.reset(new lease_data(packet.chaddr, std::chrono::seconds(60))); // Set lease for offer time (60 sec)
out_packet.yiaddr = htonl(leaseIter->first.ip);
dhcp_add_option(&out_packet, dhcp_option::_code::lease_time, htonl(leaseIter->second->lease_time.count()));
dhcp_add_option(&out_packet, dhcp_option::_code::subnet_mask, htonl(leaseIter->first.subnet));
in_addr addr = {out_packet.yiaddr};
log.infoStream() << "Make offer for " << inet_ntoa(addr) << " to " << mac_to_string(out_packet.chaddr);
return out_packet;
}
dhcp_packet ndhcpd_private::process_ip_request(const dhcp_packet &packet)
{
const uint32_t *server_id_opt = (uint32_t *)dhcp_get_option(packet, dhcp_option::_code::server_id);
const uint32_t *requested_ip_opt = (uint32_t *)dhcp_get_option(packet, dhcp_option::_code::requested_ip);
uint32_t requested_ip;
if(requested_ip_opt) {
requested_ip = ntohl(*requested_ip_opt);
}
else {
requested_ip = ntohl(packet.ciaddr);
if(requested_ip == 0) {
throw std::system_error(make_error_code(dhcp_error::no_ip_requested), "process_ip_request()");
}
}
leases_t::iterator leaseIter = std::find_if(leases.begin(), leases.end(), lease_is_mac_equal(packet.chaddr));
if(leaseIter != leases.end() && leaseIter->first.ip == requested_ip) {
// client requested or configured IP matches the lease.
// ACK it, and bump lease expiration time.
in_addr addr = {htonl(requested_ip)};
log.infoStream() << "Acknowledge request for " << inet_ntoa(addr) << " to " << mac_to_string(packet.chaddr);
return ack_packet(packet, &(*leaseIter));
}
// No lease for this MAC, or lease IP != requested IP
if(server_id_opt // client is in SELECTING state
|| requested_ip_opt // client is in INIT-REBOOT state
) {
// "No, we don't have this IP for you"
log.infoStream() << "Not acknowledge request to " << mac_to_string(packet.chaddr);
return nak_packet(packet);
}
// client is in RENEWING or REBINDING, with wrong IP address.
// do not answer
throw std::system_error(make_error_code(dhcp_error::invalid_packet), "process_ip_request()");
}
dhcp_packet ndhcpd_private::ack_packet(const dhcp_packet &packet, leases_t::value_type *lease)
{
dhcp_packet out_packet;
memset(&out_packet, 0, sizeof(out_packet));
out_packet.op = dhcp_packet::_op::BOOTREPLY;
out_packet.htype = ARPHRD_ETHER;
out_packet.hlen = 6;
out_packet.xid = packet.xid;
memcpy(out_packet.chaddr, packet.chaddr, sizeof(out_packet.chaddr));
out_packet.flags = packet.flags;
out_packet.ciaddr = packet.ciaddr;
out_packet.cookie = (dhcp_packet::_cookie)htonl(dhcp_packet::cookie_value_he);
out_packet.options[0] = (uint8_t)dhcp_option::_code::end;
dhcp_add_option(&out_packet, dhcp_option::_code::message_type, dhcp_message_type::ack);
dhcp_add_option(&out_packet, dhcp_option::_code::server_id, server_id);
lease->second.reset(new lease_data(packet.chaddr, std::chrono::hours(1))); // Set lease for lease time (1hour)
out_packet.yiaddr = htonl(lease->first.ip);
dhcp_add_option(&out_packet, dhcp_option::_code::lease_time, htonl(lease->second->lease_time.count()));
dhcp_add_option(&out_packet, dhcp_option::_code::subnet_mask, htonl(lease->first.subnet));
return out_packet;
}
dhcp_packet ndhcpd_private::nak_packet(const dhcp_packet &packet)
{
dhcp_packet out_packet;
memset(&out_packet, 0, sizeof(out_packet));
out_packet.op = dhcp_packet::_op::BOOTREPLY;
out_packet.htype = ARPHRD_ETHER;
out_packet.hlen = 6;
out_packet.xid = packet.xid;
memcpy(out_packet.chaddr, packet.chaddr, sizeof(out_packet.chaddr));
out_packet.flags = packet.flags;
out_packet.ciaddr = packet.ciaddr;
out_packet.cookie = (dhcp_packet::_cookie)htonl(dhcp_packet::cookie_value_he);
out_packet.options[0] = (uint8_t)dhcp_option::_code::end;
dhcp_add_option(&out_packet, dhcp_option::_code::message_type, dhcp_message_type::nak);
dhcp_add_option(&out_packet, dhcp_option::_code::server_id, server_id);
return out_packet;
}