b789e11eced491ae234cad3b083319907714346b
[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 disover 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;
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 DBG1(DBG_CFG, "received DHCP OFFER %H from %H", offer, server);
458
459 this->mutex->lock(this->mutex);
460 enumerator = this->discover->create_enumerator(this->discover);
461 while (enumerator->enumerate(enumerator, &transaction))
462 {
463 if (transaction->get_id(transaction) == dhcp->transaction_id)
464 {
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 this->mutex->unlock(this->mutex);
474 this->condvar->broadcast(this->condvar);
475 offer->destroy(offer);
476 server->destroy(server);
477 }
478
479 /**
480 * Handle a DHCP ACK
481 */
482 static void handle_ack(private_dhcp_socket_t *this, dhcp_t *dhcp, int optlen)
483 {
484 dhcp_transaction_t *transaction;
485 enumerator_t *enumerator;
486 host_t *offer;
487
488 offer = host_create_from_chunk(AF_INET,
489 chunk_from_thing(dhcp->your_address), 0);
490 DBG1(DBG_CFG, "received DHCP ACK for %H", offer);
491
492 this->mutex->lock(this->mutex);
493 enumerator = this->request->create_enumerator(this->request);
494 while (enumerator->enumerate(enumerator, &transaction))
495 {
496 if (transaction->get_id(transaction) == dhcp->transaction_id)
497 {
498 this->request->remove_at(this->request, enumerator);
499 this->completed->insert_last(this->completed, transaction);
500 break;
501 }
502 }
503 enumerator->destroy(enumerator);
504 this->mutex->unlock(this->mutex);
505 this->condvar->broadcast(this->condvar);
506 offer->destroy(offer);
507 }
508
509 /**
510 * Receive DHCP responses
511 */
512 static job_requeue_t receive_dhcp(private_dhcp_socket_t *this)
513 {
514 struct sockaddr_ll addr;
515 socklen_t addr_len = sizeof(addr);
516 struct __attribute__((packed)) {
517 struct iphdr ip;
518 struct udphdr udp;
519 dhcp_t dhcp;
520 } packet;
521 int oldstate, optlen, origoptlen, optsize, optpos = 0;
522 ssize_t len;
523 dhcp_option_t *option;
524
525 oldstate = thread_cancelability(TRUE);
526 len = recvfrom(this->receive, &packet, sizeof(packet), 0,
527 (struct sockaddr*)&addr, &addr_len);
528 thread_cancelability(oldstate);
529
530 if (len >= sizeof(struct iphdr) + sizeof(struct udphdr) +
531 offsetof(dhcp_t, options))
532 {
533 origoptlen = optlen = len - sizeof(struct iphdr) +
534 sizeof(struct udphdr) + offsetof(dhcp_t, options);
535 while (optlen > sizeof(dhcp_option_t))
536 {
537 option = (dhcp_option_t*)&packet.dhcp.options[optpos];
538 optsize = sizeof(dhcp_option_t) + option->len;
539 if (option->type == DHCP_OPTEND || optlen < optsize)
540 {
541 break;
542 }
543 if (option->type == DHCP_MESSAGE_TYPE && option->len == 1)
544 {
545 switch (option->data[0])
546 {
547 case DHCP_OFFER:
548 handle_offer(this, &packet.dhcp, origoptlen);
549 break;
550 case DHCP_ACK:
551 handle_ack(this, &packet.dhcp, origoptlen);
552 default:
553 break;
554 }
555 break;
556 }
557 optlen -= optsize;
558 optpos += optsize;
559 }
560 }
561 return JOB_REQUEUE_DIRECT;
562 }
563
564 METHOD(dhcp_socket_t, destroy, void,
565 private_dhcp_socket_t *this)
566 {
567 if (this->job)
568 {
569 this->job->cancel(this->job);
570 }
571 while (this->waiting)
572 {
573 this->condvar->signal(this->condvar);
574 }
575 if (this->send > 0)
576 {
577 close(this->send);
578 }
579 if (this->receive > 0)
580 {
581 close(this->receive);
582 }
583 this->mutex->destroy(this->mutex);
584 this->condvar->destroy(this->condvar);
585 this->discover->destroy_offset(this->discover,
586 offsetof(dhcp_transaction_t, destroy));
587 this->request->destroy_offset(this->request,
588 offsetof(dhcp_transaction_t, destroy));
589 this->completed->destroy_offset(this->completed,
590 offsetof(dhcp_transaction_t, destroy));
591 DESTROY_IF(this->rng);
592 DESTROY_IF(this->dst);
593 free(this);
594 }
595
596 /**
597 * See header
598 */
599 dhcp_socket_t *dhcp_socket_create()
600 {
601 private_dhcp_socket_t *this;
602 struct sockaddr_in src;
603 int on = 1;
604 struct sock_filter dhcp_filter_code[] = {
605 BPF_STMT(BPF_LD+BPF_B+BPF_ABS,
606 offsetof(struct iphdr, protocol)),
607 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_UDP, 0, 14),
608 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, sizeof(struct iphdr) +
609 offsetof(struct udphdr, source)),
610 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_SERVER_PORT, 0, 12),
611 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, sizeof(struct iphdr) +
612 offsetof(struct udphdr, dest)),
613 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, DHCP_CLIENT_PORT, 0, 10),
614 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
615 sizeof(struct udphdr) + offsetof(dhcp_t, opcode)),
616 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, BOOTREPLY, 0, 8),
617 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
618 sizeof(struct udphdr) + offsetof(dhcp_t, hw_type)),
619 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARPHRD_ETHER, 0, 6),
620 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, sizeof(struct iphdr) +
621 sizeof(struct udphdr) + offsetof(dhcp_t, hw_addr_len)),
622 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 6, 0, 4),
623 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, sizeof(struct iphdr) +
624 sizeof(struct udphdr) + offsetof(dhcp_t, magic_cookie)),
625 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x63825363, 0, 2),
626 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
627 BPF_STMT(BPF_RET+BPF_A, 0),
628 BPF_STMT(BPF_RET+BPF_K, 0),
629 };
630 struct sock_fprog dhcp_filter = {
631 sizeof(dhcp_filter_code) / sizeof(struct sock_filter),
632 dhcp_filter_code,
633 };
634
635 INIT(this,
636 .public = {
637 .enroll = _enroll,
638 .release = _release,
639 .destroy = _destroy,
640 },
641 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
642 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
643 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
644 .discover = linked_list_create(),
645 .request = linked_list_create(),
646 .completed = linked_list_create(),
647 );
648
649 if (!this->rng)
650 {
651 DBG1(DBG_CFG, "unable to create RNG");
652 destroy(this);
653 return NULL;
654 }
655 this->identity_lease = lib->settings->get_bool(lib->settings,
656 "charon.plugins.dhcp.identity_lease", FALSE);
657 this->dst = host_create_from_string(lib->settings->get_str(lib->settings,
658 "charon.plugins.dhcp.server", "255.255.255.255"),
659 DHCP_SERVER_PORT);
660 if (!this->dst)
661 {
662 DBG1(DBG_CFG, "configured DHCP server address invalid");
663 destroy(this);
664 return NULL;
665 }
666
667 this->send = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
668 if (this->send == -1)
669 {
670 DBG1(DBG_CFG, "unable to create DHCP send socket: %s", strerror(errno));
671 destroy(this);
672 return NULL;
673 }
674 if (setsockopt(this->send, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
675 {
676 DBG1(DBG_CFG, "unable to reuse DHCP socket address: %s", strerror(errno));
677 destroy(this);
678 return NULL;
679 }
680 if (setsockopt(this->send, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1)
681 {
682 DBG1(DBG_CFG, "unable to broadcast on DHCP socket: %s", strerror(errno));
683 destroy(this);
684 return NULL;
685 }
686 src.sin_family = AF_INET;
687 src.sin_port = htons(DHCP_CLIENT_PORT);
688 src.sin_addr.s_addr = INADDR_ANY;
689 if (bind(this->send, (struct sockaddr*)&src, sizeof(src)) == -1)
690 {
691 DBG1(DBG_CFG, "unable to bind DHCP send socket: %s", strerror(errno));
692 destroy(this);
693 return NULL;
694 }
695
696 this->receive = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
697 if (this->receive == -1)
698 {
699 DBG1(DBG_NET, "opening DHCP receive socket failed: %s", strerror(errno));
700 destroy(this);
701 return NULL;
702 }
703 if (setsockopt(this->receive, SOL_SOCKET, SO_ATTACH_FILTER,
704 &dhcp_filter, sizeof(dhcp_filter)) < 0)
705 {
706 DBG1(DBG_CFG, "installing DHCP socket filter failed: %s",
707 strerror(errno));
708 destroy(this);
709 return NULL;
710 }
711
712 this->job = callback_job_create((callback_job_cb_t)receive_dhcp,
713 this, NULL, NULL);
714 charon->processor->queue_job(charon->processor, (job_t*)this->job);
715
716 return &this->public;
717 }
718