-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathoptions.c
488 lines (376 loc) · 12.4 KB
/
options.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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/errno.h>
#include <time.h>
#include <ctype.h>
#include <regex.h>
#include "queue.h"
#include "options.h"
#include "logging.h"
const uint8_t option_magic[4] = { 0x63, 0x82, 0x53, 0x63 };
/*
* Mapping table between DHCP options and
* functions that parse their value.
*/
static struct {
char *name;
int (*f) (char *, void **);
} dhcp_option_info [256] = {
[PAD] { "PAD", NULL },
[END] { "END", NULL },
[SUBNET_MASK] { "SUBNET_MASK", parse_ip },
[TIME_OFFSET] { "TIME_OFFSET", parse_long },
[ROUTER] { "ROUTER", parse_ip_list },
[TIME_SERVER] { "TIME_SERVER", parse_ip_list },
[NAME_SERVER] { "NAME_SERVER", parse_ip_list },
[DOMAIN_NAME_SERVER] { "DOMAIN_NAME_SERVER", parse_ip_list },
[LOG_SERVER] { "LOG_SERVER", parse_ip_list },
[COOKIE_SERVER] { "COOKIE_SERVER", parse_ip_list },
[LPR_SERVER] { "LPR_SERVER", parse_ip_list },
[IMPRESS_SERVER] { "IMPRESS_SERVER", parse_ip_list },
[RESOURCE_LOCATION_SERVER] { "RESOURCE_LOCATION_SERVER", parse_ip_list },
[HOST_NAME] { "HOST_NAME", parse_string },
[BOOT_FILE_SIZE] { "BOOT_FILE_SIZE", parse_short },
[MERIT_DUMP_FILE] { "MERIT_DUMP_FILE", parse_string },
[DOMAIN_NAME] { "DOMAIN_NAME", parse_string },
[SWAP_SERVER] { "SWAP_SERVER", parse_ip },
[ROOT_PATH] { "ROOT_PATH", parse_string },
[EXTENSIONS_PATH] { "EXTENSIONS_PATH", parse_string },
[IP_FORWARDING] { "IP_FORWARDING", parse_byte },
[NON_LOCAL_SOURCE_ROUTING] { "NON_LOCAL_SOURCE_ROUTING", parse_byte },
[POLICY_FILTER] { "POLICY_FILTER", parse_ip_list },
[MAXIMUM_DATAGRAM_REASSEMBLY_SIZE] { "MAXIMUM_DATAGRAM_REASSEMBLY_SIZE", parse_short },
[DEFAULT_IP_TIME_TO_LIVE] { "DEFAULT_IP_TIME_TO_LIVE", parse_byte },
[PATH_MTU_AGING_TIMEOUT] { "PATH_MTU_AGING_TIMEOUT", parse_long },
[PATH_MTU_PLATEAU_TABLE] { "PATH_MTU_PLATEAU_TABLE", parse_short_list },
[INTERFACE_MTU] { "INTERFACE_MTU", parse_short },
[ALL_SUBNETS_ARE_LOCAL] { "ALL_SUBNETS_ARE_LOCAL", parse_byte },
[BROADCAST_ADDRESS] { "BROADCAST_ADDRESS", parse_ip },
[PERFORM_MASK_DISCOVERY] { "PERFORM_MASK_DISCOVERY", parse_byte },
[MASK_SUPPLIER] { "MASK_SUPPLIER", parse_byte },
[PERFORM_ROUTER_DISCOVERY] { "PERFORM_ROUTER_DISCOVERY", parse_byte },
[ROUTER_SOLICITATION_ADDRESS] { "ROUTER_SOLICITATION_ADDRESS", parse_ip },
[STATIC_ROUTE] { "STATIC_ROUTE", parse_ip_list },
[TRAILER_ENCAPSULATION] { "TRAILER_ENCAPSULATION", parse_byte },
[ARP_CACHE_TIMEOUT] { "ARP_CACHE_TIMEOUT", parse_long },
[ETHERNET_ENCAPSULATION] { "ETHERNET_ENCAPSULATION", parse_byte },
[TCP_DEFAULT_TTL] { "TCP_DEFAULT_TTL", parse_byte },
[TCP_KEEPALIVE_INTERVAL] { "TCP_KEEPALIVE_INTERVAL", parse_long },
[TCP_KEEPALIVE_GARBAGE] { "TCP_KEEPALIVE_GARBAGE", parse_byte },
[NETWORK_INFORMATION_SERVICE_DOMAIN] { "NETWORK_INFORMATION_SERVICE_DOMAIN", parse_string },
[NETWORK_INFORMATION_SERVERS] { "NETWORK_INFORMATION_SERVERS", parse_ip_list },
[NETWORK_TIME_PROTOCOL_SERVERS] { "NETWORK_TIME_PROTOCOL_SERVERS", parse_ip_list },
[VENDOR_SPECIFIC_INFORMATION] { "VENDOR_SPECIFIC_INFORMATION", parse_byte_list },
[NETBIOS_OVER_TCP_IP_NAME_SERVER] { "NETBIOS_OVER_TCP_IP_NAME_SERVER", parse_ip_list },
[NETBIOS_OVER_TCP_IP_DATAGRAM_DISTRIBUTION_SERVER] { "NETBIOS_OVER_TCP_IP_DATAGRAM_DISTRIBUTION_SERVER", parse_ip_list },
[NETBIOS_OVER_TCP_IP_NODE_TYPE] { "NETBIOS_OVER_TCP_IP_NODE_TYPE", parse_byte },
[NETBIOS_OVER_TCP_IP_SCOPE] { "NETBIOS_OVER_TCP_IP_SCOPE", parse_string },
[X_WINDOW_SYSTEM_FONT_SERVER] { "X_WINDOW_SYSTEM_FONT_SERVER", parse_ip_list },
[X_WINDOW_SYSTEM_DISPLAY_MANAGER] { "X_WINDOW_SYSTEM_DISPLAY_MANAGER", parse_ip_list },
[NETWORK_INFORMATION_SERVICE_PLUS_DOMAIN] { "NETWORK_INFORMATION_SERVICE_PLUS_DOMAIN", parse_string },
[NETWORK_INFORMATION_SERVICE_PLUS_SERVERS] { "NETWORK_INFORMATION_SERVICE_PLUS_SERVERS", parse_ip_list },
[MOBILE_IP_HOME_AGENT] { "MOBILE_IP_HOME_AGENT", parse_ip_list },
[SMTP_SERVER] { "SMTP_SERVER", parse_ip_list },
[POP3_SERVER] { "POP3_SERVER", parse_ip_list },
[NNTP_SERVER] { "NNTP_SERVER", parse_ip_list },
[DEFAULT_WWW_SERVER] { "DEFAULT_WWW_SERVER", parse_ip_list },
[DEFAULT_FINGER_SERVER] { "DEFAULT_FINGER_SERVER", parse_ip_list },
[DEFAULT_IRC_SERVER] { "DEFAULT_IRC_SERVER", parse_ip_list },
[STREETTALK_SERVER] { "STREETTALK_SERVER", parse_ip_list },
[STREETTALK_DIRECTORY_ASSISTANCE_SERVER] { "STREETTALK_DIRECTORY_ASSISTANCE_SERVER", parse_ip_list },
[REQUESTED_IP_ADDRESS] { "REQUESTED_IP_ADDRESS", NULL },
[IP_ADDRESS_LEASE_TIME] { "IP_ADDRESS_LEASE_TIME", parse_long },
[OPTION_OVERLOAD] { "OPTION_OVERLOAD", parse_byte },
[TFTP_SERVER_NAME] { "TFTP_SERVER_NAME", parse_string },
[BOOTFILE_NAME] { "BOOTFILE_NAME", parse_string },
[DHCP_MESSAGE_TYPE] { "DHCP_MESSAGE_TYPE", NULL },
[SERVER_IDENTIFIER] { "SERVER_IDENTIFIER", parse_ip },
[PARAMETER_REQUEST_LIST] { "PARAMETER_REQUEST_LIST", NULL },
[MESSAGE] { "MESSAGE", NULL },
[MAXIMUM_DHCP_MESSAGE_SIZE] { "MAXIMUM_DHCP_MESSAGE_SIZE", NULL },
[RENEWAL_T1_TIME_VALUE] { "RENEWAL_T1_TIME_VALUE", parse_long },
[REBINDING_T2_TIME_VALUE] { "REBINDING_T2_TIME_VALUE", parse_long },
[VENDOR_CLASS_IDENTIFIER] { "VENDOR_CLASS_IDENTIFIER", NULL },
[CLIENT_IDENTIFIER] { "CLIENT_IDENTIFIER", NULL },
};
/* Value parsing functions */
int
parse_byte (char *s, void **p)
{
*p = malloc(sizeof(uint8_t));
uint8_t n = ((uint8_t) strtol(s, NULL, 0));
memcpy(*p, &n, sizeof(n));
return sizeof(uint8_t);
}
int
parse_byte_list (char *s, void **p)
{
*p = malloc(strlen(s) * sizeof(uint8_t)); // slightly over the strictly requested size
int count = 0;
char *s2 = strdup(s);
char *s3 = strtok(s2, ", ");
while(s3 != NULL) {
uint8_t n = ((uint8_t) strtol(s3, NULL, 0));
memcpy(((uint8_t *) *p) + count, &n, sizeof(uint8_t));
count += sizeof(uint8_t);
s3 = strtok(NULL, " ");
}
free(s2);
return count;
}
int
parse_short (char *s, void **p)
{
*p = malloc(sizeof(uint16_t));
uint16_t n = ((uint16_t) strtol(s, NULL, 0));
memcpy(*p, &n, sizeof(n));
return sizeof(uint16_t);
}
int
parse_short_list (char *s, void **p)
{
*p = malloc(strlen(s) * sizeof(uint16_t)); // slightly over the strictly requested size
int count = 0;
char *s2 = strdup(s);
char *s3 = strtok(s2, ", ");
while(s3 != NULL) {
uint16_t n = ((uint16_t) strtol(s3, NULL, 0));
memcpy(((uint8_t *) *p) + count, &n, sizeof(uint16_t));
count += sizeof(uint16_t);
s3 = strtok(NULL, " ");
}
free(s2);
return count;
}
int
parse_long (char *s, void **p)
{
*p = malloc(sizeof(uint32_t));
uint32_t n = strtol(s, NULL, 0);
memcpy(*p, &n, sizeof(n));
return sizeof(uint32_t);
}
int
parse_string (char *s, void **p)
{
*p = strdup(s);
return strlen(s);
}
int
parse_ip (char *s, void **p)
{
struct sockaddr_in ip;
*p = malloc(sizeof(uint32_t));
if (inet_aton(s, &ip.sin_addr) == 0) { // error: invalid IP address
free(*p);
return 0;
}
memcpy(*p, &ip.sin_addr, sizeof(uint32_t));
return sizeof(uint32_t);
}
int
parse_ip_list (char *s, void **p)
{
*p = malloc(strlen(s) * sizeof(uint32_t) / 4); // slightly over the strictly required size
int count = 0;
char *s2 = strdup(s);
char *s3 = strtok(s2, ", ");
while(s3 != NULL) {
struct sockaddr_in ip;
if (inet_aton(s3, &ip.sin_addr) == 0) { // error: invalid IP address
free(*p);
return 0;
}
memcpy(((uint8_t *) *p) + count, &ip.sin_addr, sizeof(uint32_t));
count += sizeof(uint32_t);
s3 = strtok(NULL, " ");
}
free(s2);
return count;
}
int
parse_mac (char *s, void **p)
{
*p = malloc(6);
int i;
if (strlen(s) != 17 ||
s[2] != ':' || s[5] != ':' || s[8] != ':' || s[11] != ':' || s[14] != ':') {
free(*p);
return 0; // error: invalid MAC address
}
if (!isxdigit(s[0]) || !isxdigit(s[1]) || !isxdigit(s[3]) || !isxdigit(s[4]) ||
!isxdigit(s[6]) || !isxdigit(s[7]) || !isxdigit(s[9]) || !isxdigit(s[10]) ||
!isxdigit(s[12]) || !isxdigit(s[13]) || !isxdigit(s[15]) || !isxdigit(s[16])) {
free(*p);
return 0; // error: invalid MAC address
}
for (i = 0; i < 6; i++) {
long b = strtol(s+(3*i), NULL, 16);
((uint8_t *) *p)[i] = (uint8_t) b;
}
return 6;
}
/* Option-related functions */
/*
* Given the name of the option and its value as strings,
* fill the dhcp_option structure pointed by opt.
*
* On success return the parsed option id,
* otherwise return zero.
*/
uint8_t
parse_option (dhcp_option *opt, char *name, char *value)
{
int (*f) (char *, void **);
int id;
uint8_t len;
uint8_t *p;
for (id = 0; id < 256; id++) { // search the option by name
if (dhcp_option_info[id].name &&
strcmp(dhcp_option_info[id].name, name) == 0) break;
}
if (id == 256) { // not found
log_error("Unsupported DHCP option '%s'", name);
return 0;
}
f = dhcp_option_info[id].f;
if (f == NULL) { // no parsing function available
log_error("Unsupported DHCP option '%s'", name);
return 0;
}
len = f(value, (void **)&p); // parse the value
if(len == 0) // error parsing the value
return 0;
// structure filling
opt->id = id;
opt->len = len;
memcpy(opt->data, p, len);
free(p);
return opt->id;
}
/*
* Initialize an option list.
*/
void
init_option_list (dhcp_option_list *list)
{
TAILQ_INIT(list);
}
/*
* Given a list of options search an option having
* the passed option id, and returns a pointer to it.
*
* If the option is not present the function returns NULL.
*/
dhcp_option *
search_option (dhcp_option_list *list, uint8_t id)
{
dhcp_option *opt, *opt_temp;
TAILQ_FOREACH_SAFE(opt, list, pointers, opt_temp) {
if(opt->id == id)
return opt;
}
return NULL;
}
/*
* Print options in list.
*/
void
print_options (dhcp_option_list *list)
{
dhcp_option *opt, *opt_temp;
int i=0;
TAILQ_FOREACH_SAFE(opt, list, pointers, opt_temp) {
printf("options[%d]=%d (%s)\n", i++, opt->id,
dhcp_option_info[opt->id].name);
}
}
/*
* Append the provided option to the list.
*
* Always allocate new memory, that must be freed later...
*/
void
append_option (dhcp_option_list *list, dhcp_option *opt)
{
dhcp_option *nopt = calloc(1, sizeof(*nopt));
memcpy(nopt, opt, 2 + opt->len);
TAILQ_INSERT_TAIL(list, nopt, pointers);
}
/*
* Parse the options contained in a DHCP message into a list.
*
* Return 1 on success, 0 if the options are malformed.
*/
int
parse_options_to_list (dhcp_option_list *list, dhcp_option *opts, size_t len)
{
dhcp_option *opt, *end;
opt = opts;
end = (dhcp_option *)(((uint8_t *)opts) + len);
if (len < 4 ||
memcmp(opt, option_magic, sizeof(option_magic)) != 0)
return 0;
opt = (dhcp_option *)(((uint8_t *) opt) + 4);
while (opt < end &&
opt->id != END) { // TODO: check also valid option sizes
if ((dhcp_option *)(((uint8_t *) opt) + 2 + opt->len) >= end)
return 0; // the len field is too long
append_option(list, opt);
opt = (dhcp_option *)(((uint8_t *) opt) + 2 + opt->len);
}
if (opt < end && opt->id == END)
return 1;
return 0;
}
/*
* Serialize a list of options, to be inserted directly inside
* the options section of a DHCP message.
*
* Return 0 on error, the total serialized len on success.
*/
size_t
serialize_option_list (dhcp_option_list *list, uint8_t *buf, size_t len)
{
uint8_t *p = buf;
if (len < 4)
return 0;
memcpy(p, option_magic, sizeof(option_magic));
p += 4; len -= 4;
dhcp_option *opt, *opt_temp;
TAILQ_FOREACH_SAFE(opt, list, pointers, opt_temp) {
if (len <= 2 + opt->len)
return 0;
memcpy(p, opt, 2 + opt->len);
p += 2 + opt->len;
len -= 2 + opt->len;
}
if (len < 1)
return 0;
*p = END;
p++; len--;
return p - buf;
}
/*
* Delete an option list and deallocate its memory.
* Deallocate even the list elements.
*/
void
delete_option_list (dhcp_option_list *list)
{
dhcp_option *opt = TAILQ_FIRST(list);
dhcp_option *tmp;
while (opt != NULL) {
tmp = TAILQ_NEXT(opt, pointers);
free(opt);
opt = tmp;
}
TAILQ_INIT(list);
}