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