-
Notifications
You must be signed in to change notification settings - Fork 12
/
dhcp_packet.c
455 lines (360 loc) · 11.2 KB
/
dhcp_packet.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
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include "types.h"
#include "logger.h"
struct sockaddr_in broadcast = {
.sin_family = AF_INET,
.sin_addr = {INADDR_BROADCAST},
};
struct sockaddr_in unicast = {
.sin_family = AF_INET,
.sin_addr = {0},
};
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
ATTR_NONNULL_ALL void printf_dhcp(dhcp_packet* packet) {
char tmpstr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(packet->ciaddr.s_addr), tmpstr, INET_ADDRSTRLEN);
DEBUG("BOOTP [ op %i, htype %i, hlen %i, hops %i, xid %lu, secs %i, flags %i, ciaddr %s, "
, packet->op
, packet->htype
, packet->hlen
, packet->hops
, (unsigned long) packet->xid
, packet->secs
, packet->flags
, tmpstr
);
inet_ntop(AF_INET, &(packet->yiaddr.s_addr), tmpstr, INET_ADDRSTRLEN);
DEBUG("yiaddr %s, ", tmpstr);
inet_ntop(AF_INET, &(packet->siaddr.s_addr), tmpstr, INET_ADDRSTRLEN);
DEBUG("siaddr %s, ", tmpstr);
inet_ntop(AF_INET, &(packet->giaddr.s_addr), tmpstr, INET_ADDRSTRLEN);
DEBUG("giaddr %s, sname: %s, file: %s ]\n"
, tmpstr
, packet->sname
, packet->file
);
dhcp_option* option = packet->options;
for (int i = 0; i < packet->options_len; i++) {
if (option->len == 1) {
DEBUG("DHCP OPTION [ code %i, length %i, value %i ]\n", option->code, option->len, option->payload[0]);
} else if (option->code == DHCP_CODE_PARAMETER_REQUEST_LIST) {
DEBUG("DHCP OPTION [ code %i, length %i, value ", option->code, option->len);
for (int k = 0; k < option->len; k++) {
LOG("%i ", option->payload[k]);
}
LOG("]\n");
} else {
DEBUG("DHCP OPTION [ code %i, length %i ]\n", option->code, option->len);
}
option++;
}
}
# else
#define printf_dhcp(packet) {}
# endif
ATTR_NONNULL_ALL size_t _dhcp_packet_len(dhcp_packet* packet) {
size_t len = 240 + 1;
dhcp_option* option = packet->options;
for (int i = 0; i < packet->options_len; i++) {
switch (option->code) {
case DHCP_CODE_PAD:
case DHCP_CODE_END:
len++;
break;
default:
len += 2 + (size_t)option->len;
break;
}
option++;
}
return len;
}
ATTR_NONNULL_ALL ssize_t ntoh_dhcp_packet(dhcp_packet* packet, uint8_t* buffer, ssize_t len) {
uint16_t tmp16;
uint32_t tmp32;
if (len < 240) {
return -1;
}
DEBUG("ntoh_dhcp_packet(...): package len:%zi\n", len);
// TODO Use macros to read from the buffer
packet->op = buffer[0];
packet->htype = buffer[1];
packet->hlen = buffer[2];
packet->hops = buffer[3];
memcpy(&tmp32, buffer + 4, 4);
packet->xid = ntohl(tmp32);
memcpy(&tmp16, buffer + 8, 2);
packet->secs = ntohs(tmp16);
memcpy(&tmp16, buffer + 10, 2);
packet->flags = ntohs(tmp16);
memcpy(&packet->ciaddr, buffer + 12, 4);
memcpy(&packet->yiaddr, buffer + 16, 4);
memcpy(&packet->siaddr, buffer + 20, 4);
memcpy(&packet->giaddr, buffer + 24, 4);
memcpy(&packet->chaddr, buffer + 28, 16);
memcpy(&packet->sname, buffer + 44, 64);
memcpy(&packet->file, buffer + 108, 128);
// Check for the magic cookie
if (!((uint8_t) buffer[236] == 99
&& (uint8_t) buffer[237] == 130
&& (uint8_t) buffer[238] == 83
&& (uint8_t) buffer[239] == 99
)) {
WARNING("ntoh_dhcp_packet(...) -> Magic cookie not found, possibly malformed request!\n");
return -7;
}
uint8_t* option = buffer + 236 + 4;
// Count options
size_t options = 0;
int exit = 0;
int dhcp_message_type = 0;
int dhcp_request_list = 0;
while (option < buffer + len && exit == 0) {
switch ((uint8_t) option[0]) {
case DHCP_CODE_PAD:
// JUMP the padding
option += 1;
continue;
break;
case DHCP_CODE_END:
options++;
exit = 1;
continue;
break;
case DHCP_CODE_MESSAGE_TYPE:
dhcp_message_type = 1;
break;
case DHCP_CODE_PARAMETER_REQUEST_LIST:
dhcp_request_list = 1;
break;
default:
break;
}
if (option + 1 > buffer + len) {
WARNING("ntoh_dhcp_packet(...): DHCP options ended improperly, possible broken client.\n");
return -4;
}
if (option + option[1] + 2 > buffer + len) {
// Error: Malformed dhcp options
WARNING("ntoh_dhcp_packet(...): DHCP options smaller than len of last option suggests, possible broken client.\n");
return -5;
} else {
option += (uint8_t) option[1] + 2;
options++;
}
}
if (dhcp_message_type != 1) {
INFO("ntoh_dhcp_packet(...): Message contains no message type - invalid!\n");
return -6;
}
if (dhcp_request_list != 1) {
DEBUG("ntoh_dhcp_packet(...): Message contains no DHCP request list - broken client?\n");
}
packet->options_len = (uint8_t)options;
packet->options = (dhcp_option*) calloc(options, sizeof(dhcp_option));
if (!packet->options) {
WARNING("ntoh_dhcp_packet(...): option memory allocation failed.\n");
return -ENOMEM;
}
option = buffer + 236 + 4;
exit = 0;
uint8_t i = 0;
while (option < buffer + len && exit == 0) {
switch ((uint8_t) option[0]) {
case DHCP_CODE_END:
exit = 1;
/* fall through */
case DHCP_CODE_PAD:
// JUMP padding and end
packet->options[i].code = option[0];
packet->options[i].len = 0;
packet->options[i].payload = NULL;
option += 1;
i++;
break;
default:
packet->options[i].code = option[0];
packet->options[i].len = option[1];
packet->options[i].payload = option + 2;
option += (uint8_t) option[1] + 2;
i++;
break;
}
}
assert(i == options);
#if LOG_LEVEL_LIMIT >= LOG_INFO
printf_dhcp(packet);
#endif
return 0;
}
ATTR_NONNULL_ALL ssize_t dhcp_packet_send(int socket, dhcp_packet* packet) {
DEBUG("dhcp_packet_send(socket:%i, dhcp_packet)\n", socket);
uint16_t tmp16;
uint32_t tmp32;
uint8_t* buffer = calloc(sizeof(char), _dhcp_packet_len(packet));
if (!buffer) {
return -ENOMEM;
}
// Header
buffer[0] = packet->op;
buffer[1] = packet->htype;
buffer[2] = packet->hlen;
buffer[3] = packet->hops;
tmp32 = htonl(packet->xid);
memcpy(buffer + 4, &tmp32, 4);
tmp16 = htons(packet->secs);
memcpy(buffer + 8, &tmp16, 2);
tmp16 = htons(packet->flags);
memcpy(buffer + 10, &tmp16, 2);
memcpy(buffer + 12, &packet->ciaddr, 4);
memcpy(buffer + 16, &packet->yiaddr, 4);
memcpy(buffer + 20, &packet->siaddr, 4);
memcpy(buffer + 24, &packet->giaddr, 4);
memcpy(buffer + 28, &packet->chaddr, 16);
memcpy(buffer + 44, &packet->sname, 64);
memcpy(buffer + 108, &packet->file, 128);
// Magic Cookie
buffer[236] = 99;
buffer[237] = 130;
buffer[238] = 83;
buffer[239] = 99;
// Options
uint8_t* obuf = buffer + 240;
dhcp_option* option = packet->options;
for (int i = 0 ; i < packet->options_len; i++) {
obuf[0] = option->code;
switch (option->code) {
case DHCP_CODE_PAD:
case DHCP_CODE_END:
obuf++;
break;
default:
obuf[1] = option->len;
memcpy(obuf + 2, option->payload, option->len);
obuf = obuf + 2 + option->len;
break;
}
option++;
}
buffer[_dhcp_packet_len(packet) - 1] = 255;
assert(obuf + 1 == buffer + _dhcp_packet_len(packet));
// Network send
printf("Message LEN: %zu\n", _dhcp_packet_len(packet));
struct sockaddr_in *address = &broadcast;
// Check the broadcast flag
if ( !(packet->flags & DHCP_BROADCAST_MASK) ) {
// Check if client address is set to none zero
uint8_t zeros[4] = {0,0,0,0};
if ( memcmp(zeros,&packet->ciaddr,4) != 0) {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char ipv4_sender[INET_ADDRSTRLEN];
DEBUG("dhcp_packet_send: Sending unicast to %s \n",inet_ntop(AF_INET, &packet->ciaddr, ipv4_sender, INET_ADDRSTRLEN));
#endif
address = &unicast;
address->sin_addr = packet->ciaddr;
}
}
address->sin_port = htons(68);
ssize_t bytes_send = sendto(socket, buffer, _dhcp_packet_len(packet), 0, (struct sockaddr*)address, sizeof(broadcast));
if ( bytes_send < 0 ) {
ERROR("dhcp_packet_send(...): Failed (%i): %s\n",errno,strerror(errno));
}
free(buffer);
return bytes_send;
}
ATTR_NONNULL_ALL int dhcp_packet_copy(dhcp_packet* dest, dhcp_packet* src) {
int err;
memcpy(dest, src, sizeof(struct dhcp_packet));
dest->options = (struct dhcp_option*) calloc(src->options_len, sizeof(struct dhcp_option));
if (!dest->options) {
return 1;
}
dhcp_option* src_option = src->options;
dhcp_option* dest_option = dest->options;
for (; src_option < src->options + src->options_len; src_option++) {
uint8_t* dest_payload = (uint8_t*) calloc(src_option->len, sizeof(uint8_t));
if (!dest_payload) {
err = -ENOMEM;
goto fail_options;
}
memcpy(dest_payload, src_option->payload, src_option->len);
dest_option->code = src_option->code;
dest_option->len = src_option->len;
dest_option->payload = dest_payload;
dest_option++;
}
return 0;
fail_options:
while (dest_option-- > dest->options) {
free(dest_option->payload);
}
free(dest->options);
return err;
}
ATTR_NONNULL_ALL int dhcp_packet_list_add(dhcp_packet_list* list, dhcp_packet* packet) {
time_t now = time(NULL);
// Save dhcp packet, for further actions, later.
dhcp_packet* copy = calloc(1, sizeof(dhcp_packet));
if (!copy) {
ERROR("dhcp_packet_list_add(...): Unable to allocate memory\n");
return 1;
}
dhcp_packet_copy(copy, packet);
copy->timeout = now + 120;
list_add_tail(©->packet_list, list);
return 0;
}
ATTR_NONNULL_ALL dhcp_packet* dhcp_packet_list_find(dhcp_packet_list* list, uint32_t xid, uint8_t* chaddr) {
DEBUG("dhcp_packet_list_find(list,xid:%u,chaddr)\n", xid);
struct list_head* pos, *q;
list_for_each_safe(pos, q, list) {
dhcp_packet* packet = list_entry(pos, dhcp_packet, packet_list);
if (packet->xid == xid) {
if (memcmp(packet->chaddr, chaddr, 16) == 0) {
DEBUG("dhcp_packet_list_find(...): packet found\n");
list_del(pos);
return packet;
}
} else {
DEBUG("dhcp_packet_list_find(...): Packet (%u)\n", packet->xid);
}
}
DEBUG("dhcp_packet_list_find(...): No matching packet found\n");
return NULL;
}
ATTR_NONNULL_ALL void dhcp_packet_list_free(dhcp_packet_list* list) {
DEBUG("dhcp_packet_list_free(list)\n");
struct list_head* pos, *q;
list_for_each_safe(pos, q, list) {
dhcp_packet* packet = list_entry(pos, dhcp_packet, packet_list);
list_del(pos);
dhcp_packet_free(packet, 1);
free(packet);
}
}
ATTR_NONNULL_ALL uint8_t dhcp_packet_message_type(dhcp_packet* packet) {
dhcp_option* option = packet->options;
for (int i = 0; i < packet->options_len; i++) {
if (option->code == DHCP_CODE_MESSAGE_TYPE) {
return (uint8_t) option->payload[0];
}
option++;
}
return 0;
}
ATTR_NONNULL_ALL void dhcp_packet_list_timeout(dhcp_packet_list* list) {
DEBUG("dhcp_packet_list_timeout(list)\n");
struct list_head* pos, *q;
time_t now = time(NULL);
list_for_each_safe(pos, q, list) {
dhcp_packet* packet = list_entry(pos, dhcp_packet, packet_list);
if (packet->timeout < now) {
list_del(pos);
dhcp_packet_free(packet, 1);
DEBUG("dhcp_packet_list_timeout(...): drop packet from cache\n");
}
}
}