forked from sargon/ddhcpd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ddhcp.c
324 lines (260 loc) · 11 KB
/
ddhcp.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
#include <assert.h>
#include "ddhcp.h"
#include "dhcp.h"
#include "logger.h"
#include "tools.h"
#include "statistics.h"
ATTR_NONNULL_ALL int ddhcp_block_init(ddhcp_config* config) {
DEBUG("ddhcp_block_init(config)\n");
if (config->number_of_blocks < 1) {
FATAL("ddhcp_block_init(...): Need at least 1 block to be configured\n");
return 1;
}
config->blocks = calloc(config->number_of_blocks, sizeof(struct ddhcp_block));
if (!config->blocks) {
FATAL("ddhcp_block_init(...): Can't allocate memory for block structure\n");
return 1;
}
time_t now = time(NULL);
// TODO Maybe we should allocate number_of_blocks dhcp_lease_blocks previous
// and assign one here instead of NULL. Performance boost, Memory defrag?
struct ddhcp_block* block = config->blocks;
for (uint32_t index = 0; index < config->number_of_blocks; index++) {
block->index = index;
block->state = DDHCP_FREE;
addr_add(&config->prefix, &block->subnet, (int)(index * config->block_size));
block->subnet_len = config->block_size;
memset(&block->owner_address, 0, sizeof(struct in6_addr));
block->timeout = now + config->block_timeout;
block->claiming_counts = 0;
block->addresses = NULL;
block++;
}
return 0;
}
ATTR_NONNULL_ALL void ddhcp_block_free(ddhcp_config* config) {
ddhcp_block* block = config->blocks;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
block_free(block++);
}
block_free_claims(config);
free(config->blocks);
}
ATTR_NONNULL_ALL int ddhcp_check_packet(struct ddhcp_mcast_packet* packet, ddhcp_config* config) {
if (memcmp(&packet->prefix, &config->prefix, sizeof(struct in_addr)) != 0 ||
packet->prefix_len != config->prefix_len ||
packet->blocksize != config->block_size) {
return 1;
}
return 0;
}
ATTR_NONNULL_ALL void ddhcp_block_process(uint8_t* buffer, ssize_t len, struct sockaddr_in6 sender, ddhcp_config* config) {
struct ddhcp_mcast_packet packet;
ssize_t ret = ntoh_mcast_packet(buffer, len, &packet);
packet.sender = &sender;
if (ret == 0) {
// Check if this packet is for our swarm
if (ddhcp_check_packet(&packet, config)) {
DEBUG("ddhcp_block_process(...): drop foreign packet before processing");
free(packet.payload);
return;
}
switch (packet.command) {
case DDHCP_MSG_UPDATECLAIM:
statistics_record(config, STAT_MCAST_RECV_UPDATECLAIM, 1);
ddhcp_block_process_claims(&packet, config);
break;
case DDHCP_MSG_INQUIRE:
statistics_record(config, STAT_MCAST_RECV_INQUIRE, 1);
ddhcp_block_process_inquire(&packet, config);
break;
default:
break;
}
free(packet.payload);
} else {
DEBUG("ddhcp_block_process(...): epoll returned status %i\n", ret);
}
}
ATTR_NONNULL_ALL void ddhcp_block_process_claims(struct ddhcp_mcast_packet* packet, ddhcp_config* config) {
DEBUG("ddhcp_block_process_claims(packet,config)\n");
assert(packet->command == 1);
time_t now = time(NULL);
ddhcp_block* blocks = config->blocks;
for (unsigned int i = 0; i < packet->count; i++) {
struct ddhcp_payload* claim = &packet->payload[i];
uint32_t block_index = claim->block_index;
if (block_index >= config->number_of_blocks) {
WARNING("ddhcp_block_process_claims(...): Malformed block number\n");
continue;
}
if (blocks[block_index].state == DDHCP_OURS && NODE_ID_CMP(packet->node_id, config->node_id) < 0) {
INFO("ddhcp_block_process_claims(...): node 0x%02x%02x%02x%02x%02x%02x%02x%02x claims our block %i\n", HEX_NODE_ID(packet->node_id), block_index);
// TODO Decide when and if we reclaim this block
// Which node has more leases in this block, ..., who has the better node_id.
// Unrelated from the above, the original concept is claiming the block now.
blocks[block_index].timeout = 0;
block_update_claims(config);
} else {
// Notice the ownership
blocks[block_index].state = DDHCP_CLAIMED;
blocks[block_index].timeout = now + claim->timeout;
// Save the connection details for the claiming node
// We need to contact him, for dhcp forwarding actions.
memcpy(&blocks[block_index].owner_address, &packet->sender->sin6_addr, sizeof(struct in6_addr));
memcpy(&blocks[block_index].node_id, &packet->node_id, sizeof(ddhcp_node_id));
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char ipv6_sender[INET6_ADDRSTRLEN];
DEBUG("ddhcp_block_process_claims(...): Register block to %s\n",
inet_ntop(AF_INET6, &blocks[block_index].owner_address, ipv6_sender, INET6_ADDRSTRLEN));
#endif
INFO("ddhcp_block_process_claims(...): node 0x%02x%02x%02x%02x%02x%02x%02x%02x claims block %i with TTL %i\n", HEX_NODE_ID(packet->node_id), block_index, claim->timeout);
}
}
}
ATTR_NONNULL_ALL void ddhcp_block_process_inquire(struct ddhcp_mcast_packet* packet, ddhcp_config* config) {
DEBUG("ddhcp_block_process_inquire(packet,config)\n");
assert(packet->command == 2);
time_t now = time(NULL);
ddhcp_block* blocks = config->blocks;
for (unsigned int i = 0; i < packet->count; i++) {
struct ddhcp_payload* tmp = &packet->payload[i];
if (tmp->block_index >= config->number_of_blocks) {
WARNING("ddhcp_block_process_inquire(...): Malformed block number\n");
continue;
}
INFO("ddhcp_block_process_inquire(...): node 0x%02x%02x%02x%02x%02x%02x%02x%02x inquires block %i\n", HEX_NODE_ID(packet->node_id), tmp->block_index);
if (blocks[tmp->block_index].state == DDHCP_OURS) {
// Update Claims
INFO("ddhcp_block_process_inquire(...): block %i is ours, notify network\n", tmp->block_index);
blocks[tmp->block_index].timeout = 0;
block_update_claims(config);
} else if (blocks[tmp->block_index].state == DDHCP_CLAIMING) {
INFO("ddhcp_block_process_inquire(...): we are furthermore interested in block %i\n", tmp->block_index);
// QUESTION Why do we need multiple states for the same process?
if (NODE_ID_CMP(packet->node_id, config->node_id) > 0) {
INFO("ddhcp_block_process_inquire(...): ... but other node wins.\n");
blocks[tmp->block_index].state = DDHCP_TENTATIVE;
blocks[tmp->block_index].timeout = now + config->tentative_timeout;
}
// otherwise keep inquiring, the other node should see our inquires and step back.
} else {
INFO("ddhcp_block_process_inquire(...): set block %i to tentative\n", tmp->block_index);
blocks[tmp->block_index].state = DDHCP_TENTATIVE;
blocks[tmp->block_index].timeout = now + config->tentative_timeout;
}
}
}
ATTR_NONNULL_ALL void ddhcp_dhcp_process(uint8_t* buffer, ssize_t len, struct sockaddr_in6 sender, ddhcp_config* config) {
struct ddhcp_mcast_packet packet;
ssize_t ret = ntoh_mcast_packet(buffer, len, &packet);
packet.sender = &sender;
if (ret == 0) {
// Check if this packet is for our swarm
if (ddhcp_check_packet(&packet, config)) {
DEBUG("ddhcp_dhcp_process(...): drop foreign packet before processing");
free(packet.renew_payload);
return;
}
switch (packet.command) {
case DDHCP_MSG_RENEWLEASE:
statistics_record(config, STAT_DIRECT_RECV_RENEWLEASE, 1);
ddhcp_dhcp_renewlease(&packet, config);
break;
case DDHCP_MSG_LEASEACK:
statistics_record(config, STAT_DIRECT_RECV_LEASEACK, 1);
ddhcp_dhcp_leaseack(&packet, config);
break;
case DDHCP_MSG_LEASENAK:
statistics_record(config, STAT_DIRECT_RECV_LEASENAK, 1);
ddhcp_dhcp_leasenak(&packet, config);
break;
case DDHCP_MSG_RELEASE:
statistics_record(config, STAT_DIRECT_RECV_RELEASE, 1);
ddhcp_dhcp_release(&packet, config);
break;
default:
break;
}
}
}
ATTR_NONNULL_ALL void ddhcp_dhcp_renewlease(struct ddhcp_mcast_packet* packet, ddhcp_config* config) {
DEBUG("ddhcp_dhcp_renewlease(request,config)\n");
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(packet->renew_payload->chaddr);
DEBUG("ddhcp_dhcp_renewlease(...): Request for xid: %u chaddr: %s\n", packet->renew_payload->xid, hwaddr);
free(hwaddr);
#endif
int ret = dhcp_rhdl_request(&(packet->renew_payload->address), config);
ddhcp_mcast_packet* answer = NULL;
if (ret == 0) {
DEBUG("ddhcp_dhcp_renewlease(...): %i ACK\n", ret);
answer = new_ddhcp_packet(DDHCP_MSG_LEASEACK, config);
statistics_record(config, STAT_DIRECT_SEND_LEASEACK, 1);
} else if (ret == 1) {
DEBUG("ddhcp_dhcp_renewlease(...): %i NAK\n", ret);
answer = new_ddhcp_packet(DDHCP_MSG_LEASENAK, config);
statistics_record(config, STAT_DIRECT_SEND_LEASENAK, 1);
// TODO Can we hand over the block?
} else {
// Unexpected behaviour
WARNING("ddhcp_dhcp_renewlease(...): Unexpected return value from dhcp_rhdl_request.");
return;
}
if (!answer) {
WARNING("ddhcp_dhcp_renewlease(...): Failed to allocate memory for ddhcpd mcast packet.\n");
return;
}
answer->renew_payload = packet->renew_payload;
statistics_record(config, STAT_DIRECT_SEND_PKG, 1);
ssize_t bytes_send = send_packet_direct(answer, &packet->sender->sin6_addr, DDHCP_SKT_SERVER(config));
statistics_record(config, STAT_DIRECT_SEND_BYTE, (long int) bytes_send);
UNUSED(bytes_send);
free(answer->renew_payload);
free(answer);
}
ATTR_NONNULL_ALL void ddhcp_dhcp_leaseack(struct ddhcp_mcast_packet* request, ddhcp_config* config) {
// Stub functions
DEBUG("ddhcp_dhcp_leaseack(request,config)\n");
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(request->renew_payload->chaddr);
DEBUG("ddhcp_dhcp_leaseack(...): ACK for xid: %u chaddr: %s\n", request->renew_payload->xid, hwaddr);
free(hwaddr);
#endif
dhcp_packet* packet = dhcp_packet_list_find(&config->dhcp_packet_cache, request->renew_payload->xid, request->renew_payload->chaddr);
if (!packet) {
// Ignore packet
DEBUG("ddhcp_dhcp_leaseack(...): No matching packet found, message ignored\n");
} else {
// Process packet
dhcp_rhdl_ack(DDHCP_SKT_DHCP(config)->fd, packet, config);
dhcp_packet_free(packet, 1);
free(packet);
}
free(request->renew_payload);
}
ATTR_NONNULL_ALL void ddhcp_dhcp_leasenak(struct ddhcp_mcast_packet* request, ddhcp_config* config) {
// Stub functions
DEBUG("ddhcp_dhcp_leasenak(request,config)\n");
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(request->renew_payload->chaddr);
DEBUG("ddhcp_dhcp_leaseack(...): NAK for xid: %u chaddr: %s\n", request->renew_payload->xid, hwaddr);
free(hwaddr);
#endif
dhcp_packet* packet = dhcp_packet_list_find(&config->dhcp_packet_cache, request->renew_payload->xid, request->renew_payload->chaddr);
if (!packet) {
// Ignore packet
DEBUG("ddhcp_dhcp_leaseack(...): No matching packet found, message ignored\n");
} else {
// Process packet
dhcp_nack(DDHCP_SKT_DHCP(config)->fd, packet, config);
dhcp_packet_free(packet, 1);
free(packet);
}
free(request->renew_payload);
}
ATTR_NONNULL_ALL void ddhcp_dhcp_release(struct ddhcp_mcast_packet* packet, ddhcp_config* config) {
DEBUG("ddhcp_dhcp_release(packet,config)\n");
dhcp_release_lease(packet->renew_payload->address, config);
free(packet->renew_payload);
}