Set broadcast flag in DHCP requests when sending broadcasts
[strongswan.git] / src / libcharon / plugins / dhcp / dhcp_socket.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "dhcp_socket.h"
17
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <netinet/udp.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_ether.h>
26 #include <linux/filter.h>
27
28 #include <utils/linked_list.h>
29 #include <utils/identification.h>
30 #include <threading/mutex.h>
31 #include <threading/condvar.h>
32 #include <threading/thread.h>
33
34 #include <hydra.h>
35 #include <daemon.h>
36 #include <processing/jobs/callback_job.h>
37
38 #define DHCP_SERVER_PORT 67
39 #define DHCP_CLIENT_PORT 68
40 #define DHCP_TRIES 5
41
42 typedef struct private_dhcp_socket_t private_dhcp_socket_t;
43
44 /**
45 * Private data of an dhcp_socket_t object.
46 */
47 struct private_dhcp_socket_t {
48
49 /**
50 * Public dhcp_socket_t interface.
51 */
52 dhcp_socket_t public;
53
54 /**
55 * Random number generator
56 */
57 rng_t *rng;
58
59 /**
60 * List of transactions in DISCOVER
61 */
62 linked_list_t *discover;
63
64 /**
65 * List of transactions in REQUEST
66 */
67 linked_list_t *request;
68
69 /**
70 * List of successfully completed transactions
71 */
72 linked_list_t *completed;
73
74 /**
75 * Lock for transactions
76 */
77 mutex_t *mutex;
78
79 /**
80 * Condvar to wait for transaction completion
81 */
82 condvar_t *condvar;
83
84 /**
85 * Threads waiting in condvar
86 */
87 int waiting;
88
89 /**
90 * DHCP send socket
91 */
92 int send;
93
94 /**
95 * DHCP receive socket
96 */
97 int receive;
98
99 /**
100 * Do we use per-identity or random leases (and MAC addresses)
101 */
102 bool identity_lease;
103
104 /**
105 * DHCP server address, or broadcast
106 */
107 host_t *dst;
108
109 /**
110 * Callback job receiving DHCP responses
111 */
112 callback_job_t *job;
113 };
114
115 /**
116 * DHCP opcode (or BOOTP actually)
117 */
118 typedef enum {
119 BOOTREQUEST = 1,
120 BOOTREPLY = 2,
121 } dhcp_opcode_t;
122
123 /**
124 * Some DHCP options used
125 */
126 typedef enum {
127 DHCP_DNS_SERVER = 6,
128 DHCP_HOST_NAME = 12,
129 DHCP_NBNS_SERVER = 44,
130 DHCP_REQUESTED_IP = 50,
131 DHCP_MESSAGE_TYPE = 53,
132 DHCP_SERVER_ID = 54,
133 DHCP_PARAM_REQ_LIST = 55,
134 DHCP_CLIENT_ID = 61,
135 DHCP_OPTEND = 255,
136 } dhcp_option_type_t;
137
138 /**
139 * DHCP messages types in the DHCP_MESSAGE_TYPE option
140 */
141 typedef enum {
142 DHCP_DISCOVER = 1,
143 DHCP_OFFER = 2,
144 DHCP_REQUEST = 3,
145 DHCP_DECLINE = 4,
146 DHCP_ACK = 5,
147 DHCP_NAK = 6,
148 DHCP_RELEASE = 7,
149 DHCP_INFORM = 8,
150 } dhcp_message_type_t;
151 /**
152 * DHCP option encoding, a TLV
153 */
154 typedef struct __attribute__((packed)) {
155 u_int8_t type;
156 u_int8_t len;
157 char data[];
158 } dhcp_option_t;
159
160 /**
161 * DHCP message format, with a maximum size options buffer
162 */
163 typedef struct __attribute__((packed)) {
164 u_int8_t opcode;
165 u_int8_t hw_type;
166 u_int8_t hw_addr_len;
167 u_int8_t hop_count;
168 u_int32_t transaction_id;
169 u_int16_t number_of_seconds;
170 u_int16_t flags;
171 u_int32_t client_address;
172 u_int32_t your_address;
173 u_int32_t server_address;
174 u_int32_t gateway_address;
175 char client_hw_addr[6];
176 char client_hw_padding[10];
177 char server_hostname[64];
178 char boot_filename[128];
179 u_int32_t magic_cookie;
180 char options[252];
181 } dhcp_t;
182
183 /**
184 * Prepare a DHCP message for a given transaction
185 */
186 static int prepare_dhcp(private_dhcp_socket_t *this,
187 dhcp_transaction_t *transaction,
188 dhcp_message_type_t type, dhcp_t *dhcp)
189 {
190 chunk_t chunk, broadcast = chunk_from_chars(0xFF,0xFF,0xFF,0xFF);
191 identification_t *identity;
192 dhcp_option_t *option;
193 int optlen = 0;
194 host_t *src;
195 u_int32_t id;
196
197 memset(dhcp, 0, sizeof(*dhcp));
198 dhcp->opcode = BOOTREQUEST;
199 dhcp->hw_type = ARPHRD_ETHER;
200 dhcp->hw_addr_len = 6;
201 dhcp->transaction_id = transaction->get_id(transaction);
202 if (chunk_equals(broadcast, this->dst->get_address(this->dst)))
203 {
204 /* Set broadcast flag to get broadcasted replies, as we actually
205 * do not own the MAC we request an address for. */
206 dhcp->flags = htons(0x8000);
207 /* TODO: send with 0.0.0.0 source address */
208 }
209 else
210 {
211 /* act as relay agent */
212 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
213 this->dst, NULL);
214 if (src)
215 {
216 memcpy(&dhcp->gateway_address, src->get_address(src).ptr,
217 sizeof(dhcp->gateway_address));
218 src->destroy(src);
219 }
220 }
221
222 identity = transaction->get_identity(transaction);
223 chunk = identity->get_encoding(identity);
224 /* magic bytes, a locally administered unicast MAC */
225 dhcp->client_hw_addr[0] = 0x7A;
226 dhcp->client_hw_addr[1] = 0xA7;
227 /* with ID specific postfix */
228 if (this->identity_lease)
229 {
230 id = htonl(chunk_hash(chunk));
231 }
232 else
233 {
234 id = transaction->get_id(transaction);
235 }
236 memcpy(&dhcp->client_hw_addr[2], &id, sizeof(id));
237
238 dhcp->magic_cookie = htonl(0x63825363);
239
240 option = (dhcp_option_t*)&dhcp->options[optlen];
241 option->type = DHCP_MESSAGE_TYPE;
242 option->len = 1;
243 option->data[0] = type;
244 optlen += sizeof(dhcp_option_t) + option->len;
245
246 if (identity->get_type(identity) == ID_FQDN)
247 {
248 option = (dhcp_option_t*)&dhcp->options[optlen];
249 option->type = DHCP_HOST_NAME;
250 option->len = min(chunk.len, 64);
251 memcpy(option->data, chunk.ptr, option->len);
252 optlen += sizeof(dhcp_option_t) + option->len;
253 }
254
255 option = (dhcp_option_t*)&dhcp->options[optlen];
256 option->type = DHCP_CLIENT_ID;
257 option->len = min(chunk.len, 64);
258 memcpy(option->data, chunk.ptr, option->len);
259 optlen += sizeof(dhcp_option_t) + option->len;
260
261 return optlen;
262 }
263
264 /**
265 * Send a DHCP message with given options length
266 */
267 static bool send_dhcp(private_dhcp_socket_t *this,
268 dhcp_transaction_t *transaction, dhcp_t *dhcp, int optlen)
269 {
270 host_t *dst;
271 ssize_t len;
272
273 dst = transaction->get_server(transaction);
274 if (!dst)
275 {
276 dst = this->dst;
277 }
278 len = offsetof(dhcp_t, magic_cookie) + ((optlen + 4) / 64 * 64 + 64);
279 return sendto(this->send, dhcp, len, 0, dst->get_sockaddr(dst),
280 *dst->get_sockaddr_len(dst)) == len;
281 }
282
283 /**
284 * Send DHCP discover using a given transaction
285 */
286 static bool discover(private_dhcp_socket_t *this,
287 dhcp_transaction_t *transaction)
288 {
289 dhcp_option_t *option;
290 dhcp_t dhcp;
291 int optlen;
292
293 optlen = prepare_dhcp(this, transaction, DHCP_DISCOVER, &dhcp);
294
295 DBG1(DBG_CFG, "sending DHCP DISCOVER to %H", this->dst);
296
297 option = (dhcp_option_t*)&dhcp.options[optlen];
298 option->type = DHCP_PARAM_REQ_LIST;
299 option->len = 2;
300 option->data[0] = DHCP_DNS_SERVER;
301 option->data[1] = DHCP_NBNS_SERVER;
302 optlen += sizeof(dhcp_option_t) + option->len;
303
304 dhcp.options[optlen++] = DHCP_OPTEND;
305
306 if (!send_dhcp(this, transaction, &dhcp, optlen))
307 {
308 DBG1(DBG_CFG, "sending DHCP DISCOVER failed: %s", strerror(errno));
309 return FALSE;
310 }
311 return TRUE;
312 }
313
314 /**
315 * Send DHCP request using a given transaction
316 */
317 static bool request(private_dhcp_socket_t *this,
318 dhcp_transaction_t *transaction)
319 {
320 dhcp_option_t *option;
321 dhcp_t dhcp;
322 host_t *offer, *server;
323 chunk_t chunk;
324 int optlen;
325
326 optlen = prepare_dhcp(this, transaction, DHCP_REQUEST, &dhcp);
327
328 offer = transaction->get_address(transaction);
329 server = transaction->get_server(transaction);
330 if (!offer || !server)
331 {
332 return FALSE;
333 }
334 DBG1(DBG_CFG, "sending DHCP REQUEST for %H to %H", offer, server);
335
336 option = (dhcp_option_t*)&dhcp.options[optlen];
337 option->type = DHCP_REQUESTED_IP;
338 option->len = 4;
339 chunk = offer->get_address(offer);
340 memcpy(option->data, chunk.ptr, min(chunk.len, option->len));
341 optlen += sizeof(dhcp_option_t) + option->len;
342
343 option = (dhcp_option_t*)&dhcp.options[optlen];
344 option->type = DHCP_SERVER_ID;
345 option->len = 4;
346 chunk = server->get_address(server);
347 memcpy(option->data, chunk.ptr, min(chunk.len, option->len));
348 optlen += sizeof(dhcp_option_t) + option->len;
349
350 option = (dhcp_option_t*)&dhcp.options[optlen];
351 option->type = DHCP_PARAM_REQ_LIST;
352 option->len = 2;
353 option->data[0] = DHCP_DNS_SERVER;
354 option->data[1] = DHCP_NBNS_SERVER;
355 optlen += sizeof(dhcp_option_t) + option->len;
356
357 dhcp.options[optlen++] = DHCP_OPTEND;
358
359 if (!send_dhcp(this, transaction, &dhcp, optlen))
360 {
361 DBG1(DBG_CFG, "sending DHCP REQUEST failed: %s", strerror(errno));
362 return FALSE;
363 }
364 return TRUE;
365 }
366
367 METHOD(dhcp_socket_t, enroll, dhcp_transaction_t*,
368 private_dhcp_socket_t *this, identification_t *identity)
369 {
370 dhcp_transaction_t *transaction;
371 u_int32_t id;
372 int try;
373
374 this->rng->get_bytes(this->rng, sizeof(id), (u_int8_t*)&id);
375 transaction = dhcp_transaction_create(id, identity);
376
377 this->mutex->lock(this->mutex);
378 this->discover->insert_last(this->discover, transaction);
379 try = 1;
380 while (try <= DHCP_TRIES && discover(this, transaction))
381 {
382 if (!this->condvar->timed_wait(this->condvar, this->mutex, 1000 * try) &&
383 this->request->find_first(this->request, NULL,
384 (void**)&transaction) == SUCCESS)
385 {
386 break;
387 }
388 try++;
389 }
390 if (this->discover->remove(this->discover, transaction, NULL))
391 { /* no OFFER received */
392 this->mutex->unlock(this->mutex);
393 transaction->destroy(transaction);
394 DBG1(DBG_CFG, "DHCP DISCOVER timed out");
395 return NULL;
396 }
397
398 try = 1;
399 while (try <= DHCP_TRIES && request(this, transaction))
400 {
401 if (!this->condvar->timed_wait(this->condvar, this->mutex, 1000 * try) &&
402 this->completed->remove(this->completed, transaction, NULL))
403 {
404 break;
405 }
406 try++;
407 }
408 if (this->request->remove(this->request, transaction, NULL))
409 { /* no ACK received */
410 this->mutex->unlock(this->mutex);
411 transaction->destroy(transaction);
412 DBG1(DBG_CFG, "DHCP REQUEST timed out");
413 return NULL;
414 }
415 this->mutex->unlock(this->mutex);
416
417 return transaction;
418 }
419
420 METHOD(dhcp_socket_t, release, void,
421 private_dhcp_socket_t *this, dhcp_transaction_t *transaction)
422 {
423 dhcp_option_t *option;
424 dhcp_t dhcp;
425 host_t *release, *server;
426 chunk_t chunk;
427 int optlen;
428
429 optlen = prepare_dhcp(this, transaction, DHCP_RELEASE, &dhcp);
430
431 release = transaction->get_address(transaction);
432 server = transaction->get_server(transaction);
433 if (!release || !server)
434 {
435 return;
436 }
437 DBG1(DBG_CFG, "sending DHCP RELEASE for %H to %H", release, server);
438
439 chunk = release->get_address(release);
440 memcpy(&dhcp.client_address, chunk.ptr,
441 min(chunk.len, sizeof(dhcp.client_address)));
442
443 option = (dhcp_option_t*)&dhcp.options[optlen];
444 option->type = DHCP_SERVER_ID;
445 option->len = 4;
446 chunk = server->get_address(server);
447 memcpy(option->data, chunk.ptr, min(chunk.len, option->len));
448 optlen += sizeof(dhcp_option_t) + option->len;
449
450 dhcp.options[optlen++] = DHCP_OPTEND;
451
452 if (!send_dhcp(this, transaction, &dhcp, optlen))
453 {
454 DBG1(DBG_CFG, "sending DHCP RELEASE failed: %s", strerror(errno));
455 }
456 }
457
458 /**
459 * Handle a DHCP OFFER
460 */
461 static void handle_offer(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
462 {
463 dhcp_transaction_t *transaction = NULL;
464 enumerator_t *enumerator;
465 host_t *offer, *server = NULL;
466
467 offer = host_create_from_chunk(AF_INET,
468 chunk_from_thing(dhcp->your_address), 0);
469
470 this->mutex->lock(this->mutex);
471 enumerator = this->discover->create_enumerator(this->discover);
472 while (enumerator->enumerate(enumerator, &transaction))
473 {
474 if (transaction->get_id(transaction) == dhcp->transaction_id)
475 {
476 this->discover->remove_at(this->discover, enumerator);
477 this->request->insert_last(this->request, transaction);
478 break;
479 }
480 }
481 enumerator->destroy(enumerator);
482
483 if (transaction)
484 {
485 int optsize, optpos = 0, pos;
486 dhcp_option_t *option;
487
488 while (optlen > sizeof(dhcp_option_t))
489 {
490 option = (dhcp_option_t*)&dhcp->options[optpos];
491 optsize = sizeof(dhcp_option_t) + option->len;
492 if (option->type == DHCP_OPTEND || optlen < optsize)
493 {
494 break;
495 }
496 if (option->type == DHCP_DNS_SERVER ||
497 option->type == DHCP_NBNS_SERVER)
498 {
499 for (pos = 0; pos + 4 <= option->len; pos += 4)
500 {
501 transaction->add_attribute(transaction, option->type ==
502 DHCP_DNS_SERVER ? INTERNAL_IP4_DNS : INTERNAL_IP4_NBNS,
503 chunk_create((char*)&option->data[pos], 4));
504 }
505 }
506 if (!server && option->type == DHCP_SERVER_ID && option->len == 4)
507 {
508 server = host_create_from_chunk(AF_INET,
509 chunk_create(option->data, 4), DHCP_SERVER_PORT);
510 }
511 optlen -= optsize;
512 optpos += optsize;
513 }
514 if (!server)
515 {
516 server = host_create_from_chunk(AF_INET,
517 chunk_from_thing(dhcp->server_address), DHCP_SERVER_PORT);
518 }
519 DBG1(DBG_CFG, "received DHCP OFFER %H from %H", offer, server);
520 transaction->set_address(transaction, offer->clone(offer));
521 transaction->set_server(transaction, server);
522 }
523 this->mutex->unlock(this->mutex);
524 this->condvar->broadcast(this->condvar);
525 offer->destroy(offer);
526 }
527
528 /**
529 * Handle a DHCP ACK
530 */
531 static void handle_ack(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
532 {
533 dhcp_transaction_t *transaction;
534 enumerator_t *enumerator;
535 host_t *offer;
536
537 offer = host_create_from_chunk(AF_INET,
538 chunk_from_thing(dhcp->your_address), 0);
539
540 this->mutex->lock(this->mutex);
541 enumerator = this->request->create_enumerator(this->request);
542 while (enumerator->enumerate(enumerator, &transaction))
543 {
544 if (transaction->get_id(transaction) == dhcp->transaction_id)
545 {
546 DBG1(DBG_CFG, "received DHCP ACK for %H", offer);
547 this->request->remove_at(this->request, enumerator);
548 this->completed->insert_last(this->completed, transaction);
549 break;
550 }
551 }
552 enumerator->destroy(enumerator);
553 this->mutex->unlock(this->mutex);
554 this->condvar->broadcast(this->condvar);
555 offer->destroy(offer);
556 }
557
558 /**
559 * Receive DHCP responses
560 */
561 static job_requeue_t receive_dhcp(private_dhcp_socket_t *this)
562 {
563 struct sockaddr_ll addr;
564 socklen_t addr_len = sizeof(addr);
565 struct __attribute__((packed)) {
566 struct iphdr ip;
567 struct udphdr udp;
568 dhcp_t dhcp;
569 } packet;
570 int oldstate, optlen, origoptlen, optsize, optpos = 0;
571 ssize_t len;
572 dhcp_option_t *option;
573
574 oldstate = thread_cancelability(TRUE);
575 len = recvfrom(this->receive, &packet, sizeof(packet), 0,
576 (struct sockaddr*)&addr, &addr_len);
577 thread_cancelability(oldstate);
578
579 if (len >= sizeof(struct iphdr) + sizeof(struct udphdr) +
580 offsetof(dhcp_t, options))
581 {
582 origoptlen = optlen = len - sizeof(struct iphdr) +
583 sizeof(struct udphdr) + offsetof(dhcp_t, options);
584 while (optlen > sizeof(dhcp_option_t))
585 {
586 option = (dhcp_option_t*)&packet.dhcp.options[optpos];
587 optsize = sizeof(dhcp_option_t) + option->len;
588 if (option->type == DHCP_OPTEND || optlen < optsize)
589 {
590 break;
591 }
592 if (option->type == DHCP_MESSAGE_TYPE && option->len == 1)
593 {
594 switch (option->data[0])
595 {
596 case DHCP_OFFER:
597 handle_offer(this, &packet.dhcp, origoptlen);
598 break;
599 case DHCP_ACK:
600 handle_ack(this, &packet.dhcp, origoptlen);
601 default:
602 break;
603 }
604 break;
605 }
606 optlen -= optsize;
607 optpos += optsize;
608 }
609 }
610 return JOB_REQUEUE_DIRECT;
611 }
612
613 METHOD(dhcp_socket_t, destroy, void,
614 private_dhcp_socket_t *this)
615 {
616 if (this->job)
617 {
618 this->job->cancel(this->job);
619 }
620 while (this->waiting)
621 {
622 this->condvar->signal(this->condvar);
623 }
624 if (this->send > 0)
625 {
626 close(this->send);
627 }
628 if (this->receive > 0)
629 {
630 close(this->receive);
631 }
632 this->mutex->destroy(this->mutex);
633 this->condvar->destroy(this->condvar);
634 this->discover->destroy_offset(this->discover,
635 offsetof(dhcp_transaction_t, destroy));
636 this->request->destroy_offset(this->request,
637 offsetof(dhcp_transaction_t, destroy));
638 this->completed->destroy_offset(this->completed,
639 offsetof(dhcp_transaction_t, destroy));
640 DESTROY_IF(this->rng);
641 DESTROY_IF(this->dst);
642 free(this);
643 }
644
645 /**
646 * See header
647 */
648 dhcp_socket_t *dhcp_socket_create()
649 {
650 private_dhcp_socket_t *this;
651 struct sockaddr_in src;
652 int on = 1;
653 struct sock_filter dhcp_filter_code[] = {
654 BPF_STMT(BPF_LD+BPF_B+BPF_ABS,
655 offsetof(struct iphdr, protocol)),
656 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_UDP, 0, 16),
657 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, sizeof(struct iphdr) +
658 offsetof(struct udphdr, source)),
659 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_SERVER_PORT, 0, 14),
660 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, sizeof(struct iphdr) +
661 offsetof(struct udphdr, dest)),
662 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_CLIENT_PORT, 0, 2),
663 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_SERVER_PORT, 0, 1),
664 BPF_JUMP(BPF_JMP+BPF_JA, 0, 0, 10),
665 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
666 sizeof(struct udphdr) + offsetof(dhcp_t, opcode)),
667 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, BOOTREPLY, 0, 8),
668 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
669 sizeof(struct udphdr) + offsetof(dhcp_t, hw_type)),
670 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARPHRD_ETHER, 0, 6),
671 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
672 sizeof(struct udphdr) + offsetof(dhcp_t, hw_addr_len)),
673 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 6, 0, 4),
674 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, sizeof(struct iphdr) +
675 sizeof(struct udphdr) + offsetof(dhcp_t, magic_cookie)),
676 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x63825363, 0, 2),
677 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
678 BPF_STMT(BPF_RET+BPF_A, 0),
679 BPF_STMT(BPF_RET+BPF_K, 0),
680 };
681 struct sock_fprog dhcp_filter = {
682 sizeof(dhcp_filter_code) / sizeof(struct sock_filter),
683 dhcp_filter_code,
684 };
685
686 INIT(this,
687 .public = {
688 .enroll = _enroll,
689 .release = _release,
690 .destroy = _destroy,
691 },
692 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
693 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
694 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
695 .discover = linked_list_create(),
696 .request = linked_list_create(),
697 .completed = linked_list_create(),
698 );
699
700 if (!this->rng)
701 {
702 DBG1(DBG_CFG, "unable to create RNG");
703 destroy(this);
704 return NULL;
705 }
706 this->identity_lease = lib->settings->get_bool(lib->settings,
707 "charon.plugins.dhcp.identity_lease", FALSE);
708 this->dst = host_create_from_string(lib->settings->get_str(lib->settings,
709 "charon.plugins.dhcp.server", "255.255.255.255"),
710 DHCP_SERVER_PORT);
711 if (!this->dst)
712 {
713 DBG1(DBG_CFG, "configured DHCP server address invalid");
714 destroy(this);
715 return NULL;
716 }
717
718 this->send = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
719 if (this->send == -1)
720 {
721 DBG1(DBG_CFG, "unable to create DHCP send socket: %s", strerror(errno));
722 destroy(this);
723 return NULL;
724 }
725 if (setsockopt(this->send, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
726 {
727 DBG1(DBG_CFG, "unable to reuse DHCP socket address: %s", strerror(errno));
728 destroy(this);
729 return NULL;
730 }
731 if (setsockopt(this->send, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1)
732 {
733 DBG1(DBG_CFG, "unable to broadcast on DHCP socket: %s", strerror(errno));
734 destroy(this);
735 return NULL;
736 }
737 src.sin_family = AF_INET;
738 src.sin_port = htons(DHCP_CLIENT_PORT);
739 src.sin_addr.s_addr = INADDR_ANY;
740 if (bind(this->send, (struct sockaddr*)&src, sizeof(src)) == -1)
741 {
742 DBG1(DBG_CFG, "unable to bind DHCP send socket: %s", strerror(errno));
743 destroy(this);
744 return NULL;
745 }
746
747 this->receive = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
748 if (this->receive == -1)
749 {
750 DBG1(DBG_NET, "opening DHCP receive socket failed: %s", strerror(errno));
751 destroy(this);
752 return NULL;
753 }
754 if (setsockopt(this->receive, SOL_SOCKET, SO_ATTACH_FILTER,
755 &dhcp_filter, sizeof(dhcp_filter)) < 0)
756 {
757 DBG1(DBG_CFG, "installing DHCP socket filter failed: %s",
758 strerror(errno));
759 destroy(this);
760 return NULL;
761 }
762
763 this->job = callback_job_create((callback_job_cb_t)receive_dhcp,
764 this, NULL, NULL);
765 lib->processor->queue_job(lib->processor, (job_t*)this->job);
766
767 return &this->public;
768 }
769