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