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