further mobike improvements, regarding to NAT-T
[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 * Update hosts, as addresses may change (NAT)
651 */
652 static void update_hosts(private_ike_sa_t *this, host_t *me, host_t *other)
653 {
654 bool update = FALSE;
655
656 if (me == NULL)
657 {
658 me = this->my_host;
659 }
660 if (other == NULL)
661 {
662 other = this->other_host;
663 }
664
665 /* apply hosts on first received message */
666 if (this->my_host->is_anyaddr(this->my_host) ||
667 this->other_host->is_anyaddr(this->other_host))
668 {
669 set_my_host(this, me->clone(me));
670 set_other_host(this, other->clone(other));
671 update = TRUE;
672 }
673 else
674 {
675 /* update our address in any case */
676 if (!me->equals(me, this->my_host))
677 {
678 set_my_host(this, me->clone(me));
679 update = TRUE;
680 }
681
682 if (!other->equals(other, this->other_host))
683 {
684 /* update others adress if we are NOT NATed,
685 * and allow port changes if we are NATed */
686 if (!has_condition(this, COND_NAT_HERE) ||
687 other->ip_equals(other, this->other_host))
688 {
689 set_other_host(this, other->clone(other));
690 update = TRUE;
691 }
692 }
693 }
694
695 /* update all associated CHILD_SAs, if required */
696 if (update)
697 {
698 iterator_t *iterator;
699 child_sa_t *child_sa;
700
701 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
702 while (iterator->iterate(iterator, (void**)&child_sa))
703 {
704 child_sa->update_hosts(child_sa, this->my_host, this->other_host,
705 has_condition(this, COND_NAT_ANY));
706 }
707 iterator->destroy(iterator);
708 }
709 }
710
711 /**
712 * Implementation of ike_sa_t.generate
713 */
714 static status_t generate_message(private_ike_sa_t *this, message_t *message,
715 packet_t **packet)
716 {
717 this->time.outbound = time(NULL);
718 message->set_ike_sa_id(message, this->ike_sa_id);
719 message->set_destination(message, this->other_host->clone(this->other_host));
720 message->set_source(message, this->my_host->clone(this->my_host));
721 return message->generate(message, this->crypter_out, this->signer_out, packet);
722 }
723
724 /**
725 * send a notify back to the sender
726 */
727 static void send_notify_response(private_ike_sa_t *this, message_t *request,
728 notify_type_t type)
729 {
730 message_t *response;
731 packet_t *packet;
732
733 response = message_create();
734 response->set_exchange_type(response, request->get_exchange_type(request));
735 response->set_request(response, FALSE);
736 response->set_message_id(response, request->get_message_id(request));
737 response->add_notify(response, FALSE, type, chunk_empty);
738 if (this->my_host->is_anyaddr(this->my_host))
739 {
740 this->my_host->destroy(this->my_host);
741 this->my_host = request->get_destination(request);
742 this->my_host = this->my_host->clone(this->my_host);
743 }
744 if (this->other_host->is_anyaddr(this->other_host))
745 {
746 this->other_host->destroy(this->other_host);
747 this->other_host = request->get_source(request);
748 this->other_host = this->other_host->clone(this->other_host);
749 }
750 if (generate_message(this, response, &packet) == SUCCESS)
751 {
752 charon->sender->send(charon->sender, packet);
753 }
754 response->destroy(response);
755 }
756
757 /**
758 * Implementation of ike_sa_t.process_message.
759 */
760 static status_t process_message(private_ike_sa_t *this, message_t *message)
761 {
762 status_t status;
763 bool is_request;
764
765 is_request = message->get_request(message);
766
767 status = message->parse_body(message, this->crypter_in, this->signer_in);
768 if (status != SUCCESS)
769 {
770
771 if (is_request)
772 {
773 switch (status)
774 {
775 case NOT_SUPPORTED:
776 DBG1(DBG_IKE, "ciritcal unknown payloads found");
777 if (is_request)
778 {
779 send_notify_response(this, message, UNSUPPORTED_CRITICAL_PAYLOAD);
780 }
781 break;
782 case PARSE_ERROR:
783 DBG1(DBG_IKE, "message parsing failed");
784 if (is_request)
785 {
786 send_notify_response(this, message, INVALID_SYNTAX);
787 }
788 break;
789 case VERIFY_ERROR:
790 DBG1(DBG_IKE, "message verification failed");
791 if (is_request)
792 {
793 send_notify_response(this, message, INVALID_SYNTAX);
794 }
795 break;
796 case FAILED:
797 DBG1(DBG_IKE, "integrity check failed");
798 /* ignored */
799 break;
800 case INVALID_STATE:
801 DBG1(DBG_IKE, "found encrypted message, but no keys available");
802 if (is_request)
803 {
804 send_notify_response(this, message, INVALID_SYNTAX);
805 }
806 default:
807 break;
808 }
809 }
810 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
811 exchange_type_names, message->get_exchange_type(message),
812 message->get_request(message) ? "request" : "response",
813 message->get_message_id(message));
814 return status;
815 }
816 else
817 {
818 host_t *me, *other;
819
820 me = message->get_destination(message);
821 other = message->get_source(message);
822
823 /* if this IKE_SA is virgin, we check for a config */
824 if (this->ike_cfg == NULL)
825 {
826 job_t *job;
827 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
828 me, other);
829 if (this->ike_cfg == NULL)
830 {
831 /* no config found for these hosts, destroy */
832 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
833 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
834 send_notify_response(this, message, NO_PROPOSAL_CHOSEN);
835 return DESTROY_ME;
836 }
837 /* add a timeout if peer does not establish it completely */
838 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
839 charon->scheduler->schedule_job(charon->scheduler, job,
840 HALF_OPEN_IKE_SA_TIMEOUT);
841 }
842
843 /* check if message is trustworthy, and update host information */
844 if (this->state == IKE_CREATED || this->state == IKE_CONNECTING ||
845 message->get_exchange_type(message) != IKE_SA_INIT)
846 {
847 update_hosts(this, me, other);
848 this->time.inbound = time(NULL);
849 }
850 return this->task_manager->process_message(this->task_manager, message);
851 }
852 }
853
854 /**
855 * Implementation of ike_sa_t.initiate.
856 */
857 static status_t initiate(private_ike_sa_t *this, child_cfg_t *child_cfg)
858 {
859 task_t *task;
860
861 if (this->state == IKE_CREATED)
862 {
863
864 if (this->other_host->is_anyaddr(this->other_host))
865 {
866 child_cfg->destroy(child_cfg);
867 SIG(IKE_UP_START, "initiating IKE_SA");
868 SIG(IKE_UP_FAILED, "unable to initiate to %%any");
869 return DESTROY_ME;
870 }
871
872 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
873 this->task_manager->queue_task(this->task_manager, task);
874 task = (task_t*)ike_natd_create(&this->public, TRUE);
875 this->task_manager->queue_task(this->task_manager, task);
876 task = (task_t*)ike_cert_create(&this->public, TRUE);
877 this->task_manager->queue_task(this->task_manager, task);
878 task = (task_t*)ike_auth_create(&this->public, TRUE);
879 this->task_manager->queue_task(this->task_manager, task);
880 task = (task_t*)ike_config_create(&this->public, TRUE);
881 this->task_manager->queue_task(this->task_manager, task);
882 task = (task_t*)ike_mobike_create(&this->public, TRUE);
883 this->task_manager->queue_task(this->task_manager, task);
884 }
885
886 task = (task_t*)child_create_create(&this->public, child_cfg);
887 child_cfg->destroy(child_cfg);
888 this->task_manager->queue_task(this->task_manager, task);
889
890 return this->task_manager->initiate(this->task_manager);
891 }
892
893 /**
894 * Implementation of ike_sa_t.acquire.
895 */
896 static status_t acquire(private_ike_sa_t *this, u_int32_t reqid)
897 {
898 child_cfg_t *child_cfg;
899 iterator_t *iterator;
900 child_sa_t *current, *child_sa = NULL;
901 task_t *task;
902 child_create_t *child_create;
903
904 if (this->state == IKE_DELETING)
905 {
906 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
907 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
908 "IKE_SA is deleting", reqid);
909 return FAILED;
910 }
911
912 /* find CHILD_SA */
913 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
914 while (iterator->iterate(iterator, (void**)&current))
915 {
916 if (current->get_reqid(current) == reqid)
917 {
918 child_sa = current;
919 break;
920 }
921 }
922 iterator->destroy(iterator);
923 if (!child_sa)
924 {
925 SIG(CHILD_UP_START, "acquiring CHILD_SA on kernel request");
926 SIG(CHILD_UP_FAILED, "acquiring CHILD_SA (reqid %d) failed: "
927 "CHILD_SA not found", reqid);
928 return FAILED;
929 }
930
931
932 if (this->state == IKE_CREATED)
933 {
934 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
935 this->task_manager->queue_task(this->task_manager, task);
936 task = (task_t*)ike_natd_create(&this->public, TRUE);
937 this->task_manager->queue_task(this->task_manager, task);
938 task = (task_t*)ike_cert_create(&this->public, TRUE);
939 this->task_manager->queue_task(this->task_manager, task);
940 task = (task_t*)ike_auth_create(&this->public, TRUE);
941 this->task_manager->queue_task(this->task_manager, task);
942 task = (task_t*)ike_config_create(&this->public, TRUE);
943 this->task_manager->queue_task(this->task_manager, task);
944 task = (task_t*)ike_mobike_create(&this->public, TRUE);
945 this->task_manager->queue_task(this->task_manager, task);
946 }
947
948 child_cfg = child_sa->get_config(child_sa);
949 child_create = child_create_create(&this->public, child_cfg);
950 child_create->use_reqid(child_create, reqid);
951 this->task_manager->queue_task(this->task_manager, (task_t*)child_create);
952
953 return this->task_manager->initiate(this->task_manager);
954 }
955
956 /**
957 * Implementation of ike_sa_t.route.
958 */
959 static status_t route(private_ike_sa_t *this, child_cfg_t *child_cfg)
960 {
961 child_sa_t *child_sa;
962 iterator_t *iterator;
963 linked_list_t *my_ts, *other_ts;
964 status_t status;
965
966 SIG(CHILD_ROUTE_START, "routing CHILD_SA");
967
968 /* check if not already routed*/
969 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
970 while (iterator->iterate(iterator, (void**)&child_sa))
971 {
972 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
973 streq(child_sa->get_name(child_sa), child_cfg->get_name(child_cfg)))
974 {
975 iterator->destroy(iterator);
976 SIG(CHILD_ROUTE_FAILED, "CHILD_SA with such a config already routed");
977 return FAILED;
978 }
979 }
980 iterator->destroy(iterator);
981
982 switch (this->state)
983 {
984 case IKE_DELETING:
985 case IKE_REKEYING:
986 SIG(CHILD_ROUTE_FAILED,
987 "unable to route CHILD_SA, as its IKE_SA gets deleted");
988 return FAILED;
989 case IKE_CREATED:
990 case IKE_CONNECTING:
991 case IKE_ESTABLISHED:
992 default:
993 break;
994 }
995
996 /* install kernel policies */
997 child_sa = child_sa_create(this->my_host, this->other_host, this->my_id,
998 this->other_id, child_cfg, FALSE, 0);
999
1000 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL,
1001 this->my_host);
1002 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL,
1003 this->other_host);
1004 status = child_sa->add_policies(child_sa, my_ts, other_ts,
1005 child_cfg->get_mode(child_cfg));
1006 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1007 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1008 if (status == SUCCESS)
1009 {
1010 this->child_sas->insert_last(this->child_sas, child_sa);
1011 SIG(CHILD_ROUTE_SUCCESS, "CHILD_SA routed");
1012 }
1013 else
1014 {
1015 SIG(CHILD_ROUTE_FAILED, "routing CHILD_SA failed");
1016 }
1017 return status;
1018 }
1019
1020 /**
1021 * Implementation of ike_sa_t.unroute.
1022 */
1023 static status_t unroute(private_ike_sa_t *this, u_int32_t reqid)
1024 {
1025 iterator_t *iterator;
1026 child_sa_t *child_sa;
1027 bool found = FALSE;
1028
1029 SIG(CHILD_UNROUTE_START, "unrouting CHILD_SA");
1030
1031 /* find CHILD_SA in ROUTED state */
1032 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1033 while (iterator->iterate(iterator, (void**)&child_sa))
1034 {
1035 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
1036 child_sa->get_reqid(child_sa) == reqid)
1037 {
1038 iterator->remove(iterator);
1039 SIG(CHILD_UNROUTE_SUCCESS, "CHILD_SA unrouted");
1040 child_sa->destroy(child_sa);
1041 found = TRUE;
1042 break;
1043 }
1044 }
1045 iterator->destroy(iterator);
1046
1047 if (!found)
1048 {
1049 SIG(CHILD_UNROUTE_FAILED, "CHILD_SA to unroute not found");
1050 return FAILED;
1051 }
1052 /* if we are not established, and we have no more routed childs, remove whole SA */
1053 if (this->state == IKE_CREATED &&
1054 this->child_sas->get_count(this->child_sas) == 0)
1055 {
1056 return DESTROY_ME;
1057 }
1058 return SUCCESS;
1059 }
1060
1061 /**
1062 * Implementation of ike_sa_t.retransmit.
1063 */
1064 static status_t retransmit(private_ike_sa_t *this, u_int32_t message_id)
1065 {
1066 this->time.outbound = time(NULL);
1067 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1068 {
1069 child_cfg_t *child_cfg;
1070 child_sa_t* child_sa;
1071 linked_list_t *to_route, *to_restart;
1072 iterator_t *iterator;
1073
1074 /* send a proper signal to brief interested bus listeners */
1075 switch (this->state)
1076 {
1077 case IKE_CONNECTING:
1078 {
1079 /* retry IKE_SA_INIT if we have multiple keyingtries */
1080 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1081 this->keyingtry++;
1082 if (tries == 0 || tries > this->keyingtry)
1083 {
1084 SIG(IKE_UP_FAILED, "peer not responding, trying again "
1085 "(%d/%d) in background ", this->keyingtry + 1, tries);
1086 reset(this);
1087 return this->task_manager->initiate(this->task_manager);
1088 }
1089 SIG(IKE_UP_FAILED, "establishing IKE_SA failed, peer not responding");
1090 break;
1091 }
1092 case IKE_REKEYING:
1093 SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed, peer not responding");
1094 break;
1095 case IKE_DELETING:
1096 SIG(IKE_DOWN_FAILED, "proper IKE_SA delete failed, peer not responding");
1097 break;
1098 default:
1099 break;
1100 }
1101
1102 /* summarize how we have to handle each child */
1103 to_route = linked_list_create();
1104 to_restart = linked_list_create();
1105 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1106 while (iterator->iterate(iterator, (void**)&child_sa))
1107 {
1108 child_cfg = child_sa->get_config(child_sa);
1109
1110 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
1111 {
1112 /* reroute routed CHILD_SAs */
1113 to_route->insert_last(to_route, child_cfg);
1114 }
1115 else
1116 {
1117 /* use DPD action for established CHILD_SAs */
1118 switch (this->peer_cfg->get_dpd_action(this->peer_cfg))
1119 {
1120 case DPD_ROUTE:
1121 to_route->insert_last(to_route, child_cfg);
1122 break;
1123 case DPD_RESTART:
1124 to_restart->insert_last(to_restart, child_cfg);
1125 break;
1126 default:
1127 break;
1128 }
1129 }
1130 }
1131 iterator->destroy(iterator);
1132
1133 /* create a new IKE_SA if we have to route or to restart */
1134 if (to_route->get_count(to_route) || to_restart->get_count(to_restart))
1135 {
1136 private_ike_sa_t *new;
1137 task_t *task;
1138
1139 new = (private_ike_sa_t*)charon->ike_sa_manager->checkout_new(
1140 charon->ike_sa_manager, TRUE);
1141
1142 set_peer_cfg(new, this->peer_cfg);
1143 /* use actual used host, not the wildcarded one in config */
1144 new->other_host->destroy(new->other_host);
1145 new->other_host = this->other_host->clone(this->other_host);
1146
1147 /* install routes */
1148 while (to_route->remove_last(to_route, (void**)&child_cfg) == SUCCESS)
1149 {
1150 route(new, child_cfg);
1151 }
1152
1153 /* restart children */
1154 if (to_restart->get_count(to_restart))
1155 {
1156 task = (task_t*)ike_init_create(&new->public, TRUE, NULL);
1157 new->task_manager->queue_task(new->task_manager, task);
1158 task = (task_t*)ike_natd_create(&new->public, TRUE);
1159 new->task_manager->queue_task(new->task_manager, task);
1160 task = (task_t*)ike_cert_create(&new->public, TRUE);
1161 new->task_manager->queue_task(new->task_manager, task);
1162 task = (task_t*)ike_config_create(&new->public, TRUE);
1163 new->task_manager->queue_task(new->task_manager, task);
1164 task = (task_t*)ike_auth_create(&new->public, TRUE);
1165 new->task_manager->queue_task(new->task_manager, task);
1166
1167 while (to_restart->remove_last(to_restart, (void**)&child_cfg) == SUCCESS)
1168 {
1169 task = (task_t*)child_create_create(&new->public, child_cfg);
1170 new->task_manager->queue_task(new->task_manager, task);
1171 }
1172 task = (task_t*)ike_mobike_create(&new->public, TRUE);
1173 new->task_manager->queue_task(new->task_manager, task);
1174 new->task_manager->initiate(new->task_manager);
1175 }
1176 charon->ike_sa_manager->checkin(charon->ike_sa_manager, &new->public);
1177 }
1178 to_route->destroy(to_route);
1179 to_restart->destroy(to_restart);
1180 return DESTROY_ME;
1181 }
1182 return SUCCESS;
1183 }
1184
1185 /**
1186 * Implementation of ike_sa_t.get_prf.
1187 */
1188 static prf_t *get_prf(private_ike_sa_t *this)
1189 {
1190 return this->prf;
1191 }
1192
1193 /**
1194 * Implementation of ike_sa_t.get_prf.
1195 */
1196 static prf_t *get_child_prf(private_ike_sa_t *this)
1197 {
1198 return this->child_prf;
1199 }
1200
1201 /**
1202 * Implementation of ike_sa_t.get_skp_bild
1203 */
1204 static chunk_t get_skp_build(private_ike_sa_t *this)
1205 {
1206 return this->skp_build;
1207 }
1208
1209 /**
1210 * Implementation of ike_sa_t.get_skp_verify
1211 */
1212 static chunk_t get_skp_verify(private_ike_sa_t *this)
1213 {
1214 return this->skp_verify;
1215 }
1216
1217 /**
1218 * Implementation of ike_sa_t.get_id.
1219 */
1220 static ike_sa_id_t* get_id(private_ike_sa_t *this)
1221 {
1222 return this->ike_sa_id;
1223 }
1224
1225 /**
1226 * Implementation of ike_sa_t.get_my_id.
1227 */
1228 static identification_t* get_my_id(private_ike_sa_t *this)
1229 {
1230 return this->my_id;
1231 }
1232
1233 /**
1234 * Implementation of ike_sa_t.set_my_id.
1235 */
1236 static void set_my_id(private_ike_sa_t *this, identification_t *me)
1237 {
1238 DESTROY_IF(this->my_id);
1239 this->my_id = me;
1240 }
1241
1242 /**
1243 * Implementation of ike_sa_t.get_other_id.
1244 */
1245 static identification_t* get_other_id(private_ike_sa_t *this)
1246 {
1247 return this->other_id;
1248 }
1249
1250 /**
1251 * Implementation of ike_sa_t.set_other_id.
1252 */
1253 static void set_other_id(private_ike_sa_t *this, identification_t *other)
1254 {
1255 DESTROY_IF(this->other_id);
1256 this->other_id = other;
1257 }
1258
1259 /**
1260 * Implementation of ike_sa_t.get_other_ca.
1261 */
1262 static ca_info_t* get_other_ca(private_ike_sa_t *this)
1263 {
1264 return this->other_ca;
1265 }
1266
1267 /**
1268 * Implementation of ike_sa_t.set_other_ca.
1269 */
1270 static void set_other_ca(private_ike_sa_t *this, ca_info_t *other_ca)
1271 {
1272 this->other_ca = other_ca;
1273 }
1274
1275 /**
1276 * Implementation of ike_sa_t.set_virtual_ip
1277 */
1278 static void set_virtual_ip(private_ike_sa_t *this, bool local, host_t *ip)
1279 {
1280 if (local)
1281 {
1282 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
1283 if (this->my_virtual_ip)
1284 {
1285 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
1286 charon->kernel_interface->del_ip(charon->kernel_interface,
1287 this->my_virtual_ip);
1288 this->my_virtual_ip->destroy(this->my_virtual_ip);
1289 }
1290 if (charon->kernel_interface->add_ip(charon->kernel_interface, ip,
1291 this->my_host) == SUCCESS)
1292 {
1293 this->my_virtual_ip = ip->clone(ip);
1294 }
1295 else
1296 {
1297 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
1298 this->my_virtual_ip = NULL;
1299 }
1300 }
1301 else
1302 {
1303 DESTROY_IF(this->other_virtual_ip);
1304 this->other_virtual_ip = ip->clone(ip);
1305 }
1306 }
1307
1308 /**
1309 * Implementation of ike_sa_t.get_virtual_ip
1310 */
1311 static host_t* get_virtual_ip(private_ike_sa_t *this, bool local)
1312 {
1313 if (local)
1314 {
1315 return this->my_virtual_ip;
1316 }
1317 else
1318 {
1319 return this->other_virtual_ip;
1320 }
1321 }
1322
1323 /**
1324 * Implementation of ike_sa_t.add_additional_address.
1325 */
1326 static void add_additional_address(private_ike_sa_t *this, host_t *host)
1327 {
1328 this->additional_addresses->insert_last(this->additional_addresses, host);
1329 }
1330
1331 /**
1332 * Implementation of ike_sa_t.create_additional_address_iterator.
1333 */
1334 static iterator_t* create_additional_address_iterator(private_ike_sa_t *this)
1335 {
1336 return this->additional_addresses->create_iterator(
1337 this->additional_addresses, TRUE);
1338 }
1339
1340 /**
1341 * Implementation of ike_sa_t.derive_keys.
1342 */
1343 static status_t derive_keys(private_ike_sa_t *this,
1344 proposal_t *proposal, chunk_t secret,
1345 chunk_t nonce_i, chunk_t nonce_r,
1346 bool initiator, prf_t *child_prf, prf_t *old_prf)
1347 {
1348 prf_plus_t *prf_plus;
1349 chunk_t skeyseed, key, nonces, prf_plus_seed;
1350 algorithm_t *algo;
1351 size_t key_size;
1352 crypter_t *crypter_i, *crypter_r;
1353 signer_t *signer_i, *signer_r;
1354 u_int8_t spi_i_buf[sizeof(u_int64_t)], spi_r_buf[sizeof(u_int64_t)];
1355 chunk_t spi_i = chunk_from_buf(spi_i_buf);
1356 chunk_t spi_r = chunk_from_buf(spi_r_buf);
1357
1358 /* Create SAs general purpose PRF first, we may use it here */
1359 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo))
1360 {
1361 DBG1(DBG_IKE, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1362 return FAILED;
1363 }
1364 this->prf = prf_create(algo->algorithm);
1365 if (this->prf == NULL)
1366 {
1367 DBG1(DBG_IKE, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1368 "%N not supported!", pseudo_random_function_names, algo->algorithm);
1369 return FAILED;
1370 }
1371
1372 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
1373 nonces = chunk_cat("cc", nonce_i, nonce_r);
1374 *((u_int64_t*)spi_i.ptr) = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
1375 *((u_int64_t*)spi_r.ptr) = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
1376 prf_plus_seed = chunk_cat("ccc", nonces, spi_i, spi_r);
1377
1378 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1379 *
1380 * if we are rekeying, SKEYSEED is built on another way
1381 */
1382 if (child_prf == NULL) /* not rekeying */
1383 {
1384 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1385 this->prf->set_key(this->prf, nonces);
1386 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
1387 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1388 this->prf->set_key(this->prf, skeyseed);
1389 chunk_free(&skeyseed);
1390 chunk_free(&secret);
1391 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
1392 }
1393 else
1394 {
1395 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1396 * use OLD SAs PRF functions for both prf_plus and prf */
1397 secret = chunk_cat("mc", secret, nonces);
1398 child_prf->allocate_bytes(child_prf, secret, &skeyseed);
1399 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
1400 old_prf->set_key(old_prf, skeyseed);
1401 chunk_free(&skeyseed);
1402 chunk_free(&secret);
1403 prf_plus = prf_plus_create(old_prf, prf_plus_seed);
1404 }
1405 chunk_free(&nonces);
1406 chunk_free(&prf_plus_seed);
1407
1408 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1409
1410 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1411 proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &algo);
1412 this->child_prf = prf_create(algo->algorithm);
1413 key_size = this->child_prf->get_key_size(this->child_prf);
1414 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1415 DBG4(DBG_IKE, "Sk_d secret %B", &key);
1416 this->child_prf->set_key(this->child_prf, key);
1417 chunk_free(&key);
1418
1419 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1420 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
1421 {
1422 DBG1(DBG_IKE, "key derivation failed: no INTEGRITY_ALGORITHM");
1423 return FAILED;
1424 }
1425 signer_i = signer_create(algo->algorithm);
1426 signer_r = signer_create(algo->algorithm);
1427 if (signer_i == NULL || signer_r == NULL)
1428 {
1429 DBG1(DBG_IKE, "key derivation failed: INTEGRITY_ALGORITHM "
1430 "%N not supported!", integrity_algorithm_names ,algo->algorithm);
1431 return FAILED;
1432 }
1433 key_size = signer_i->get_key_size(signer_i);
1434
1435 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1436 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
1437 signer_i->set_key(signer_i, key);
1438 chunk_free(&key);
1439
1440 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1441 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
1442 signer_r->set_key(signer_r, key);
1443 chunk_free(&key);
1444
1445 if (initiator)
1446 {
1447 this->signer_in = signer_r;
1448 this->signer_out = signer_i;
1449 }
1450 else
1451 {
1452 this->signer_in = signer_i;
1453 this->signer_out = signer_r;
1454 }
1455
1456 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1457 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
1458 {
1459 DBG1(DBG_IKE, "key derivation failed: no ENCRYPTION_ALGORITHM");
1460 return FAILED;
1461 }
1462 crypter_i = crypter_create(algo->algorithm, algo->key_size / 8);
1463 crypter_r = crypter_create(algo->algorithm, algo->key_size / 8);
1464 if (crypter_i == NULL || crypter_r == NULL)
1465 {
1466 DBG1(DBG_IKE, "key derivation failed: ENCRYPTION_ALGORITHM "
1467 "%N (key size %d) not supported!",
1468 encryption_algorithm_names, algo->algorithm, algo->key_size);
1469 return FAILED;
1470 }
1471 key_size = crypter_i->get_key_size(crypter_i);
1472
1473 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1474 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
1475 crypter_i->set_key(crypter_i, key);
1476 chunk_free(&key);
1477
1478 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1479 DBG4(DBG_IKE, "Sk_er secret %B", &key);
1480 crypter_r->set_key(crypter_r, key);
1481 chunk_free(&key);
1482
1483 if (initiator)
1484 {
1485 this->crypter_in = crypter_r;
1486 this->crypter_out = crypter_i;
1487 }
1488 else
1489 {
1490 this->crypter_in = crypter_i;
1491 this->crypter_out = crypter_r;
1492 }
1493
1494 /* SK_pi/SK_pr used for authentication => stored for later */
1495 key_size = this->prf->get_key_size(this->prf);
1496 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1497 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
1498 if (initiator)
1499 {
1500 this->skp_build = key;
1501 }
1502 else
1503 {
1504 this->skp_verify = key;
1505 }
1506 prf_plus->allocate_bytes(prf_plus, key_size, &key);
1507 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
1508 if (initiator)
1509 {
1510 this->skp_verify = key;
1511 }
1512 else
1513 {
1514 this->skp_build = key;
1515 }
1516
1517 /* all done, prf_plus not needed anymore */
1518 prf_plus->destroy(prf_plus);
1519
1520 return SUCCESS;
1521 }
1522
1523 /**
1524 * Implementation of ike_sa_t.add_child_sa.
1525 */
1526 static void add_child_sa(private_ike_sa_t *this, child_sa_t *child_sa)
1527 {
1528 this->child_sas->insert_last(this->child_sas, child_sa);
1529 }
1530
1531 /**
1532 * Implementation of ike_sa_t.get_child_sa.
1533 */
1534 static child_sa_t* get_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1535 u_int32_t spi, bool inbound)
1536 {
1537 iterator_t *iterator;
1538 child_sa_t *current, *found = NULL;
1539
1540 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1541 while (iterator->iterate(iterator, (void**)&current))
1542 {
1543 if (current->get_spi(current, inbound) == spi &&
1544 current->get_protocol(current) == protocol)
1545 {
1546 found = current;
1547 }
1548 }
1549 iterator->destroy(iterator);
1550 return found;
1551 }
1552
1553 /**
1554 * Implementation of ike_sa_t.create_child_sa_iterator.
1555 */
1556 static iterator_t* create_child_sa_iterator(private_ike_sa_t *this)
1557 {
1558 return this->child_sas->create_iterator(this->child_sas, TRUE);
1559 }
1560
1561 /**
1562 * Implementation of ike_sa_t.rekey_child_sa.
1563 */
1564 static status_t rekey_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1565 {
1566 child_sa_t *child_sa;
1567 child_rekey_t *child_rekey;
1568
1569 child_sa = get_child_sa(this, protocol, spi, TRUE);
1570 if (child_sa)
1571 {
1572 child_rekey = child_rekey_create(&this->public, child_sa);
1573 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1574 return this->task_manager->initiate(this->task_manager);
1575 }
1576 return FAILED;
1577 }
1578
1579 /**
1580 * Implementation of ike_sa_t.delete_child_sa.
1581 */
1582 static status_t delete_child_sa(private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1583 {
1584 child_sa_t *child_sa;
1585 child_delete_t *child_delete;
1586
1587 child_sa = get_child_sa(this, protocol, spi, TRUE);
1588 if (child_sa)
1589 {
1590 child_delete = child_delete_create(&this->public, child_sa);
1591 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1592 return this->task_manager->initiate(this->task_manager);
1593 }
1594 return FAILED;
1595 }
1596
1597 /**
1598 * Implementation of ike_sa_t.destroy_child_sa.
1599 */
1600 static status_t destroy_child_sa(private_ike_sa_t *this, protocol_id_t protocol,
1601 u_int32_t spi)
1602 {
1603 iterator_t *iterator;
1604 child_sa_t *child_sa;
1605 status_t status = NOT_FOUND;
1606
1607 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
1608 while (iterator->iterate(iterator, (void**)&child_sa))
1609 {
1610 if (child_sa->get_protocol(child_sa) == protocol &&
1611 child_sa->get_spi(child_sa, TRUE) == spi)
1612 {
1613 child_sa->destroy(child_sa);
1614 iterator->remove(iterator);
1615 status = SUCCESS;
1616 break;
1617 }
1618 }
1619 iterator->destroy(iterator);
1620 return status;
1621 }
1622
1623 /**
1624 * Implementation of public_ike_sa_t.delete.
1625 */
1626 static status_t delete_(private_ike_sa_t *this)
1627 {
1628 ike_delete_t *ike_delete;
1629
1630 switch (this->state)
1631 {
1632 case IKE_ESTABLISHED:
1633 case IKE_REKEYING:
1634 ike_delete = ike_delete_create(&this->public, TRUE);
1635 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1636 return this->task_manager->initiate(this->task_manager);
1637 default:
1638 DBG1(DBG_IKE, "destroying IKE_SA in state %N without notification",
1639 ike_sa_state_names, this->state);
1640 break;
1641 }
1642 return DESTROY_ME;
1643 }
1644
1645 /**
1646 * Implementation of ike_sa_t.rekey.
1647 */
1648 static status_t rekey(private_ike_sa_t *this)
1649 {
1650 ike_rekey_t *ike_rekey;
1651
1652 ike_rekey = ike_rekey_create(&this->public, TRUE);
1653
1654 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1655 return this->task_manager->initiate(this->task_manager);
1656 }
1657
1658 /**
1659 * Implementation of ike_sa_t.reestablish
1660 */
1661 static status_t reestablish(private_ike_sa_t *this)
1662 {
1663 task_t *task;
1664
1665 task = (task_t*)ike_reauth_create(&this->public);
1666 this->task_manager->queue_task(this->task_manager, task);
1667
1668 return this->task_manager->initiate(this->task_manager);
1669 }
1670
1671 /**
1672 * Implementation of ike_sa_t.roam.
1673 */
1674 static status_t roam(private_ike_sa_t *this)
1675 {
1676 iterator_t *iterator;
1677 host_t *me, *other;
1678 ike_mobike_t *mobike;
1679
1680 /* only initiator handles address updated actively */
1681 if (!this->ike_sa_id->is_initiator(this->ike_sa_id))
1682 {
1683 return SUCCESS;
1684 }
1685
1686 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
1687 this->other_host);
1688 if (me && this->my_virtual_ip && me->ip_equals(me, this->my_virtual_ip))
1689 { /* do not roam to the virtual IP of this IKE_SA */
1690 me->destroy(me);
1691 me = NULL;
1692 }
1693
1694 if (me)
1695 {
1696 set_condition(this, COND_STALE, FALSE);
1697 /* attachment still the same? */
1698 if (me->ip_equals(me, this->my_host))
1699 {
1700 DBG2(DBG_IKE, "%H still reached through %H, no update needed",
1701 this->other_host, me);
1702 me->destroy(me);
1703 return SUCCESS;
1704 }
1705 me->set_port(me, this->my_host->get_port(this->my_host));
1706
1707 /* our attachement changed, update if we have mobike */
1708 if (supports_extension(this, EXT_MOBIKE))
1709 {
1710 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1711 mobike = ike_mobike_create(&this->public, TRUE);
1712 mobike->roam(mobike, me, NULL);
1713 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1714 return this->task_manager->initiate(this->task_manager);
1715 }
1716 DBG1(DBG_IKE, "reestablishing IKE_SA due address change");
1717 /* reestablish if not */
1718 set_my_host(this, me);
1719 return reestablish(this);
1720 }
1721
1722 /* there is nothing we can do without mobike */
1723 if (!supports_extension(this, EXT_MOBIKE))
1724 {
1725 set_condition(this, COND_STALE, TRUE);
1726 return FAILED;
1727 }
1728
1729 /* we are unable to reach the peer. Try an alternative address */
1730 iterator = create_additional_address_iterator(this);
1731 while (iterator->iterate(iterator, (void**)&other))
1732 {
1733 me = charon->kernel_interface->get_source_addr(charon->kernel_interface,
1734 other);
1735 if (me && me->ip_equals(me, this->my_virtual_ip))
1736 { /* do not roam to the virtual IP of this IKE_SA */
1737 me->destroy(me);
1738 me = NULL;
1739 }
1740
1741 if (me)
1742 {
1743 /* good, we have a new route. Use MOBIKE to update */
1744 set_condition(this, COND_STALE, FALSE);
1745 iterator->destroy(iterator);
1746 me->set_port(me, this->my_host->get_port(this->my_host));
1747 other->set_port(other, this->other_host->get_port(this->other_host));
1748 mobike = ike_mobike_create(&this->public, TRUE);
1749 mobike->roam(mobike, me, other);
1750 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1751 return this->task_manager->initiate(this->task_manager);
1752 }
1753 }
1754 iterator->destroy(iterator);
1755
1756 /* no route found to host, give up (temporary) */
1757 set_condition(this, COND_STALE, TRUE);
1758 return FAILED;
1759 }
1760
1761 /**
1762 * Implementation of ike_sa_t.inherit.
1763 */
1764 static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
1765 {
1766 child_sa_t *child_sa;
1767 host_t *ip;
1768
1769 /* apply hosts and ids */
1770 this->my_host->destroy(this->my_host);
1771 this->other_host->destroy(this->other_host);
1772 this->my_id->destroy(this->my_id);
1773 this->other_id->destroy(this->other_id);
1774 this->my_host = other->my_host->clone(other->my_host);
1775 this->other_host = other->other_host->clone(other->other_host);
1776 this->my_id = other->my_id->clone(other->my_id);
1777 this->other_id = other->other_id->clone(other->other_id);
1778
1779 /* apply virtual assigned IPs... */
1780 if (other->my_virtual_ip)
1781 {
1782 this->my_virtual_ip = other->my_virtual_ip;
1783 other->my_virtual_ip = NULL;
1784 }
1785 if (other->other_virtual_ip)
1786 {
1787 this->other_virtual_ip = other->other_virtual_ip;
1788 other->other_virtual_ip = NULL;
1789 }
1790
1791 /* ... and DNS servers */
1792 while (other->dns_servers->remove_last(other->dns_servers,
1793 (void**)&ip) == SUCCESS)
1794 {
1795 this->dns_servers->insert_first(this->dns_servers, ip);
1796 }
1797
1798 /* adopt all children */
1799 while (other->child_sas->remove_last(other->child_sas,
1800 (void**)&child_sa) == SUCCESS)
1801 {
1802 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1803 }
1804
1805 /* move pending tasks to the new IKE_SA */
1806 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
1807
1808 /* we have to initate here, there may be new tasks to handle */
1809 return this->task_manager->initiate(this->task_manager);
1810 }
1811
1812 /**
1813 * Implementation of ike_sa_t.remove_dns_server
1814 */
1815 static void remove_dns_servers(private_ike_sa_t *this)
1816 {
1817 FILE *file;
1818 struct stat stats;
1819 chunk_t contents, line, orig_line, token;
1820 char string[INET6_ADDRSTRLEN];
1821 host_t *ip;
1822 iterator_t *iterator;
1823
1824 if (this->dns_servers->get_count(this->dns_servers) == 0)
1825 {
1826 /* don't touch anything if we have no nameservers installed */
1827 return;
1828 }
1829
1830 file = fopen(RESOLV_CONF, "r");
1831 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1832 {
1833 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1834 RESOLV_CONF, strerror(errno));
1835 return;
1836 }
1837
1838 contents = chunk_alloca((size_t)stats.st_size);
1839
1840 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1841 {
1842 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1843 fclose(file);
1844 return;
1845 }
1846
1847 fclose(file);
1848 file = fopen(RESOLV_CONF, "w");
1849 if (file == NULL)
1850 {
1851 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1852 RESOLV_CONF, strerror(errno));
1853 return;
1854 }
1855
1856 iterator = this->dns_servers->create_iterator(this->dns_servers, TRUE);
1857 while (fetchline(&contents, &line))
1858 {
1859 bool found = FALSE;
1860 orig_line = line;
1861 if (extract_token(&token, ' ', &line) &&
1862 strncasecmp(token.ptr, "nameserver", token.len) == 0)
1863 {
1864 if (!extract_token(&token, ' ', &line))
1865 {
1866 token = line;
1867 }
1868 iterator->reset(iterator);
1869 while (iterator->iterate(iterator, (void**)&ip))
1870 {
1871 snprintf(string, sizeof(string), "%H", ip);
1872 if (strlen(string) == token.len &&
1873 strncmp(token.ptr, string, token.len) == 0)
1874 {
1875 iterator->remove(iterator);
1876 ip->destroy(ip);
1877 found = TRUE;
1878 break;
1879 }
1880 }
1881 }
1882
1883 if (!found)
1884 {
1885 /* write line untouched back to file */
1886 fwrite(orig_line.ptr, orig_line.len, 1, file);
1887 fprintf(file, "\n");
1888 }
1889 }
1890 iterator->destroy(iterator);
1891 fclose(file);
1892 }
1893
1894 /**
1895 * Implementation of ike_sa_t.add_dns_server
1896 */
1897 static void add_dns_server(private_ike_sa_t *this, host_t *dns)
1898 {
1899 FILE *file;
1900 struct stat stats;
1901 chunk_t contents;
1902
1903 DBG1(DBG_IKE, "installing DNS server %H", dns);
1904
1905 file = fopen(RESOLV_CONF, "a+");
1906 if (file == NULL || stat(RESOLV_CONF, &stats) != 0)
1907 {
1908 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1909 RESOLV_CONF, strerror(errno));
1910 return;
1911 }
1912
1913 contents = chunk_alloca(stats.st_size);
1914
1915 if (fread(contents.ptr, 1, contents.len, file) != contents.len)
1916 {
1917 DBG1(DBG_IKE, "unable to read DNS configuration file: %s", strerror(errno));
1918 fclose(file);
1919 return;
1920 }
1921
1922 fclose(file);
1923 file = fopen(RESOLV_CONF, "w");
1924 if (file == NULL)
1925 {
1926 DBG1(DBG_IKE, "unable to open DNS configuration file %s: %s",
1927 RESOLV_CONF, strerror(errno));
1928 return;
1929 }
1930
1931 if (fprintf(file, "nameserver %H # added by strongSwan, assigned by %D\n",
1932 dns, this->other_id) < 0)
1933 {
1934 DBG1(DBG_IKE, "unable to write DNS configuration: %s", strerror(errno));
1935 }
1936 else
1937 {
1938 this->dns_servers->insert_last(this->dns_servers, dns->clone(dns));
1939 }
1940 fwrite(contents.ptr, contents.len, 1, file);
1941
1942 fclose(file);
1943 }
1944
1945 /**
1946 * Implementation of ike_sa_t.destroy.
1947 */
1948 static void destroy(private_ike_sa_t *this)
1949 {
1950 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
1951
1952 DESTROY_IF(this->crypter_in);
1953 DESTROY_IF(this->crypter_out);
1954 DESTROY_IF(this->signer_in);
1955 DESTROY_IF(this->signer_out);
1956 DESTROY_IF(this->prf);
1957 DESTROY_IF(this->child_prf);
1958 chunk_free(&this->skp_verify);
1959 chunk_free(&this->skp_build);
1960
1961 if (this->my_virtual_ip)
1962 {
1963 charon->kernel_interface->del_ip(charon->kernel_interface,
1964 this->my_virtual_ip);
1965 this->my_virtual_ip->destroy(this->my_virtual_ip);
1966 }
1967 DESTROY_IF(this->other_virtual_ip);
1968
1969 remove_dns_servers(this);
1970 this->dns_servers->destroy_offset(this->dns_servers,
1971 offsetof(host_t, destroy));
1972 this->additional_addresses->destroy_offset(this->additional_addresses,
1973 offsetof(host_t, destroy));
1974
1975 DESTROY_IF(this->my_host);
1976 DESTROY_IF(this->other_host);
1977 DESTROY_IF(this->my_id);
1978 DESTROY_IF(this->other_id);
1979
1980 DESTROY_IF(this->ike_cfg);
1981 DESTROY_IF(this->peer_cfg);
1982
1983 this->ike_sa_id->destroy(this->ike_sa_id);
1984 this->task_manager->destroy(this->task_manager);
1985 free(this);
1986 }
1987
1988 /*
1989 * Described in header.
1990 */
1991 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
1992 {
1993 private_ike_sa_t *this = malloc_thing(private_ike_sa_t);
1994 static u_int32_t unique_id = 0;
1995
1996 /* Public functions */
1997 this->public.get_state = (ike_sa_state_t (*)(ike_sa_t*)) get_state;
1998 this->public.set_state = (void (*)(ike_sa_t*,ike_sa_state_t)) set_state;
1999 this->public.get_stats = (void (*)(ike_sa_t*,u_int32_t*))get_stats;
2000 this->public.get_name = (char* (*)(ike_sa_t*))get_name;
2001 this->public.process_message = (status_t (*)(ike_sa_t*, message_t*)) process_message;
2002 this->public.initiate = (status_t (*)(ike_sa_t*,child_cfg_t*)) initiate;
2003 this->public.route = (status_t (*)(ike_sa_t*,child_cfg_t*)) route;
2004 this->public.unroute = (status_t (*)(ike_sa_t*,u_int32_t)) unroute;
2005 this->public.acquire = (status_t (*)(ike_sa_t*,u_int32_t)) acquire;
2006 this->public.get_ike_cfg = (ike_cfg_t* (*)(ike_sa_t*))get_ike_cfg;
2007 this->public.set_ike_cfg = (void (*)(ike_sa_t*,ike_cfg_t*))set_ike_cfg;
2008 this->public.get_peer_cfg = (peer_cfg_t* (*)(ike_sa_t*))get_peer_cfg;
2009 this->public.set_peer_cfg = (void (*)(ike_sa_t*,peer_cfg_t*))set_peer_cfg;
2010 this->public.get_id = (ike_sa_id_t* (*)(ike_sa_t*)) get_id;
2011 this->public.get_my_host = (host_t* (*)(ike_sa_t*)) get_my_host;
2012 this->public.set_my_host = (void (*)(ike_sa_t*,host_t*)) set_my_host;
2013 this->public.get_other_host = (host_t* (*)(ike_sa_t*)) get_other_host;
2014 this->public.set_other_host = (void (*)(ike_sa_t*,host_t*)) set_other_host;
2015 this->public.update_hosts = (void(*)(ike_sa_t*, host_t *me, host_t *other))update_hosts;
2016 this->public.get_my_id = (identification_t* (*)(ike_sa_t*)) get_my_id;
2017 this->public.set_my_id = (void (*)(ike_sa_t*,identification_t*)) set_my_id;
2018 this->public.get_other_id = (identification_t* (*)(ike_sa_t*)) get_other_id;
2019 this->public.set_other_id = (void (*)(ike_sa_t*,identification_t*)) set_other_id;
2020 this->public.get_other_ca = (ca_info_t* (*)(ike_sa_t*)) get_other_ca;
2021 this->public.set_other_ca = (void (*)(ike_sa_t*,ca_info_t*)) set_other_ca;
2022 this->public.enable_extension = (void(*)(ike_sa_t*, ike_extension_t extension))enable_extension;
2023 this->public.supports_extension = (bool(*)(ike_sa_t*, ike_extension_t extension))supports_extension;
2024 this->public.set_condition = (void (*)(ike_sa_t*, ike_condition_t,bool)) set_condition;
2025 this->public.has_condition = (bool (*)(ike_sa_t*,ike_condition_t)) has_condition;
2026 this->public.create_additional_address_iterator = (iterator_t*(*)(ike_sa_t*))create_additional_address_iterator;
2027 this->public.add_additional_address = (void(*)(ike_sa_t*, host_t *host))add_additional_address;
2028 this->public.retransmit = (status_t (*)(ike_sa_t *, u_int32_t)) retransmit;
2029 this->public.delete = (status_t (*)(ike_sa_t*))delete_;
2030 this->public.destroy = (void (*)(ike_sa_t*))destroy;
2031 this->public.send_dpd = (status_t (*)(ike_sa_t*)) send_dpd;
2032 this->public.send_keepalive = (void (*)(ike_sa_t*)) send_keepalive;
2033 this->public.get_prf = (prf_t* (*)(ike_sa_t*)) get_prf;
2034 this->public.get_child_prf = (prf_t* (*)(ike_sa_t *)) get_child_prf;
2035 this->public.get_skp_verify = (chunk_t (*)(ike_sa_t *)) get_skp_verify;
2036 this->public.get_skp_build = (chunk_t (*)(ike_sa_t *)) get_skp_build;
2037 this->public.derive_keys = (status_t (*)(ike_sa_t *,proposal_t*,chunk_t,chunk_t,chunk_t,bool,prf_t*,prf_t*)) derive_keys;
2038 this->public.add_child_sa = (void (*)(ike_sa_t*,child_sa_t*)) add_child_sa;
2039 this->public.get_child_sa = (child_sa_t* (*)(ike_sa_t*,protocol_id_t,u_int32_t,bool)) get_child_sa;
2040 this->public.create_child_sa_iterator = (iterator_t* (*)(ike_sa_t*)) create_child_sa_iterator;
2041 this->public.rekey_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) rekey_child_sa;
2042 this->public.delete_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t)) delete_child_sa;
2043 this->public.destroy_child_sa = (status_t (*)(ike_sa_t*,protocol_id_t,u_int32_t))destroy_child_sa;
2044 this->public.rekey = (status_t (*)(ike_sa_t*))rekey;
2045 this->public.reestablish = (status_t (*)(ike_sa_t*))reestablish;
2046 this->public.roam = (status_t(*)(ike_sa_t*))roam;
2047 this->public.inherit = (status_t (*)(ike_sa_t*,ike_sa_t*))inherit;
2048 this->public.generate_message = (status_t (*)(ike_sa_t*,message_t*,packet_t**))generate_message;
2049 this->public.reset = (void (*)(ike_sa_t*))reset;
2050 this->public.get_unique_id = (u_int32_t (*)(ike_sa_t*))get_unique_id;
2051 this->public.set_virtual_ip = (void (*)(ike_sa_t*,bool,host_t*))set_virtual_ip;
2052 this->public.get_virtual_ip = (host_t* (*)(ike_sa_t*,bool))get_virtual_ip;
2053 this->public.add_dns_server = (void (*)(ike_sa_t*,host_t*))add_dns_server;
2054
2055 /* initialize private fields */
2056 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
2057 this->child_sas = linked_list_create();
2058 this->my_host = host_create_any(AF_INET);
2059 this->other_host = host_create_any(AF_INET);
2060 this->my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2061 this->other_id = identification_create_from_encoding(ID_ANY, chunk_empty);
2062 this->other_ca = NULL;
2063 this->extensions = 0;
2064 this->conditions = 0;
2065 this->crypter_in = NULL;
2066 this->crypter_out = NULL;
2067 this->signer_in = NULL;
2068 this->signer_out = NULL;
2069 this->prf = NULL;
2070 this->skp_verify = chunk_empty;
2071 this->skp_build = chunk_empty;
2072 this->child_prf = NULL;
2073 this->state = IKE_CREATED;
2074 this->time.inbound = this->time.outbound = time(NULL);
2075 this->time.established = 0;
2076 this->time.rekey = 0;
2077 this->time.delete = 0;
2078 this->ike_cfg = NULL;
2079 this->peer_cfg = NULL;
2080 this->task_manager = task_manager_create(&this->public);
2081 this->unique_id = ++unique_id;
2082 this->my_virtual_ip = NULL;
2083 this->other_virtual_ip = NULL;
2084 this->dns_servers = linked_list_create();
2085 this->additional_addresses = linked_list_create();
2086 this->keyingtry = 0;
2087
2088 return &this->public;
2089 }