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