forked from sargon/ddhcpd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
596 lines (491 loc) · 17.1 KB
/
main.c
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <arpa/inet.h>
#include <assert.h>
#include <getopt.h>
#include <math.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <limits.h>
#include "block.h"
#include "control.h"
#include "ddhcp.h"
#include "dhcp.h"
#include "dhcp_options.h"
#include "dhcp_packet.h"
#include "epoll.h"
#include "hook.h"
#include "logger.h"
#include "netlink.h"
#include "netsock.h"
#include "packet.h"
#include "statistics.h"
#include "tools.h"
#include "version.h"
static volatile sig_atomic_t daemon_running = 0;
extern int log_level;
const int NET = 0;
const int NET_LEN = 10;
uint8_t* buffer = NULL;
ATTR_NONNULL_ALL in_addr_storage get_in_addr(struct sockaddr* sa)
{
struct sockaddr_in in;
struct sockaddr_in6 in6;
in_addr_storage in_store = { 0 };
if (sa->sa_family == AF_INET) {
memcpy(&in, sa, sizeof(in));
in_store.in_addr = in.sin_addr;
}
if (sa->sa_family == AF_INET6) {
memcpy(&in6, sa, sizeof(in6));
in_store.in6_addr = in6.sin6_addr;
}
return in_store;
}
/**
* House Keeping
*
* - Free timed-out DHCP leases.
* - Refresh timed-out blocks.
* + Claim new blocks if we are low on spare leases.
* + Update our claims.
*/
ATTR_NONNULL_ALL void house_keeping(ddhcp_config* config) {
DEBUG("house_keeping(blocks,config)\n");
block_check_timeouts(config);
uint32_t spare_leases = block_num_free_leases(config);
int32_t leases_needed = (int32_t)config->spare_leases_needed - (int32_t)spare_leases;
int32_t blocks_needed = leases_needed / config->block_size;
if (leases_needed % config->block_size > 0) {
blocks_needed++;
}
if (blocks_needed < 0 ) {
block_drop_unused(config);
} else {
if (config->needless_marks != 0) {
block_unmark_needless(config);
}
if (blocks_needed > 0) {
block_claim(blocks_needed, config);
}
}
block_update_claims(config);
dhcp_packet_list_timeout(&config->dhcp_packet_cache);
DEBUG("house_keeping(...) finish\n\n");
}
ATTR_NONNULL_ALL uint32_t get_loop_timeout(ddhcp_config* config) {
//Multiply by 500 to convert the timeout value given in seconds
//into milliseconds AND dividing the value by two at the same time.
//The integer overflow occurring for timeouts greater than 99.4 days is ignored here.
return config->tentative_timeout * 500u;
}
typedef void (*sighandler_t)(int);
static sighandler_t
handle_signal(int sig_nr, sighandler_t signalhandler) {
struct sigaction new_sig, old_sig;
new_sig.sa_handler = signalhandler;
sigemptyset(&new_sig.sa_mask);
new_sig.sa_flags = SA_RESTART;
if (sigaction(sig_nr, &new_sig, &old_sig) < 0) {
return SIG_ERR;
}
return old_sig.sa_handler;
}
void handle_signal_terminate(int sig_nr) {
if (SIGINT == sig_nr) {
daemon_running = 0;
} else if (SIGTERM == sig_nr) {
daemon_running = 0;
}
}
ATTR_NONNULL_ALL int hdl_ddhcp_dhcp(epoll_data_t data, ddhcp_config* config) {
int fd = epoll_get_fd(data);
ssize_t len;
struct sockaddr_in6 sender;
socklen_t sender_len = sizeof sender;
while ((len = recvfrom(fd, buffer, 1500, 0, (struct sockaddr*) &sender, &sender_len)) > 0) {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
in_addr_storage in_addr;
char ipv6_sender[INET6_ADDRSTRLEN];
in_addr = get_in_addr((struct sockaddr*)&sender);
DEBUG("Receive message from %s\n", inet_ntop(AF_INET6, &in_addr, ipv6_sender, INET6_ADDRSTRLEN));
#endif
statistics_record(config, STAT_DIRECT_RECV_BYTE, (long int)len);
statistics_record(config, STAT_DIRECT_RECV_PKG, 1);
ddhcp_dhcp_process(buffer, len, sender, config);
}
return 0;
}
ATTR_NONNULL_ALL int hdl_ddhcp_block(epoll_data_t data, ddhcp_config* config) {
int fd = epoll_get_fd(data);
ssize_t len;
struct sockaddr_in6 sender;
socklen_t sender_len = sizeof sender;
while ((len = recvfrom(fd, buffer, 1500, 0, (struct sockaddr*) &sender, &sender_len)) > 0) {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
in_addr_storage in_addr;
char ipv6_sender[INET6_ADDRSTRLEN];
in_addr = get_in_addr((struct sockaddr*)&sender);
DEBUG("Receive message from %s\n", inet_ntop(AF_INET6, &in_addr, ipv6_sender, INET6_ADDRSTRLEN));
#endif
statistics_record(config, STAT_MCAST_RECV_BYTE, (long int)len);
statistics_record(config, STAT_MCAST_RECV_PKG, 1);
ddhcp_block_process(buffer, len, sender, config);
}
return 1;
}
ATTR_NONNULL_ALL int hdl_dhcp(epoll_data_t data, ddhcp_config* config) {
int fd = epoll_get_fd(data);
ssize_t len;
int need_house_keeping = 0;
while ((len = read(fd, buffer, 1500)) > 0) {
statistics_record(config, STAT_DHCP_RECV_BYTE, (long int)len);
statistics_record(config, STAT_DHCP_RECV_PKG, 1);
need_house_keeping |= dhcp_process(buffer, len, config);
}
return need_house_keeping;
}
ATTR_NONNULL_ALL int hdl_ctrl_cmd(epoll_data_t data, ddhcp_config* config) {
int fd = epoll_get_fd(data);
ssize_t len;
// Handle commands coming over a control_socket
len = read(fd, buffer, 1500);
if (handle_command(fd, buffer, len, config) < 0) {
ERROR("Malformed command on control socket.\n");
}
del_fd(config->epoll_fd, fd);
close(fd);
return 0;
}
ATTR_NONNULL_ALL int hdl_ctrl_new(epoll_data_t data, ddhcp_config* config) {
UNUSED(config);
int fd = epoll_get_fd(data);
// Handle new control socket connections
struct sockaddr_un client_fd;
unsigned int len = sizeof(client_fd);
ddhcp_epoll_data* control_link = epoll_data_new(config->control_path, NULL, hdl_ctrl_cmd, NULL);
control_link->fd = accept(fd, (struct sockaddr*) &client_fd, &len);
//set_nonblocking(config.client_control_socket);
epoll_add_fd(config->epoll_fd, control_link, EPOLLIN | EPOLLET, config);
DEBUG("ControlSocket: new connections\n");
return 0;
}
int main(int argc, char** argv) {
srand((unsigned int)time(NULL));
ddhcp_config config;
config.block_size = 32;
config.claiming_blocks_amount = 0;
inet_aton("10.0.0.0", &config.prefix);
config.prefix_len = 24;
config.spare_leases_needed = 2;
config.block_timeout = 60;
config.block_refresh_factor = 4;
config.block_needless_timeout = 300;
config.tentative_timeout = 15;
config.control_path = (char*)"/tmp/ddhcpd_ctl";
config.disable_dhcp = 0;
config.hook_command = NULL;
#ifdef DDHCPD_STATISTICS
memset(config.statistics, 0, sizeof(long int) * STAT_NUM_OF_FIELDS);
#endif
// DHCP
config.dhcp_port = 67;
INIT_LIST_HEAD(&config.options);
INIT_LIST_HEAD(&config.claiming_blocks);
INIT_LIST_HEAD(&config.dhcp_packet_cache);
char* interface = (char*)"server0";
char* interface_client = (char*)"client0";
daemon_running = 2;
int c;
int show_usage = 0;
int learning_phase = 1;
while ((c = getopt(argc, argv, "C:c:i:St:dvVDhLb:B:N:o:s:H:n:")) != -1) {
switch (c) {
case 'i':
if (strlen(optarg) > IFNAMSIZ) {
ERROR("Server interface name too long\n");
exit(1);
}
interface = strdup(optarg);
break;
case 'c':
if (strlen(optarg) > IFNAMSIZ) {
ERROR("Client interface name too long\n");
exit(1);
}
interface_client = strdup(optarg);
break;
case 'b':
config.block_size = (uint8_t)(1 << atoi(optarg));
break;
case 'B':
{
unsigned long block_timeout = strtoul(optarg, NULL, 0);
if(!block_timeout) {
ERROR("Block timeout must be > 0\n");
exit(1);
}
if(block_timeout == ULONG_MAX && errno) {
ERROR("Failed to parse block timeout: %s(%d)\n", strerror(errno), errno);
exit(1);
}
config.block_timeout = (uint16_t)block_timeout;
}
break;
case 't':
config.tentative_timeout = (uint16_t)atoi(optarg);
if (config.tentative_timeout < 2) {
ERROR("Tentative timeout must be at least two\n");
exit(1);
}
break;
case 'd':
daemon_running = 1;
break;
case 'D':
//We pretend we are normally running, just in another mode
daemon_running = 2;
break;
case 'h':
show_usage = 1;
break;
case 'L':
learning_phase = 0;
break;
case 'V':
printf("Revision: %s\n", REVISION);
return 0;
case 'N':
do {
// TODO Split prefix and cidr
size_t optlen = strlen(optarg);
char* cidr = strchr(optarg, '/');
if (!cidr) {
ERROR("Malformed network '%s'\n", optarg);
exit(1);
}
if (cidr == optarg + optlen - 1) {
ERROR("Malformed network '%s'\n", optarg);
exit(1);
}
cidr[0] = '\0';
cidr++;
inet_aton(optarg, &config.prefix);
config.prefix_len = (uint8_t)atoi(cidr);
if (config.prefix_len < 8) {
ERROR("Are you the internet? CIDR less than 8 seems strange.\n");
exit(1);
}
} while (0);
break;
case 'S':
config.disable_dhcp = 1;
break;
case 'o':
do {
dhcp_option* option = parse_option();
set_option_in_store(&config.options, option);
} while (0);
break;
case 's':
config.spare_leases_needed = (uint8_t)atoi(optarg);
#if LOG_LEVEL_LIMIT >= LOG_WARNING
if (config.spare_leases_needed == 0) {
WARNING("This daemon will only serve roamed clients with a spare limit of zero.");
}
#endif
break;
case 'C':
config.control_path = strdup(optarg);
break;
case 'H':
config.hook_command = strdup(optarg);
break;
case 'v':
if (log_level < LOG_LEVEL_MAX) {
log_level++;
}
break;
case 'n':
config.block_needless_timeout = (uint16_t)(atoi(optarg));
break;
default:
printf("ARGC: %i\n", argc);
show_usage = 1;
break;
}
}
if (show_usage) {
printf("Usage: %s [-h] [-d|-D] [-L] [-c CLT-IFACE|-S] [-i SRV-IFACE] [-t TENTATIVE-TIMEOUT] [-B BLOCK-TIMEOUT]\n", argv[0]);
printf("\n");
printf("-h This usage information\n");
printf("-c CLT-IFACE Interface on which requests from clients are handled\n");
printf("-i SRV-IFACE Interface on which different servers communicate\n");
printf("-S no Client interface\n");
printf("-t TENTATIVE Time required for a block to be claimed\n");
printf("-N NETWORK/CIDR Network to announce and manage blocks in\n");
printf("-o CODE:LEN:P1. .. .Pn DHCP Option with code,len and #len chars in decimal\n");
printf("-b BLKSIZEPOW Power over two of block size\n");
printf("-B TIMEOUT Block claim timeout\n");
printf("-n NEEDLESS_TIMEOUT Time until we release needless blocks\n");
printf("-s SPARELEASES Amount of spare leases (max: 256)\n");
printf("-L Deactivate learning phase\n");
printf("-d Run in background and daemonize\n");
printf("-D Run in foreground and log to console (default)\n");
printf("-C CTRL_PATH Path to control socket\n");
printf("-H COMMAND Hook to call on events\n");
printf("-V Print build revision\n");
printf("-v Increase verbosity, can be specified multiple times\n");
exit(0);
}
if (log_level > LOG_LEVEL_LIMIT) {
LOG("WARNING: Requested verbosity is higher than maximum supported by this build\n");
}
config.number_of_blocks = (uint32_t)pow(2u, (32u - config.prefix_len - ceil(log2(config.block_size))));
if (config.disable_dhcp) {
config.spare_leases_needed = 0;
}
INFO("CONFIG: network=%s/%i\n", inet_ntoa(config.prefix), config.prefix_len);
INFO("CONFIG: block_size=%i\n", config.block_size);
INFO("CONFIG: #blocks=%i\n", config.number_of_blocks);
INFO("CONFIG: #spare_leases=%i\n", config.spare_leases_needed);
INFO("CONFIG: timeout=%i\n", config.block_timeout);
INFO("CONFIG: refresh_factor=%i\n", config.block_refresh_factor);
INFO("CONFIG: tentative_timeout=%i\n", config.tentative_timeout);
INFO("CONFIG: client_interface=%s\n", interface_client);
INFO("CONFIG: group_interface=%s\n", interface);
//Register signal handlers
handle_signal(SIGHUP, SIG_IGN);
handle_signal(SIGPIPE, SIG_IGN);
handle_signal(SIGINT, handle_signal_terminate);
handle_signal(SIGTERM, handle_signal_terminate);
//Daemonize if requested
if (1 == daemon_running) {
if (daemon(0, 0)) {
perror("ddhcp");
exit(1);
}
//Conflicts with existing logging
//openlog("ddhcp", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_DAEMON);
}
// init block structure
ddhcp_block_init(&config);
if (dhcp_options_init(&config)) {
FATAL("Failed to allocate memory for option store\n");
abort();
}
hook_init();
// --------------------------------------------------------------------------
// Initializing Network Buffer, EPOLL and Sockets
// Here all later event loop handling is initialized
// --------------------------------------------------------------------------
buffer = calloc(1500, 1);
if (!buffer) {
FATAL("Failed to allocate network buffer\n");
abort();
}
size_t maxevents = 64;
struct epoll_event* events;
epoll_init(&config);
config.sockets[SKT_MCAST] = epoll_data_new(interface, netsock_multicast_init, hdl_ddhcp_block, NULL);
config.sockets[SKT_SERVER] = epoll_data_new(interface, netsock_server_init, hdl_ddhcp_dhcp, NULL);
config.sockets[SKT_CONTROL] = epoll_data_new(config.control_path, netsock_control_init, hdl_ctrl_new, NULL);
ddhcp_epoll_data* netlink = epoll_data_new(NULL, netlink_init, netlink_in, netlink_close);
// Trigger socket initializing and register to EPOLL
epoll_add_fd(config.epoll_fd, config.sockets[SKT_MCAST], EPOLLIN | EPOLLET,&config);
epoll_add_fd(config.epoll_fd, config.sockets[SKT_SERVER], EPOLLIN | EPOLLET,&config);
epoll_add_fd(config.epoll_fd, config.sockets[SKT_CONTROL], EPOLLIN | EPOLLET,&config);
epoll_add_fd(config.epoll_fd, netlink, EPOLLIN | EPOLLET,&config);
if (config.disable_dhcp == 0) {
config.sockets[SKT_DHCP] = epoll_data_new(interface_client, netsock_dhcp_init, hdl_dhcp, NULL);
epoll_add_fd(config.epoll_fd, config.sockets[SKT_DHCP], EPOLLIN | EPOLLET,&config);
}
// Event buffer
events = calloc(maxevents, sizeof(struct epoll_event));
if (!events) {
FATAL("Failed to allocate event buffer\n");
abort();
}
// --------------------------------------------------------------------------
// Main event loop && House keeping handler
// --------------------------------------------------------------------------
int need_house_keeping = 0;
uint32_t loop_timeout = config.loop_timeout = get_loop_timeout(&config);
time_t now = time(NULL);
// The first time we want to make housekeeping is after the learning phase,
// which is block_timeout long.
time_t timeout_time = now + config.block_timeout;
if (!learning_phase) {
// We want no learning phase, so reset all the timers
loop_timeout = 0;
timeout_time = now;
hook(HOOK_LEARNING_PHASE_END,&config);
}
INFO("loop timeout: %i msecs\n", get_loop_timeout(&config));
do {
int n = 0;
do {
n = epoll_wait(config.epoll_fd, events, (int)maxevents, (int)loop_timeout);
} while (n < 0 && errno == EINTR);
if (n < 0) {
ERROR("epoll error (%i) %s",errno,strerror(errno));
}
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
if (loop_timeout != config.loop_timeout) {
DEBUG("Increase loop timeout from %i to %i\n", loop_timeout, config.loop_timeout);
}
#endif
loop_timeout = config.loop_timeout;
now = time(NULL);
if (timeout_time <= now) {
// Our time for house keeping has come
need_house_keeping = 1;
// The next time for house keeping is in half the tentative timeout.
timeout_time = now + (config.tentative_timeout >> 1);
if (learning_phase) {
learning_phase = 0;
hook(HOOK_LEARNING_PHASE_END,&config);
}
} else {
need_house_keeping = 0;
}
for (int i = 0; i < n; i++) {
ddhcp_epoll_data* data = (ddhcp_epoll_data*) events[i].data.ptr;
if ((events[i].events & EPOLLERR)) {
ERROR("Error in epoll: %i \n", errno);
exit(1);
} else if (events[i].events & EPOLLIN) {
ddhcpd_epoll_event_t fct = data->epollin;
need_house_keeping |= fct(events[i].data,&config);
} else if (events[i].events & EPOLLHUP) {
ddhcpd_epoll_event_t fct = data->epollhup;
need_house_keeping |= fct(events[i].data,&config);
}
}
if (need_house_keeping) {
if (!learning_phase) {
house_keeping(&config);
}
}
} while (daemon_running);
// --------------------------------------------------------------------------
// Shutdown process, cleaning up stuff
// --------------------------------------------------------------------------
// TODO free dhcp_leases
free(events);
free(buffer);
ddhcp_block_free(&config);
free_option_store(&config.options);
dhcp_packet_list_free(&config.dhcp_packet_cache);
// TODO Handle shutdown of sockets
//close(config.mcast_socket);
//close(config.client_socket);
//close(config.control_socket);
epoll_data_call(netlink,epollhup,(&config));
remove(config.control_path);
return 0;
}