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