forked from fritz0705/dhcpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpd.c
449 lines (356 loc) · 9.71 KB
/
dhcpd.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
/* (c) 2013 Fritz Conrad Grimpen */
#define DHCP_DHCPD
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <unistd.h>
#ifdef __linux__
#include <cap-ng.h>
#endif
#include <pwd.h>
#include <grp.h>
#include <ev.h>
#include "array.h"
#include "dhcp.h"
#include "argv.h"
#include "error.h"
#include "config.h"
#include "iplist.h"
#include "packet.h"
#include "pool.h"
#ifndef RECV_BUF_LEN
#define RECV_BUF_LEN 4096
#endif
#ifndef SEND_BUF_LEN
#define SEND_BUF_LEN 4096
#endif
#define VERSION "0.1"
uint8_t recv_buffer[RECV_BUF_LEN];
uint8_t send_buffer[SEND_BUF_LEN];
struct config cfg = CONFIG_EMPTY;
struct pool *pool;
bool debug = false;
static const char BROKEN_SOFTWARE_NOTIFICATION[] =
"#################################### ALERT ####################################\n"
" BROKEN SOFTWARE NOTIFICATION - SOMETHING SENDS INVALID DHCP MESSAGES IN YOUR\n"
" NETWORK\n";
static const char USAGE[] =
"%s [-h[elp]] [-v[ersion]] [-d[ebug]] [-user UID] [-group GID]\n"
"\t[-interface IF] [-db FILE]\n"
"\t[-new] [-allocate] [-iprange IP IP] [-router IP]... [-nameserver IP]...\n";
/**
* Handle DHCPDISCOVER request and reply to that
*/
static void discover_cb(EV_P_ ev_io *w, struct dhcp_msg *msg)
{
(void)EV_A;
/* XXX: Take address from pool
* If it is empty, ask for new address
* If not, send offer
* and publish lease
* and rebalance pool if below threshhold?
*/
struct dhcp_lease lease = DHCP_LEASE_EMPTY;
struct pool_entry *entry;
entry = pool_get(pool);
/* If our pool is empty we'll ask the network to offer an address to us.
* In the meantime, we won't respond to the client.
*/
if (entry == NULL) {
// XXX: Ask for address
return;
}
lease = (struct dhcp_lease){
.routers = cfg.routers,
.routers_cnt = cfg.routers_cnt,
.nameservers = cfg.nameservers,
.nameservers_cnt = cfg.nameservers_cnt,
.leasetime = cfg.leasetime,
.prefixlen = cfg.prefixlen,
.address = entry->address
};
free(entry);
send_offer(w->fd, msg, &lease);
// XXX: Send (lease.address, msg->chaddr, lease.leasetime) to DHT
}
/**
* Handle to DHCPREQUEST request and reply to that, and allocate lease if
* enabled
*/
static void request_cb(EV_P_ ev_io *w, struct dhcp_msg *msg)
{
(void)EV_A;
/* XXX: Fetch lease with callback
*
* struct:
* - request_id
* - dhcp_msg
* - callback_ptr
* - timeout
*
* timeout -> NAK
*
* Callback: Check whether lease matches client
* If yes, ACK
* If no, NAK
*/
struct in_addr *requested_server;
struct in_addr *requested_addr;
uint8_t *options;
struct dhcp_opt current_opt;
requested_addr = NULL;
requested_server = (struct in_addr *)DHCP_MSG_F_SIADDR(msg->data);
options = DHCP_MSG_F_OPTIONS(msg->data);
while (dhcp_opt_next(&options, ¤t_opt, msg->end))
switch (current_opt.code)
{
case DHCP_OPT_REQIPADDR:
requested_addr = (struct in_addr *)current_opt.data;
break;
case DHCP_OPT_SERVERID:
requested_server = (struct in_addr *)current_opt.data;
break;
}
if (requested_server->s_addr != msg->sid->sin_addr.s_addr)
return;
struct dhcp_lease lease = DHCP_LEASE_EMPTY;
lease.address = *requested_addr;
if (0) {
// NACK
send_nak(w->fd, msg);
} else {
// ACK
send_ack(w->fd, msg, &lease);
}
}
/**
* Handle DHCPRELEASE request and release the lease if it was allocated
*/
static void release_cb(EV_P_ ev_io *w, struct dhcp_msg *msg)
{
(void)EV_A;
(void)w;
(void)msg;
/* XXX: Send message to manager */
}
/**
* Handle DHCPDECLINE request and do nothing at the moment
*/
static void decline_cb(EV_P_ ev_io *w, struct dhcp_msg *msg)
{
(void)EV_A;
(void)w;
(void)msg;
/* XXX: Send message to manager */
}
/**
* Handle DHCPINFORM request and reply with the correct information
*/
static void inform_cb(EV_P_ ev_io *w, struct dhcp_msg *msg)
{
(void)EV_A;
(void)w;
(void)msg;
/* XXX: http://tools.ietf.org/html/draft-ietf-dhc-dhcpinform-clarify-04
*/
}
/**
* Handle libev IO event to socket and call the correct message type
* handler.
*/
static void req_cb(EV_P_ ev_io *w, int revents)
{
(void)revents;
/* Initialize address struct passed to recvfrom */
struct sockaddr_in srcaddr = {
.sin_addr = {INADDR_ANY}
};
socklen_t srcaddrlen = AF_INET;
/* Receive data from socket */
ssize_t recvd = recvfrom(
w->fd,
recv_buffer,
RECV_BUF_LEN,
MSG_DONTWAIT,
(struct sockaddr * restrict)&srcaddr, &srcaddrlen);
/* Detect errors */
if (recvd < 0)
return;
/* Detect too small messages */
if (recvd < DHCP_MSG_HDRLEN)
return;
/* Check magic value */
uint8_t *magic = DHCP_MSG_F_MAGIC(recv_buffer);
if (!DHCP_MSG_MAGIC_CHECK(magic))
return;
/* Extract message type from options */
uint8_t *options = DHCP_MSG_F_OPTIONS(recv_buffer);
struct dhcp_opt current_option;
enum dhcp_msg_type msg_type = 0;
while (dhcp_opt_next(&options, ¤t_option, (uint8_t*)(recv_buffer + recvd)))
if (current_option.code == 53)
msg_type = (enum dhcp_msg_type)current_option.data[0];
struct dhcp_msg msg = {
.data = recv_buffer,
.end = recv_buffer + recvd,
.length = recvd,
.type = msg_type,
.ciaddr.s_addr = ntohl(*DHCP_MSG_F_CIADDR(recv_buffer)),
.yiaddr.s_addr = ntohl(*DHCP_MSG_F_YIADDR(recv_buffer)),
.siaddr.s_addr = ntohl(*DHCP_MSG_F_SIADDR(recv_buffer)),
.giaddr.s_addr = ntohl(*DHCP_MSG_F_GIADDR(recv_buffer)),
.source = (struct sockaddr *)&srcaddr,
.sid = (struct sockaddr_in *)&server_id
};
memcpy(&msg.chaddr, DHCP_MSG_F_CHADDR(recv_buffer), sizeof(msg.chaddr));
switch (msg_type)
{
case DHCPDISCOVER:
discover_cb(EV_A_ w, &msg);
break;
case DHCPREQUEST:
request_cb(EV_A_ w, &msg);
break;
case DHCPRELEASE:
release_cb(EV_A_ w, &msg);
break;
case DHCPDECLINE:
decline_cb(EV_A_ w, &msg);
break;
case DHCPINFORM:
inform_cb(EV_A_ w, &msg);
break;
default:
fprintf(stderr, BROKEN_SOFTWARE_NOTIFICATION);
break;
}
}
int main(int argc, char **argv)
{
struct argv argv_cfg = ARGV_EMPTY;
if (!argv_parse(argc, argv, &argv_cfg))
{
if (argv_cfg.argerror == -1)
dhcpd_error(1, 0, "Unexpected argument list end");
else
dhcpd_error(1, 0, "Unexpected argument %s", argv_cfg.argv[argv_cfg.argerror]);
exit(1);
}
if (argv_cfg.version)
{
printf("dhcpd " VERSION " - (c) 2013 Fritz Conrad Grimpen\n");
exit(0);
}
if (argv_cfg.help || argv_cfg.interface == NULL)
{
printf(USAGE, argv_cfg.arg0);
exit(0);
}
if (!config_fill(&cfg, &argv_cfg))
dhcpd_error(1, 0, cfg.error);
if (argv_cfg.user != NULL)
{
#ifdef __linux__
uid_t uid = 0;
gid_t gid = 0;
struct passwd *pwent;
pwent = getpwnam(argv_cfg.user);
if (pwent == NULL)
{
pwent = getpwuid(atoi(argv_cfg.user));
if (pwent == NULL)
dhcpd_error(1, errno, "Could not find user identified by \"%s\"", argv_cfg.user);
}
uid = pwent->pw_uid;
gid = pwent->pw_gid;
if (argv_cfg.group != NULL)
{
struct group *grent;
grent = getgrnam(argv_cfg.group);
if (grent == NULL)
{
grent = getgrgid(atoi(argv_cfg.group));
if (grent == NULL)
dhcpd_error(1, errno, "Could not find user identified by \"%s\"", argv_cfg.group);
}
gid = grent->gr_gid;
}
capng_clear(CAPNG_SELECT_BOTH);
capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
CAP_NET_BIND_SERVICE, CAP_NET_BROADCAST, CAP_NET_RAW, -1);
if (capng_change_id(uid, gid, CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING))
dhcpd_error(1, 0, "Could not change UID and drop capabilities");
#else
dhcpd_error(1, 0, "Can only drop privileges on Linux");
#endif
}
/* Prepare dummy IP Pool */
pool = pool_create(8);
struct pool_entry entry;
uint32_t ip;
IPRANGE_FOREACH(cfg.iprange[0], cfg.iprange[1], ip) {
entry.address.s_addr = htonl(ip);
pool_add(pool, &entry);
}
/* Set client IP address */
broadcast.sin_port = htons(68);
/* Clear IO buffers */
memset(send_buffer, 0, ARRAY_LEN(send_buffer));
memset(recv_buffer, 0, ARRAY_LEN(recv_buffer));
if (if_nametoindex(argv_cfg.interface) == 0)
dhcpd_error(1, errno, argv_cfg.interface);
if (argv_cfg.debug)
debug = true;
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
dhcpd_error(1, errno, "Could not create socket");
struct sockaddr_in bind_addr = {
.sin_family = AF_INET,
.sin_port = htons(67),
.sin_addr = {INADDR_ANY}
};
struct ifaddrs *ifaddrs, *ifa;
if (getifaddrs(&ifaddrs) == -1)
dhcpd_error(1, errno, "Could not get interface information");
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
if (ifa->ifa_addr->sa_family == AF_INET &&
strcmp(ifa->ifa_name, argv_cfg.interface) == 0)
{
struct sockaddr_in *ifa_addr_in = (struct sockaddr_in *)ifa->ifa_addr;
server_id = *ifa_addr_in;
break;
}
}
freeifaddrs(ifaddrs);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (int[]){1}, sizeof(int)) != 0)
dhcpd_error(1, errno, "Could not set socket to reuse address");
if (bind(sock, (const struct sockaddr *)&bind_addr, sizeof(struct sockaddr_in)) < 0)
dhcpd_error(1, errno, "Could not bind to 0.0.0.0:67");
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (int[]){1}, sizeof(int)) != 0)
dhcpd_error(1, errno, "Could not set broadcast socket option");
#ifdef __linux__
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, argv_cfg.interface, strlen(argv_cfg.interface)) != 0)
dhcpd_error(1, errno, "Could not bind to device %s", argv_cfg.interface);
#endif
struct ev_loop *loop = EV_DEFAULT;
ev_io read_watch;
ev_io_init(&read_watch, req_cb, sock, EV_READ);
ev_io_start(loop, &read_watch);
ev_run(loop, 0);
config_free(&cfg);
argv_free(&argv_cfg);
exit(0);
}