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