fixed IKE_SA reestablishment after DPD using port 500
[strongswan.git] / src / charon / sa / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Implementation of ike_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include <sys/time.h>
26 #include <string.h>
27 #include <printf.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30
31 #include "ike_sa.h"
32
33 #include <library.h>
34 #include <daemon.h>
35 #include <utils/linked_list.h>
36 #include <utils/lexparser.h>
37 #include <crypto/diffie_hellman.h>
38 #include <crypto/prf_plus.h>
39 #include <crypto/crypters/crypter.h>
40 #include <crypto/hashers/hasher.h>
41 #include <encoding/payloads/sa_payload.h>
42 #include <encoding/payloads/nonce_payload.h>
43 #include <encoding/payloads/ke_payload.h>
44 #include <encoding/payloads/delete_payload.h>
45 #include <encoding/payloads/transform_substructure.h>
46 #include <encoding/payloads/transform_attribute.h>
47 #include <encoding/payloads/ts_payload.h>
48 #include <sa/task_manager.h>
49 #include <sa/tasks/ike_init.h>
50 #include <sa/tasks/ike_natd.h>
51 #include <sa/tasks/ike_mobike.h>
52 #include <sa/tasks/ike_auth.h>
53 #include <sa/tasks/ike_config.h>
54 #include <sa/tasks/ike_cert.h>
55 #include <sa/tasks/ike_rekey.h>
56 #include <sa/tasks/ike_reauth.h>
57 #include <sa/tasks/ike_delete.h>
58 #include <sa/tasks/ike_dpd.h>
59 #include <sa/tasks/child_create.h>
60 #include <sa/tasks/child_delete.h>
61 #include <sa/tasks/child_rekey.h>
62 #include <processing/jobs/retransmit_job.h>
63 #include <processing/jobs/delete_ike_sa_job.h>
64 #include <processing/jobs/send_dpd_job.h>
65 #include <processing/jobs/send_keepalive_job.h>
66 #include <processing/jobs/rekey_ike_sa_job.h>
67
68
69 #ifndef RESOLV_CONF
70 #define RESOLV_CONF "/etc/resolv.conf"
71 #endif
72
73 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DELETING,
74 "CREATED",
75 "CONNECTING",
76 "ESTABLISHED",
77 "REKEYING",
78 "DELETING",
79 );
80
81 typedef struct private_ike_sa_t private_ike_sa_t;
82
83 /**
84 * Private data of an ike_sa_t object.
85 */
86 struct private_ike_sa_t {
87
88 /**
89 * Public members
90 */
91 ike_sa_t public;
92
93 /**
94 * Identifier for the current IKE_SA.
95 */
96 ike_sa_id_t *ike_sa_id;
97
98 /**
99 * unique numerical ID for this IKE_SA.
100 */
101 u_int32_t unique_id;
102
103 /**
104 * Current state of the IKE_SA
105 */
106 ike_sa_state_t state;
107
108 /**
109 * IKE configuration used to set up this IKE_SA
110 */
111 ike_cfg_t *ike_cfg;
112
113 /**
114 * Peer and authentication information to establish IKE_SA.
115 */
116 peer_cfg_t *peer_cfg;
117
118 /**
119 * Juggles tasks to process messages
120 */
121 task_manager_t *task_manager;
122
123 /**
124 * Address of local host
125 */
126 host_t *my_host;
127
128 /**
129 * Address of remote host
130 */
131 host_t *other_host;
132
133 /**
134 * Identification used for us
135 */
136 identification_t *my_id;
137
138 /**
139 * Identification used for other
140 */
141 identification_t *other_id;
142
143 /**
144 * CA that issued the certificate of other
145 */
146 ca_info_t *other_ca;
147
148 /**
149 * set of extensions the peer supports
150 */
151 ike_extension_t extensions;
152
153 /**
154 * set of condition flags currently enabled for this IKE_SA
155 */
156 ike_condition_t conditions;
157
158 /**
159 * Linked List containing the child sa's of the current IKE_SA.
160 */
161 linked_list_t *child_sas;
162
163 /**
164 * crypter for inbound traffic
165 */
166 crypter_t *crypter_in;
167
168 /**
169 * crypter for outbound traffic
170 */
171 crypter_t *crypter_out;
172
173 /**
174 * Signer for inbound traffic
175 */
176 signer_t *signer_in;
177
178 /**
179 * Signer for outbound traffic
180 */
181 signer_t *signer_out;
182
183 /**
184 * Multi purpose prf, set key, use it, forget it
185 */
186 prf_t *prf;
187
188 /**
189 * Prf function for derivating keymat child SAs
190 */
191 prf_t *child_prf;
192
193 /**
194 * Key to build outging authentication data (SKp)
195 */
196 chunk_t skp_build;
197
198 /**
199 * Key to verify incoming authentication data (SKp)
200 */
201 chunk_t skp_verify;
202
203 /**
204 * Virtual IP on local host, if any
205 */
206 host_t *my_virtual_ip;
207
208 /**
209 * Virtual IP on remote host, if any
210 */
211 host_t *other_virtual_ip;
212
213 /**
214 * List of DNS servers installed by us
215 */
216 linked_list_t *dns_servers;
217
218 /**
219 * list of peers additional addresses, transmitted via MOBIKE
220 */
221 linked_list_t *additional_addresses;
222
223 /**
224 * Timestamps for this IKE_SA
225 */
226 struct {
227 /** last IKE message received */
228 u_int32_t inbound;
229 /** last IKE message sent */
230 u_int32_t outbound;
231 /** when IKE_SA became established */
232 u_int32_t established;
233 /** when IKE_SA gets rekeyed */
234 u_int32_t rekey;
235 /** when IKE_SA gets deleted */
236 u_int32_t delete;
237 } time;
238
239 /**
240 * how many times we have retried so far (keyingtries)
241 */
242 u_int32_t keyingtry;
243 };
244
245 /**
246 * get the time of the latest traffic processed by the kernel
247 */
248 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
249 {
250 iterator_t *iterator;
251 child_sa_t *child_sa;
252 time_t latest = 0, use_time;
253
254 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
255 while (iterator->iterate(iterator, (void**)&child_sa))
256 {
257 if (child_sa->get_use_time(child_sa, inbound, &use_time) == SUCCESS)
258 {
259 latest = max(latest, use_time);
260 }
261 }
262 iterator->destroy(iterator);
263
264 if (inbound)
265 {
266 return max(this->time.inbound, latest);
267 }
268 else
269 {
270 return max(this->time.outbound, latest);
271 }
272 }
273
274 /**
275 * Implementation of ike_sa_t.get_unique_id
276 */
277 static u_int32_t get_unique_id(private_ike_sa_t *this)
278 {
279 return this->unique_id;
280 }
281
282 /**
283 * Implementation of ike_sa_t.get_name.
284 */
285 static char *get_name(private_ike_sa_t *this)
286 {
287 if (this->peer_cfg)
288 {
289 return this->peer_cfg->get_name(this->peer_cfg);
290 }
291 return "(unnamed)";
292 }
293
294
295 /**
296 * Implementation of ike_sa_t.get_stats.
297 */
298 static void get_stats(private_ike_sa_t *this, u_int32_t *next_rekeying)
299 {
300 if (next_rekeying)
301 {
302 *next_rekeying = this->time.rekey;
303 }
304 }
305
306 /**
307 * Implementation of ike_sa_t.get_my_host.
308 */
309 static host_t *get_my_host(private_ike_sa_t *this)
310 {
311 return this->my_host;
312 }
313
314 /**
315 * Implementation of ike_sa_t.set_my_host.
316 */
317 static void set_my_host(private_ike_sa_t *this, host_t *me)
318 {
319 DESTROY_IF(this->my_host);
320 this->my_host = me;
321 }
322
323 /**
324 * Implementation of ike_sa_t.get_other_host.
325 */
326 static host_t *get_other_host(private_ike_sa_t *this)
327 {
328 return this->other_host;
329 }
330
331 /**
332 * Implementation of ike_sa_t.set_other_host.
333 */
334 static void set_other_host(private_ike_sa_t *this, host_t *other)
335 {
336 DESTROY_IF(this->other_host);
337 this->other_host = other;
338 }
339
340 /**
341 * Implementation of ike_sa_t.get_peer_cfg
342 */
343 static peer_cfg_t* get_peer_cfg(private_ike_sa_t *this)
344 {
345 return this->peer_cfg;
346 }
347
348 /**
349 * Implementation of ike_sa_t.set_peer_cfg
350 */
351 static void set_peer_cfg(private_ike_sa_t *this, peer_cfg_t *peer_cfg)
352 {
353 peer_cfg->get_ref(peer_cfg);
354 this->peer_cfg = peer_cfg;
355
356 if (this->ike_cfg == NULL)
357 {
358 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
359 this->ike_cfg->get_ref(this->ike_cfg);
360 }
361
362 /* apply values, so we are ready to initate/acquire */
363 if (this->my_host->is_anyaddr(this->my_host))
364 {
365 host_t *me = this->ike_cfg->get_my_host(this->ike_cfg);
366 set_my_host(this, me->clone(me));
367 }
368 if (this->other_host->is_anyaddr(this->other_host))
369 {
370 host_t *other = this->ike_cfg->get_other_host(this->ike_cfg);
371 set_other_host(this, other->clone(other));
372 }
373 /* apply IDs if they are not already set */
374 if (this->my_id->contains_wildcards(this->my_id))
375 {
376 DESTROY_IF(this->my_id);
377 this->my_id = this->peer_cfg->get_my_id(this->peer_cfg);
378 this->my_id = this->my_id->clone(this->my_id);
379 }
380 if (this->other_id->contains_wildcards(this->other_id))
381 {
382 DESTROY_IF(this->other_id);
383 this->other_id = this->peer_cfg->get_other_id(this->peer_cfg);
384 this->other_id = this->other_id->clone(this->other_id);
385 }
386 }
387
388 /**
389 * Implementation of ike_sa_t.send_keepalive
390 */
391 static void send_keepalive(private_ike_sa_t *this)
392 {
393 send_keepalive_job_t *job;
394 time_t last_out, now, diff;
395
396 last_out = get_use_time(this, FALSE);
397 now = time(NULL);
398
399 diff = now - last_out;
400
401 if (diff >= KEEPALIVE_INTERVAL)
402 {
403 packet_t *packet;
404 chunk_t data;
405
406 packet = packet_create();
407 packet->set_source(packet, this->my_host->clone(this->my_host));
408 packet->set_destination(packet, this->other_host->clone(this->other_host));
409 data.ptr = malloc(1);
410 data.ptr[0] = 0xFF;
411 data.len = 1;
412 packet->set_data(packet, data);
413 charon->sender->send(charon->sender, packet);
414 DBG1(DBG_IKE, "sending keep alive");
415 diff = 0;
416 }
417 job = send_keepalive_job_create(this->ike_sa_id);
418 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
419 (KEEPALIVE_INTERVAL - diff) * 1000);
420 }
421
422 /**
423 * Implementation of ike_sa_t.get_ike_cfg
424 */
425 static ike_cfg_t *get_ike_cfg(private_ike_sa_t *this)
426 {
427 return this->ike_cfg;
428 }
429
430 /**
431 * Implementation of ike_sa_t.set_ike_cfg
432 */
433 static void set_ike_cfg(private_ike_sa_t *this, ike_cfg_t *ike_cfg)
434 {
435 ike_cfg->get_ref(ike_cfg);
436 this->ike_cfg = ike_cfg;
437 }
438 /**
439 * Implementation of ike_sa_t.enable_extension.
440 */
441 static void enable_extension(private_ike_sa_t *this, ike_extension_t extension)
442 {
443 this->extensions |= extension;
444 }
445
446 /**
447 * Implementation of ike_sa_t.has_extension.
448 */
449 static bool supports_extension(private_ike_sa_t *this, ike_extension_t extension)
450 {
451 return (this->extensions & extension) != FALSE;
452 }
453
454 /**
455 * Implementation of ike_sa_t.has_condition.
456 */
457 static bool has_condition(private_ike_sa_t *this, ike_condition_t condition)
458 {
459 return (this->conditions & condition) != FALSE;
460 }
461
462 /**
463 * Implementation of ike_sa_t.enable_condition.
464 */
465 static void set_condition(private_ike_sa_t *this, ike_condition_t condition,
466 bool enable)
467 {
468 if (has_condition(this, condition) != enable)
469 {
470 if (enable)
471 {
472 this->conditions |= condition;
473 switch (condition)
474 {
475 case COND_STALE:
476 DBG1(DBG_IKE, "no route to %H, setting IKE_SA to stale",
477 this->other_host);
478 break;
479 case COND_NAT_HERE:
480 DBG1(DBG_IKE, "local host is behind NAT, sending keep alives");
481 this->conditions |= COND_NAT_ANY;
482 send_keepalive(this);
483 break;
484 case COND_NAT_THERE:
485 DBG1(DBG_IKE, "remote host is behind NAT");
486 this->conditions |= COND_NAT_ANY;
487 break;
488 default:
489 break;
490 }
491 }
492 else
493 {
494 this->conditions &= ~condition;
495 switch (condition)
496 {
497 case COND_STALE:
498 DBG1(DBG_IKE, "new route to %H found", this->other_host);
499 break;
500 case COND_NAT_HERE:
501 case COND_NAT_THERE:
502 set_condition(this, COND_NAT_ANY,
503 has_condition(this, COND_NAT_HERE) ||
504 has_condition(this, COND_NAT_THERE));
505 break;
506 default:
507 break;
508 }
509 }
510 }
511 }
512
513 /**
514 * Implementation of ike_sa_t.send_dpd
515 */
516 static status_t send_dpd(private_ike_sa_t *this)
517 {
518 send_dpd_job_t *job;
519 time_t diff, delay;
520
521 delay = this->peer_cfg->get_dpd_delay(this->peer_cfg);
522
523 if (delay == 0)
524 {
525 /* DPD disabled */
526 return SUCCESS;
527 }
528
529 if (this->task_manager->busy(this->task_manager))
530 {
531 /* an exchange is in the air, no need to start a DPD check */
532 diff = 0;
533 }
534 else
535 {
536 /* check if there was any inbound traffic */
537 time_t last_in, now;
538 last_in = get_use_time(this, TRUE);
539 now = time(NULL);
540 diff = now - last_in;
541 if (diff >= delay)
542 {
543 /* to long ago, initiate dead peer detection */
544 task_t *task;
545
546 task = (task_t*)ike_dpd_create(TRUE);
547 diff = 0;
548 DBG1(DBG_IKE, "sending DPD request");
549
550 this->task_manager->queue_task(this->task_manager, task);
551 this->task_manager->initiate(this->task_manager);
552 }
553 }
554 /* recheck in "interval" seconds */
555 job = send_dpd_job_create(this->ike_sa_id);
556 charon->scheduler->schedule_job(charon->scheduler, (job_t*)job,
557 (delay - diff) * 1000);
558 return SUCCESS;
559 }
560
561 /**
562 * Implementation of ike_sa_t.get_state.
563 */
564 static ike_sa_state_t get_state(private_ike_sa_t *this)
565 {
566 return this->state;
567 }
568
569 /**
570 * Implementation of ike_sa_t.set_state.
571 */
572 static void set_state(private_ike_sa_t *this, ike_sa_state_t state)
573 {
574 DBG1(DBG_IKE, "IKE_SA state change: %N => %N",
575 ike_sa_state_names, this->state,
576 ike_sa_state_names, state);
577
578 switch (state)
579 {
580 case IKE_ESTABLISHED:
581 {
582 if (this->state == IKE_CONNECTING)
583 {
584 job_t *job;
585 u_int32_t now = time(NULL);
586 u_int32_t soft, hard;
587 bool reauth;
588
589 this->time.established = now;
590 /* start DPD checks */
591 send_dpd(this);
592
593 /* schedule rekeying/reauthentication */
594 soft = this->peer_cfg->get_lifetime(this->peer_cfg, TRUE);
595 hard = this->peer_cfg->get_lifetime(this->peer_cfg, FALSE);
596 reauth = this->peer_cfg->use_reauth(this->peer_cfg);
597 DBG1(DBG_IKE, "scheduling %s in %ds, maximum lifetime %ds",
598 reauth ? "reauthentication": "rekeying", soft, hard);
599
600 if (soft)
601 {
602 this->time.rekey = now + soft;
603 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, reauth);
604 charon->scheduler->schedule_job(charon->scheduler, job,
605 soft * 1000);
606 }
607
608 if (hard)
609 {
610 this->time.delete = now + hard;
611 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
612 charon->scheduler->schedule_job(charon->scheduler, job,
613 hard * 1000);
614 }
615 }
616 break;
617 }
618 case IKE_DELETING:
619 {
620 /* delete may fail if a packet gets lost, so set a timeout */
621 job_t *job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
622 charon->scheduler->schedule_job(charon->scheduler, job,
623 HALF_OPEN_IKE_SA_TIMEOUT);
624 break;
625 }
626 default:
627 break;
628 }
629
630 this->state = state;
631 }
632
633 /**
634 * Implementation of ike_sa_t.reset
635 */
636 static void reset(private_ike_sa_t *this)
637 {
638 /* the responder ID is reset, as peer may choose another one */
639 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
640 {
641 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
642 }
643
644 set_state(this, IKE_CREATED);
645
646 this->task_manager->reset(this->task_manager);
647 }
648
649 /**
650 * Implementation of ike_sa_t.set_virtual_ip
651 */
652 static void set_virtual_ip(private_ike_sa_t *this, bool local, host_t *ip)
653 {
654 if (local)
655 {
656 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
657 if (this->my_virtual_ip)
658 {
659 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
660 charon->kernel_interface->del_ip(charon->kernel_interface,
661 this->my_virtual_ip);
662 this->my_virtual_ip->destroy(this->my_virtual_ip);
663 }
664 if (charon->kernel_interface->add_ip(charon->kernel_interface, ip,
665 this->my_host) == SUCCESS)
666 {
667 this->my_virtual_ip = ip->clone(ip);
668 }
669 else
670 {
671 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
672 this->my_virtual_ip = NULL;
673 }
674 }
675 else
676 {
677 DESTROY_IF(this->other_virtual_ip);
678 this->other_virtual_ip = ip->clone(ip);
679 }
680 }
681
682 /**
683 * Implementation of ike_sa_t.get_virtual_ip
684 */
685 static host_t* get_virtual_ip(private_ike_sa_t *this, bool local)
686 {
687 if (local)
688 {
689 return this->my_virtual_ip;
690 }
691 else
692 {
693 return this->other_virtual_ip;
694 }
695 }
696
697 /**
698 * Implementation of ike_sa_t.add_additional_address.
699 */
700 static void add_additional_address(private_ike_sa_t *this, host_t *host)
701 {
702 this->additional_addresses->insert_last(this->additional_addresses, host);
703 }
704
705 /**
706 * Implementation of ike_sa_t.create_additional_address_iterator.
707 */
708 static iterator_t* create_additional_address_iterator(private_ike_sa_t *this)
709 {
710 return this->additional_addresses->create_iterator(
711 this->additional_addresses, TRUE);
712 }
713
714 /**
715 * Update hosts, as addresses may change (NAT)
716 */
717 static void update_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
718 {
719 bool update = FALSE;
720
721 if (me == NULL)
722 {
723 me = this->my_host;
724 }
725 if (other == NULL)
726 {
727 other = this->other_host;
728 }
729
730 /* apply hosts on first received message */
731 if (this->my_host->is_anyaddr(this->my_host) ||
732 this->other_host->is_anyaddr(this->other_host))
733 {
734 set_my_host(this, me->clone(me));
735 set_other_host(this, other->clone(other));
736 update = TRUE;
737 }
738 else
739 {
740 /* update our address in any case */
741 if (!me->equals(me, this->my_host))
742 {
743 set_my_host(this, me->clone(me));
744 update = TRUE;
745 }
746
747 if (!other->equals(other, this->other_host))
748 {
749 /* update others adress if we are NOT NATed,
750 * and allow port changes if we are NATed */
751 if (!has_condition(this, COND_NAT_HERE) ||
752 other->ip_equals(other, this->other_host))
753 {
754 set_other_host(this, other->clone(other));
755 update = TRUE;
756 }
757 }
758 }
759
760 /* update all associated CHILD_SAs, if required */
761 if (update)
762 {
763 iterator_t *iterator;
764 child_sa_t *child_sa;
765
766 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
767 while (iterator->iterate(iterator, (void**)&child_sa))
768 {
769 child_sa->update_hosts(child_sa, this->my_host, this->other_host,
770 has_condition(this, COND_NAT_ANY));
771 }
772 iterator->destroy(iterator);
773 }
774 }
775
776 /**
777 * Implementation of ike_sa_t.generate
778 */
779 static status_t generate_message(private_ike_sa_t *this, message_t *message,
780 packet_t **packet)
781 {
782 this->time.outbound = time(NULL);
783 message->set_ike_sa_id(message, this->ike_sa_id);
784 message->set_destination(message, this->other_host->clone(this->other_host));
785 message->set_source(message, this->my_host->clone(this->my_host));
786 return message->generate(message, this->crypter_out, this->signer_out, packet);
787 }
788
789 /**
790 * send a notify back to the sender
791 */
792 static void send_notify_response(private_ike_sa_t *this, message_t *request,
793 notify_type_t type)
794 {
795 message_t *response;
796 packet_t *packet;
797
798 response = message_create();
799 response->set_exchange_type(response, request->get_exchange_type(request));
800 response->set_request(response, FALSE);
801 response->set_message_id(response, request->get_message_id(request));
802 response->add_notify(response, FALSE, type, chunk_empty);
803 if (this->my_host->is_anyaddr(this->my_host))
804 {
805 this->my_host->destroy(this->my_host);
806 this->my_host = request->get_destination(request);
807 this->my_host = this->my_host->clone(this->my_host);
808 }
809 if (this->other_host->is_anyaddr(this->other_host))
810 {
811 this->other_host->destroy(this->other_host);
812 this->other_host = request->get_source(request);
813 this->other_host = this->other_host->clone(this->other_host);
814 }
815 if (generate_message(this, response, &packet) == SUCCESS)
816 {
817 charon->sender->send(charon->sender, packet);
818 }
819 response->destroy(response);
820 }
821
822 /**
823 * Implementation of ike_sa_t.process_message.
824 */
825 static status_t process_message(private_ike_sa_t *this, message_t *message)
826 {
827 status_t status;
828 bool is_request;
829
830 is_request = message->get_request(message);
831
832 status = message->parse_body(message, this->crypter_in, this->signer_in);
833 if (status != SUCCESS)
834 {
835
836 if (is_request)
837 {
838 switch (status)
839 {
840 case NOT_SUPPORTED:
841 DBG1(DBG_IKE, "ciritcal unknown payloads found");
842 if (is_request)
843 {
844 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
845 }
846 break;
847 case PARSE_ERROR:
848 DBG1(DBG_IKE, "message parsing failed");
849 if (is_request)
850 {
851 send_notify_response(this, message, INVALID_SYNTAX);
852 }
853 break;
854 case VERIFY_ERROR:
855 DBG1(DBG_IKE, "message verification failed");
856 if (is_request)
857 {
858 send_notify_response(this, message, INVALID_SYNTAX);
859 }
860 break;
861 case FAILED:
862 DBG1(DBG_IKE, "integrity check failed");
863 /* ignored */
864 break;
865 case INVALID_STATE:
866 DBG1(DBG_IKE, "found encrypted message, but no keys available");
867 if (is_request)
868 {
869 send_notify_response(this, message, INVALID_SYNTAX);
870 }
871 default:
872 break;
873 }
874 }
875 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
876 exchange_type_names, message->get_exchange_type(message),
877 message->get_request(message) ? "request" : "response",
878 message->get_message_id(message));
879 return status;
880 }
881 else
882 {
883 host_t *me, *other;
884
885 me = message->get_destination(message);
886 other = message->get_source(message);
887
888 /* if this IKE_SA is virgin, we check for a config */
889 if (this->ike_cfg == NULL)
890 {
891 job_t *job;
892 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
893 me, other);
894 if (this->ike_cfg == NULL)
895 {
896 /* no config found for these hosts, destroy */
897 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
898 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
899 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
900 return DESTROY_ME;
901 }
902 /* add a timeout if peer does not establish it completely */
903 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
904 charon->scheduler->schedule_job(charon->scheduler, job,
905 HALF_OPEN_IKE_SA_TIMEOUT);
906 }
907
908 /* check if message is trustworthy, and update host information */
909 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
910 message->get_exchange_type(message) != IKE_SA_INIT)
911 {
912 update_hosts(this, me, other);
913 this->time.inbound = time(NULL);
914 }
915 return this->task_manager->process_message(this->task_manager, message);
916 }
917 }
918
919 /**
920 * Implementation of ike_sa_t.initiate.
921 */
922 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
923 {
924 task_t *task;
925
926 if (this->state == IKE_CREATED)
927 {
928
929 if (this->other_host->is_anyaddr(this->other_host))
930 {
931 child_cfg->destroy(child_cfg);
932 SIG(IKE_UP_START, "initiating IKE_SA");
933 SIG(IKE_UP_FAILED, "unable to initiate to %%any");
934 return DESTROY_ME;
935 }
936
937 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
938 this->task_manager->queue_task(this->task_manager, task);
939 task = (task_t*)ike_natd_create(&this->public, TRUE);
940 this->task_manager->queue_task(this->task_manager, task);
941 task = (task_t*)ike_cert_create(&this->public, TRUE);
942 this->task_manager->queue_task(this->task_manager, task);
943 task = (task_t*)ike_auth_create(&this->public, TRUE);
944 this->task_manager->queue_task(this->task_manager, task);
945 task = (task_t*)ike_config_create(&this->public, TRUE);
946 this->task_manager->queue_task(this->task_manager, task);
947 task = (task_t*)ike_mobike_create(&this->public, TRUE);
948 this->task_manager->queue_task(this->task_manager, task);
949 }
950
951 task = (task_t*)child_create_create(&this->public, child_cfg);
952 child_cfg->destroy(child_cfg);
953 this->task_manager->queue_task(this->task_manager, task);
954
955 return this->task_manager->initiate(this->task_manager);
956 }
957
958 /**
959 * Implementation of ike_sa_t.acquire.
960 */
961 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
962 {
963 child_cfg_t *child_cfg;
964 iterator_t *iterator;
965 child_sa_t *current, *child_sa = NULL;
966 task_t *task;
967 child_create_t *child_create;
968
969 if (this->state == IKE_DELETING)
970 {
971 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
972 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
973 "IKE_SA is deleting", reqid);
974 return FAILED;
975 }
976
977 /* find CHILD_SA */
978 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
979 while (iterator->iterate(iterator, (void**)&current))
980 {
981 if (current->get_reqid(current) == reqid)
982 {
983 child_sa = current;
984 break;
985 }
986 }
987 iterator->destroy(iterator);
988 if (!child_sa)
989 {
990 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
991 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
992 "CHILD_SA not found", reqid);
993 return FAILED;
994 }
995
996
997 if (this->state == IKE_CREATED)
998 {
999 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1000 this->task_manager->queue_task(this->task_manager, task);
1001 task = (task_t*)ike_natd_create(&this->public, TRUE);
1002 this->task_manager->queue_task(this->task_manager, task);
1003 task = (task_t*)ike_cert_create(&this->public, TRUE);
1004 this->task_manager->queue_task(this->task_manager, task);
1005 task = (task_t*)ike_auth_create(&this->public, TRUE);
1006 this->task_manager->queue_task(this->task_manager, task);
1007 task = (task_t*)ike_config_create(&this->public, TRUE);
1008 this->task_manager->queue_task(this->task_manager, task);
1009 task = (task_t*)ike_mobike_create(&this->public, TRUE);
1010 this->task_manager->queue_task(this->task_manager, task);
1011 }
1012
1013 child_cfg = child_sa->get_config(child_sa);
1014 child_create = child_create_create(&this->public, child_cfg);
1015 child_create->use_reqid(child_create, reqid);
1016 this->task_manager->queue_task(this->task_manager, (task_t*)child_create);
1017
1018 return this->task_manager->initiate(this->task_manager);
1019 }
1020
1021 /**
1022 * Implementation of ike_sa_t.route.
1023 */
1024 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
1025 {
1026 child_sa_t *child_sa;
1027 iterator_t *iterator;
1028 linked_list_t *my_ts, *other_ts;
1029 status_t status;
1030
1031 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
1032
1033 /* check if not already routed*/
1034 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1035 while (iterator->iterate(iterator, (void**)&child_sa))
1036 {
1037 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1038 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
1039 {
1040 iterator->destroy(iterator);
1041 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a config already routed");
1042 return FAILED;
1043 }
1044 }
1045 iterator->destroy(iterator);
1046
1047 switch (this->state)
1048 {
1049 case IKE_DELETING:
1050 case IKE_REKEYING:
1051 SIG(CHILD_ROUTE_FAILED,
1052 "unable to route CHILD_SA, as its IKE_SA gets deleted");
1053 return FAILED;
1054 case IKE_CREATED:
1055 case IKE_CONNECTING:
1056 case IKE_ESTABLISHED:
1057 default:
1058 break;
1059 }
1060
1061 /* install kernel policies */
1062 child_sa = child_sa_create(this->my_host, this->other_host, this->my_id,
1063 this->other_id, child_cfg, FALSE, 0);
1064
1065 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL,
1066 this->my_host);
1067 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL,
1068 this->other_host);
1069 status = child_sa->add_policies(child_sa, my_ts, other_ts,
1070 child_cfg->get_mode(child_cfg));
1071 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1072 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1073 if (status == SUCCESS)
1074 {
1075 this->child_sas->insert_last(this->child_sas, child_sa);
1076 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
1077 }
1078 else
1079 {
1080 SIG(CHILD_ROUTE_FAILED, "routing CHILD_SA failed");
1081 }
1082 return status;
1083 }
1084
1085 /**
1086 * Implementation of ike_sa_t.unroute.
1087 */
1088 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
1089 {
1090 iterator_t *iterator;
1091 child_sa_t *child_sa;
1092 bool found = FALSE;
1093
1094 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
1095
1096 /* find CHILD_SA in ROUTED state */
1097 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1098 while (iterator->iterate(iterator, (void**)&child_sa))
1099 {
1100 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1101 child_sa->get_reqid(child_sa) == reqid)
1102 {
1103 iterator->remove(iterator);
1104 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
1105 child_sa->destroy(child_sa);
1106 found = TRUE;
1107 break;
1108 }
1109 }
1110 iterator->destroy(iterator);
1111
1112 if (!found)
1113 {
1114 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
1115 return FAILED;
1116 }
1117 /* if we are not established, and we have no more routed childs, remove whole SA */
1118 if (this->state == IKE_CREATED &&
1119 this->child_sas->get_count(this->child_sas) == 0)
1120 {
1121 return DESTROY_ME;
1122 }
1123 return SUCCESS;
1124 }
1125
1126 /**
1127 * Implementation of ike_sa_t.retransmit.
1128 */
1129 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
1130 {
1131 this->time.outbound = time(NULL);
1132 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1133 {
1134 child_cfg_t *child_cfg;
1135 child_sa_t* child_sa;
1136 linked_list_t *to_route, *to_restart;
1137 iterator_t *iterator;
1138
1139 /* send a proper signal to brief interested bus listeners */
1140 switch (this->state)
1141 {
1142 case IKE_CONNECTING:
1143 {
1144 /* retry IKE_SA_INIT if we have multiple keyingtries */
1145 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1146 this->keyingtry++;
1147 if (tries == 0 || tries > this->keyingtry)
1148 {
1149 SIG(IKE_UP_FAILED, "peer not responding, trying again "
1150 "(%d/%d) in background ", this->keyingtry + 1, tries);
1151 reset(this);
1152 return this->task_manager->initiate(this->task_manager);
1153 }
1154 SIG(IKE_UP_FAILED, "establishing IKE_SA failed, peer not responding");
1155 break;
1156 }
1157 case IKE_REKEYING:
1158 SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
1159 break;
1160 case IKE_DELETING:
1161 SIG(IKE_DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
1162 break;
1163 default:
1164 break;
1165 }
1166
1167 /* summarize how we have to handle each child */
1168 to_route = linked_list_create();
1169 to_restart = linked_list_create();
1170 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1171 while (iterator->iterate(iterator, (void**)&child_sa))
1172 {
1173 child_cfg = child_sa->get_config(child_sa);
1174
1175 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1176 {
1177 /* reroute routed CHILD_SAs */
1178 to_route->insert_last(to_route, child_cfg);
1179 }
1180 else
1181 {
1182 /* use DPD action for established CHILD_SAs */
1183 switch (this->peer_cfg->get_dpd_action(this->peer_cfg))
1184 {
1185 case DPD_ROUTE:
1186 to_route->insert_last(to_route, child_cfg);
1187 break;
1188 case DPD_RESTART:
1189 to_restart->insert_last(to_restart, child_cfg);
1190 break;
1191 default:
1192 break;
1193 }
1194 }
1195 }
1196 iterator->destroy(iterator);
1197
1198 /* create a new IKE_SA if we have to route or to restart */
1199 if (to_route->get_count(to_route) || to_restart->get_count(to_restart))
1200 {
1201 private_ike_sa_t *new;
1202 task_t *task;
1203
1204 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1205 charon->ike_sa_manager, TRUE);
1206
1207 set_peer_cfg(new, this->peer_cfg);
1208 /* use actual used host, not the wildcarded one in config */
1209 new->other_host->destroy(new->other_host);
1210 new->other_host = this->other_host->clone(this->other_host);
1211 new->other_host->set_port(new->other_host, IKEV2_UDP_PORT);
1212
1213 if (this->my_virtual_ip)
1214 {
1215 set_virtual_ip(new, TRUE, this->my_virtual_ip);
1216 }
1217
1218 /* install routes */
1219 while (to_route->remove_last(to_route, (void**)&child_cfg) == SUCCESS)
1220 {
1221 route(new, child_cfg);
1222 }
1223
1224 /* restart children */
1225 if (to_restart->get_count(to_restart))
1226 {
1227 task = (task_t*)ike_init_create(&new->public, TRUE, NULL);
1228 new->task_manager->queue_task(new->task_manager, task);
1229 task = (task_t*)ike_natd_create(&new->public, TRUE);
1230 new->task_manager->queue_task(new->task_manager, task);
1231 task = (task_t*)ike_cert_create(&new->public, TRUE);
1232 new->task_manager->queue_task(new->task_manager, task);
1233 task = (task_t*)ike_config_create(&new->public, TRUE);
1234 new->task_manager->queue_task(new->task_manager, task);
1235 task = (task_t*)ike_auth_create(&new->public, TRUE);
1236 new->task_manager->queue_task(new->task_manager, task);
1237
1238 while (to_restart->remove_last(to_restart, (void**)&child_cfg) == SUCCESS)
1239 {
1240 task = (task_t*)child_create_create(&new->public, child_cfg);
1241 new->task_manager->queue_task(new->task_manager, task);
1242 }
1243 task = (task_t*)ike_mobike_create(&new->public, TRUE);
1244 new->task_manager->queue_task(new->task_manager, task);
1245 new->task_manager->initiate(new->task_manager);
1246 }
1247 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1248 }
1249 to_route->destroy(to_route);
1250 to_restart->destroy(to_restart);
1251 return DESTROY_ME;
1252 }
1253 return SUCCESS;
1254 }
1255
1256 /**
1257 * Implementation of ike_sa_t.get_prf.
1258 */
1259 static prf_t *get_prf(private_ike_sa_t *this)
1260 {
1261 return this->prf;
1262 }
1263
1264 /**
1265 * Implementation of ike_sa_t.get_prf.
1266 */
1267 static prf_t *get_child_prf(private_ike_sa_t *this)
1268 {
1269 return this->child_prf;
1270 }
1271
1272 /**
1273 * Implementation of ike_sa_t.get_skp_bild
1274 */
1275 static chunk_t get_skp_build(private_ike_sa_t *this)
1276 {
1277 return this->skp_build;
1278 }
1279
1280 /**
1281 * Implementation of ike_sa_t.get_skp_verify
1282 */
1283 static chunk_t get_skp_verify(private_ike_sa_t *this)
1284 {
1285 return this->skp_verify;
1286 }
1287
1288 /**
1289 * Implementation of ike_sa_t.get_id.
1290 */
1291 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1292 {
1293 return this->ike_sa_id;
1294 }
1295
1296 /**
1297 * Implementation of ike_sa_t.get_my_id.
1298 */
1299 static identification_t* get_my_id(private_ike_sa_t *this)
1300 {
1301 return this->my_id;
1302 }
1303
1304 /**
1305 * Implementation of ike_sa_t.set_my_id.
1306 */
1307 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1308 {
1309 DESTROY_IF(this->my_id);
1310 this->my_id = me;
1311 }
1312
1313 /**
1314 * Implementation of ike_sa_t.get_other_id.
1315 */
1316 static identification_t* get_other_id(private_ike_sa_t *this)
1317 {
1318 return this->other_id;
1319 }
1320
1321 /**
1322 * Implementation of ike_sa_t.set_other_id.
1323 */
1324 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1325 {
1326 DESTROY_IF(this->other_id);
1327 this->other_id = other;
1328 }
1329
1330 /**
1331 * Implementation of ike_sa_t.get_other_ca.
1332 */
1333 static ca_info_t* get_other_ca(private_ike_sa_t *this)
1334 {
1335 return this->other_ca;
1336 }
1337
1338 /**
1339 * Implementation of ike_sa_t.set_other_ca.
1340 */
1341 static void set_other_ca(private_ike_sa_t *this, ca_info_t *other_ca)
1342 {
1343 this->other_ca = other_ca;
1344 }
1345
1346 /**
1347 * Implementation of ike_sa_t.derive_keys.
1348 */
1349 static status_t derive_keys(private_ike_sa_t *this,
1350 proposal_t *proposal, chunk_t secret,
1351 chunk_t nonce_i, chunk_t nonce_r,
1352 bool initiator, prf_t *child_prf, prf_t *old_prf)
1353 {
1354 prf_plus_t *prf_plus;
1355 chunk_t skeyseed, key, nonces, prf_plus_seed;
1356 algorithm_t *algo;
1357 size_t key_size;
1358 crypter_t *crypter_i, *crypter_r;
1359 signer_t *signer_i, *signer_r;
1360 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1361 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1362 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1363
1364 /* Create SAs general purpose PRF first, we may use it here */
1365 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1366 {
1367 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1368 return FAILED;
1369 }
1370 this->prf = prf_create(algo->algorithm);
1371 if (this->prf == NULL)
1372 {
1373 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1374 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1375 return FAILED;
1376 }
1377
1378 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1379 nonces = chunk_cat("cc", nonce_i, nonce_r);
1380 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1381 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1382 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1383
1384 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1385 *
1386 * if we are rekeying, SKEYSEED is built on another way
1387 */
1388 if (child_prf == NULL) /* not rekeying */
1389 {
1390 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1391 this->prf->set_key(this->prf, nonces);
1392 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1393 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1394 this->prf->set_key(this->prf, skeyseed);
1395 chunk_free(&skeyseed);
1396 chunk_free(&secret);
1397 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1398 }
1399 else
1400 {
1401 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1402 * use OLD SAs PRF functions for both prf_plus and prf */
1403 secret = chunk_cat("mc", secret, nonces);
1404 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1405 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1406 old_prf->set_key(old_prf, skeyseed);
1407 chunk_free(&skeyseed);
1408 chunk_free(&secret);
1409 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1410 }
1411 chunk_free(&nonces);
1412 chunk_free(&prf_plus_seed);
1413
1414 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1415
1416 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1417 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1418 this->child_prf = prf_create(algo->algorithm);
1419 key_size = this->child_prf->get_key_size(this->child_prf);
1420 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1421 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1422 this->child_prf->set_key(this->child_prf, key);
1423 chunk_free(&key);
1424
1425 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1426 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1427 {
1428 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1429 return FAILED;
1430 }
1431 signer_i = signer_create(algo->algorithm);
1432 signer_r = signer_create(algo->algorithm);
1433 if (signer_i == NULL || signer_r == NULL)
1434 {
1435 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1436 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1437 return FAILED;
1438 }
1439 key_size = signer_i->get_key_size(signer_i);
1440
1441 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1442 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1443 signer_i->set_key(signer_i, key);
1444 chunk_free(&key);
1445
1446 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1447 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1448 signer_r->set_key(signer_r, key);
1449 chunk_free(&key);
1450
1451 if (initiator)
1452 {
1453 this->signer_in = signer_r;
1454 this->signer_out = signer_i;
1455 }
1456 else
1457 {
1458 this->signer_in = signer_i;
1459 this->signer_out = signer_r;
1460 }
1461
1462 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1463 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1464 {
1465 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1466 return FAILED;
1467 }
1468 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1469 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1470 if (crypter_i == NULL || crypter_r == NULL)
1471 {
1472 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1473 "%N (key size %d) not supported!",
1474 encryption_algorithm_names, algo->algorithm, algo->key_size);
1475 return FAILED;
1476 }
1477 key_size = crypter_i->get_key_size(crypter_i);
1478
1479 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1480 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1481 crypter_i->set_key(crypter_i, key);
1482 chunk_free(&key);
1483
1484 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1485 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1486 crypter_r->set_key(crypter_r, key);
1487 chunk_free(&key);
1488
1489 if (initiator)
1490 {
1491 this->crypter_in = crypter_r;
1492 this->crypter_out = crypter_i;
1493 }
1494 else
1495 {
1496 this->crypter_in = crypter_i;
1497 this->crypter_out = crypter_r;
1498 }
1499
1500 /* SK_pi/SK_pr used for authentication => stored for later */
1501 key_size = this->prf->get_key_size(this->prf);
1502 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1503 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1504 if (initiator)
1505 {
1506 this->skp_build = key;
1507 }
1508 else
1509 {
1510 this->skp_verify = key;
1511 }
1512 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1513 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1514 if (initiator)
1515 {
1516 this->skp_verify = key;
1517 }
1518 else
1519 {
1520 this->skp_build = key;
1521 }
1522
1523 /* all done, prf_plus not needed anymore */
1524 prf_plus->destroy(prf_plus);
1525
1526 return SUCCESS;
1527 }
1528
1529 /**
1530 * Implementation of ike_sa_t.add_child_sa.
1531 */
1532 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1533 {
1534 this->child_sas->insert_last(this->child_sas, child_sa);
1535 }
1536
1537 /**
1538 * Implementation of ike_sa_t.get_child_sa.
1539 */
1540 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1541 u_int32_t spi, bool inbound)
1542 {
1543 iterator_t *iterator;
1544 child_sa_t *current, *found = NULL;
1545
1546 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1547 while (iterator->iterate(iterator, (void**)&current))
1548 {
1549 if (current->get_spi(current, inbound) == spi &&
1550 current->get_protocol(current) == protocol)
1551 {
1552 found = current;
1553 }
1554 }
1555 iterator->destroy(iterator);
1556 return found;
1557 }
1558
1559 /**
1560 * Implementation of ike_sa_t.create_child_sa_iterator.
1561 */
1562 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1563 {
1564 return this->child_sas->create_iterator(this->child_sas, TRUE);
1565 }
1566
1567 /**
1568 * Implementation of ike_sa_t.rekey_child_sa.
1569 */
1570 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1571 {
1572 child_sa_t *child_sa;
1573 child_rekey_t *child_rekey;
1574
1575 child_sa = get_child_sa(this, protocol, spi, TRUE);
1576 if (child_sa)
1577 {
1578 child_rekey = child_rekey_create(&this->public, child_sa);
1579 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1580 return this->task_manager->initiate(this->task_manager);
1581 }
1582 return FAILED;
1583 }
1584
1585 /**
1586 * Implementation of ike_sa_t.delete_child_sa.
1587 */
1588 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1589 {
1590 child_sa_t *child_sa;
1591 child_delete_t *child_delete;
1592
1593 child_sa = get_child_sa(this, protocol, spi, TRUE);
1594 if (child_sa)
1595 {
1596 child_delete = child_delete_create(&this->public, child_sa);
1597 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1598 return this->task_manager->initiate(this->task_manager);
1599 }
1600 return FAILED;
1601 }
1602
1603 /**
1604 * Implementation of ike_sa_t.destroy_child_sa.
1605 */
1606 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1607 u_int32_t spi)
1608 {
1609 iterator_t *iterator;
1610 child_sa_t *child_sa;
1611 status_t status = NOT_FOUND;
1612
1613 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1614 while (iterator->iterate(iterator, (void**)&child_sa))
1615 {
1616 if (child_sa->get_protocol(child_sa) == protocol &&
1617 child_sa->get_spi(child_sa, TRUE) == spi)
1618 {
1619 child_sa->destroy(child_sa);
1620 iterator->remove(iterator);
1621 status = SUCCESS;
1622 break;
1623 }
1624 }
1625 iterator->destroy(iterator);
1626 return status;
1627 }
1628
1629 /**
1630 * Implementation of public_ike_sa_t.delete.
1631 */
1632 static status_t delete_(private_ike_sa_t *this)
1633 {
1634 ike_delete_t *ike_delete;
1635
1636 switch (this->state)
1637 {
1638 case IKE_ESTABLISHED:
1639 case IKE_REKEYING:
1640 ike_delete = ike_delete_create(&this->public, TRUE);
1641 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1642 return this->task_manager->initiate(this->task_manager);
1643 default:
1644 DBG1(DBG_IKE, "destroying IKE_SA in state %N without notification",
1645 ike_sa_state_names, this->state);
1646 break;
1647 }
1648 return DESTROY_ME;
1649 }
1650
1651 /**
1652 * Implementation of ike_sa_t.rekey.
1653 */
1654 static status_t rekey(private_ike_sa_t *this)
1655 {
1656 ike_rekey_t *ike_rekey;
1657
1658 ike_rekey = ike_rekey_create(&this->public, TRUE);
1659
1660 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1661 return this->task_manager->initiate(this->task_manager);
1662 }
1663
1664 /**
1665 * Implementation of ike_sa_t.reestablish
1666 */
1667 static status_t reestablish(private_ike_sa_t *this)
1668 {
1669 task_t *task;
1670
1671 task = (task_t*)ike_reauth_create(&this->public);
1672 this->task_manager->queue_task(this->task_manager, task);
1673
1674 return this->task_manager->initiate(this->task_manager);
1675 }
1676
1677 /**
1678 * Implementation of ike_sa_t.roam.
1679 */
1680 static status_t roam(private_ike_sa_t *this)
1681 {
1682 iterator_t *iterator;
1683 host_t *me, *other;
1684 ike_mobike_t *mobike;
1685
1686 /* only initiator handles address updated actively */
1687 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
1688 {
1689 return SUCCESS;
1690 }
1691
1692 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
1693 this->other_host);
1694 if (me && this->my_virtual_ip && me->ip_equals(me, this->my_virtual_ip))
1695 { /* do not roam to the virtual IP of this IKE_SA */
1696 me->destroy(me);
1697 me = NULL;
1698 }
1699
1700 if (me)
1701 {
1702 set_condition(this, COND_STALE, FALSE);
1703 /* attachment still the same? */
1704 if (me->ip_equals(me, this->my_host))
1705 {
1706 DBG2(DBG_IKE, "%H still reached through %H, no update needed",
1707 this->other_host, me);
1708 me->destroy(me);
1709 return SUCCESS;
1710 }
1711 me->set_port(me, this->my_host->get_port(this->my_host));
1712
1713 /* our attachement changed, update if we have mobike */
1714 if (supports_extension(this, EXT_MOBIKE))
1715 {
1716 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1717 mobike = ike_mobike_create(&this->public, TRUE);
1718 mobike->roam(mobike, me, NULL);
1719 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1720 return this->task_manager->initiate(this->task_manager);
1721 }
1722 DBG1(DBG_IKE, "reestablishing IKE_SA due address change");
1723 /* reestablish if not */
1724 set_my_host(this, me);
1725 return reestablish(this);
1726 }
1727
1728 /* there is nothing we can do without mobike */
1729 if (!supports_extension(this, EXT_MOBIKE))
1730 {
1731 set_condition(this, COND_STALE, TRUE);
1732 return FAILED;
1733 }
1734
1735 /* we are unable to reach the peer. Try an alternative address */
1736 iterator = create_additional_address_iterator(this);
1737 while (iterator->iterate(iterator, (void**)&other))
1738 {
1739 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
1740 other);
1741 if (me && me->ip_equals(me, this->my_virtual_ip))
1742 { /* do not roam to the virtual IP of this IKE_SA */
1743 me->destroy(me);
1744 me = NULL;
1745 }
1746
1747 if (me)
1748 {
1749 /* good, we have a new route. Use MOBIKE to update */
1750 set_condition(this, COND_STALE, FALSE);
1751 iterator->destroy(iterator);
1752 me->set_port(me, this->my_host->get_port(this->my_host));
1753 other->set_port(other, this->other_host->get_port(this->other_host));
1754 mobike = ike_mobike_create(&this->public, TRUE);
1755 mobike->roam(mobike, me, other);
1756 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1757 return this->task_manager->initiate(this->task_manager);
1758 }
1759 }
1760 iterator->destroy(iterator);
1761
1762 /* no route found to host, give up (temporary) */
1763 set_condition(this, COND_STALE, TRUE);
1764 return FAILED;
1765 }
1766
1767 /**
1768 * Implementation of ike_sa_t.inherit.
1769 */
1770 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
1771 {
1772 child_sa_t *child_sa;
1773 host_t *ip;
1774
1775 /* apply hosts and ids */
1776 this->my_host->destroy(this->my_host);
1777 this->other_host->destroy(this->other_host);
1778 this->my_id->destroy(this->my_id);
1779 this->other_id->destroy(this->other_id);
1780 this->my_host = other->my_host->clone(other->my_host);
1781 this->other_host = other->other_host->clone(other->other_host);
1782 this->my_id = other->my_id->clone(other->my_id);
1783 this->other_id = other->other_id->clone(other->other_id);
1784
1785 /* apply virtual assigned IPs... */
1786 if (other->my_virtual_ip)
1787 {
1788 this->my_virtual_ip = other->my_virtual_ip;
1789 other->my_virtual_ip = NULL;
1790 }
1791 if (other->other_virtual_ip)
1792 {
1793 this->other_virtual_ip = other->other_virtual_ip;
1794 other->other_virtual_ip = NULL;
1795 }
1796
1797 /* ... and DNS servers */
1798 while (other->dns_servers->remove_last(other->dns_servers,
1799 (void**)&ip) == SUCCESS)
1800 {
1801 this->dns_servers->insert_first(this->dns_servers, ip);
1802 }
1803
1804 /* adopt all children */
1805 while (other->child_sas->remove_last(other->child_sas,
1806 (void**)&child_sa) == SUCCESS)
1807 {
1808 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1809 }
1810
1811 /* move pending tasks to the new IKE_SA */
1812 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1813
1814 /* we have to initate here, there may be new tasks to handle */
1815 return this->task_manager->initiate(this->task_manager);
1816 }
1817
1818 /**
1819 * Implementation of ike_sa_t.remove_dns_server
1820 */
1821 static void remove_dns_servers(private_ike_sa_t *this)
1822 {
1823 FILE *file;
1824 struct stat stats;
1825 chunk_t contents, line, orig_line, token;
1826 char string[INET6_ADDRSTRLEN];
1827 host_t *ip;
1828 iterator_t *iterator;
1829
1830 if (this->dns_servers->get_count(this->dns_servers) == 0)
1831 {
1832 /* don't touch anything if we have no nameservers installed */
1833 return;
1834 }
1835
1836 file = fopen(RESOLV_CONF, "r");
1837 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1838 {
1839 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1840 RESOLV_CONF, strerror(errno));
1841 return;
1842 }
1843
1844 contents = chunk_alloca((size_t)stats.st_size);
1845
1846 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1847 {
1848 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1849 fclose(file);
1850 return;
1851 }
1852
1853 fclose(file);
1854 file = fopen(RESOLV_CONF, "w");
1855 if (file == NULL)
1856 {
1857 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1858 RESOLV_CONF, strerror(errno));
1859 return;
1860 }
1861
1862 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
1863 while (fetchline(&contents, &line))
1864 {
1865 bool found = FALSE;
1866 orig_line = line;
1867 if (extract_token(&token, ' ', &line) &&
1868 strncasecmp(token.ptr, "nameserver", token.len) == 0)
1869 {
1870 if (!extract_token(&token, ' ', &line))
1871 {
1872 token = line;
1873 }
1874 iterator->reset(iterator);
1875 while (iterator->iterate(iterator, (void**)&ip))
1876 {
1877 snprintf(string, sizeof(string), "%H", ip);
1878 if (strlen(string) == token.len &&
1879 strncmp(token.ptr, string, token.len) == 0)
1880 {
1881 iterator->remove(iterator);
1882 ip->destroy(ip);
1883 found = TRUE;
1884 break;
1885 }
1886 }
1887 }
1888
1889 if (!found)
1890 {
1891 /* write line untouched back to file */
1892 fwrite(orig_line.ptr, orig_line.len, 1, file);
1893 fprintf(file, "\n");
1894 }
1895 }
1896 iterator->destroy(iterator);
1897 fclose(file);
1898 }
1899
1900 /**
1901 * Implementation of ike_sa_t.add_dns_server
1902 */
1903 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
1904 {
1905 FILE *file;
1906 struct stat stats;
1907 chunk_t contents;
1908
1909 DBG1(DBG_IKE, "installing DNS server %H", dns);
1910
1911 file = fopen(RESOLV_CONF, "a+");
1912 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1913 {
1914 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1915 RESOLV_CONF, strerror(errno));
1916 return;
1917 }
1918
1919 contents = chunk_alloca(stats.st_size);
1920
1921 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1922 {
1923 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1924 fclose(file);
1925 return;
1926 }
1927
1928 fclose(file);
1929 file = fopen(RESOLV_CONF, "w");
1930 if (file == NULL)
1931 {
1932 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1933 RESOLV_CONF, strerror(errno));
1934 return;
1935 }
1936
1937 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
1938 dns, this->other_id) < 0)
1939 {
1940 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
1941 }
1942 else
1943 {
1944 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
1945 }
1946 fwrite(contents.ptr, contents.len, 1, file);
1947
1948 fclose(file);
1949 }
1950
1951 /**
1952 * Implementation of ike_sa_t.destroy.
1953 */
1954 static void destroy(private_ike_sa_t *this)
1955 {
1956 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1957
1958 DESTROY_IF(this->crypter_in);
1959 DESTROY_IF(this->crypter_out);
1960 DESTROY_IF(this->signer_in);
1961 DESTROY_IF(this->signer_out);
1962 DESTROY_IF(this->prf);
1963 DESTROY_IF(this->child_prf);
1964 chunk_free(&this->skp_verify);
1965 chunk_free(&this->skp_build);
1966
1967 if (this->my_virtual_ip)
1968 {
1969 charon->kernel_interface->del_ip(charon->kernel_interface,
1970 this->my_virtual_ip);
1971 this->my_virtual_ip->destroy(this->my_virtual_ip);
1972 }
1973 DESTROY_IF(this->other_virtual_ip);
1974
1975 remove_dns_servers(this);
1976 this->dns_servers->destroy_offset(this->dns_servers,
1977 offsetof(host_t, destroy));
1978 this->additional_addresses->destroy_offset(this->additional_addresses,
1979 offsetof(host_t, destroy));
1980
1981 DESTROY_IF(this->my_host);
1982 DESTROY_IF(this->other_host);
1983 DESTROY_IF(this->my_id);
1984 DESTROY_IF(this->other_id);
1985
1986 DESTROY_IF(this->ike_cfg);
1987 DESTROY_IF(this->peer_cfg);
1988
1989 this->ike_sa_id->destroy(this->ike_sa_id);
1990 this->task_manager->destroy(this->task_manager);
1991 free(this);
1992 }
1993
1994 /*
1995 * Described in header.
1996 */
1997 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
1998 {
1999 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
2000 static u_int32_t unique_id = 0;
2001
2002 /* Public functions */
2003 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
2004 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
2005 this->public.get_stats = (void (*)(ike_sa_t*,u_int32_t*))get_stats;
2006 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2007 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2008 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2009 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2010 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2011 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2012 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2013 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2014 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2015 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2016 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2017 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2018 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2019 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2020 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2021 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2022 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2023 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2024 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2025 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2026 this->public.get_other_ca = (ca_info_t* (*)(ike_sa_t*)) get_other_ca;
2027 this->public.set_other_ca = (void (*)(ike_sa_t*,ca_info_t*)) set_other_ca;
2028 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2029 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2030 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2031 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2032 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2033 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2034 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2035 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2036 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2037 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2038 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2039 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
2040 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
2041 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
2042 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
2043 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2044 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2045 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2046 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2047 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2048 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2049 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2050 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2051 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2052 this->public.roam = (status_t(*)(ike_sa_t*))roam;
2053 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2054 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2055 this->public.reset = (void (*)(ike_sa_t*))reset;
2056 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2057 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2058 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2059 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2060
2061 /* initialize private fields */
2062 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2063 this->child_sas = linked_list_create();
2064 this->my_host = host_create_any(AF_INET);
2065 this->other_host = host_create_any(AF_INET);
2066 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2067 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2068 this->other_ca = NULL;
2069 this->extensions = 0;
2070 this->conditions = 0;
2071 this->crypter_in = NULL;
2072 this->crypter_out = NULL;
2073 this->signer_in = NULL;
2074 this->signer_out = NULL;
2075 this->prf = NULL;
2076 this->skp_verify = chunk_empty;
2077 this->skp_build = chunk_empty;
2078 this->child_prf = NULL;
2079 this->state = IKE_CREATED;
2080 this->time.inbound = this->time.outbound = time(NULL);
2081 this->time.established = 0;
2082 this->time.rekey = 0;
2083 this->time.delete = 0;
2084 this->ike_cfg = NULL;
2085 this->peer_cfg = NULL;
2086 this->task_manager = task_manager_create(&this->public);
2087 this->unique_id = ++unique_id;
2088 this->my_virtual_ip = NULL;
2089 this->other_virtual_ip = NULL;
2090 this->dns_servers = linked_list_create();
2091 this->additional_addresses = linked_list_create();
2092 this->keyingtry = 0;
2093
2094 return &this->public;
2095 }