Initiate and respond to quick mode task (stub)
[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 <sa/tasks/main_mode.h>
48 #include <sa/tasks/quick_mode.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_create(this->version,
723 this->ike_sa_id->is_initiator(this->ike_sa_id));
724
725 this->task_manager->reset(this->task_manager, 0, 0);
726 }
727
728 METHOD(ike_sa_t, get_keymat, keymat_t*,
729 private_ike_sa_t *this)
730 {
731 return this->keymat;
732 }
733
734 METHOD(ike_sa_t, set_virtual_ip, void,
735 private_ike_sa_t *this, bool local, host_t *ip)
736 {
737 if (local)
738 {
739 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
740 if (hydra->kernel_interface->add_ip(hydra->kernel_interface, ip,
741 this->my_host) == SUCCESS)
742 {
743 if (this->my_virtual_ip)
744 {
745 DBG1(DBG_IKE, "removing old virtual IP %H", this->my_virtual_ip);
746 hydra->kernel_interface->del_ip(hydra->kernel_interface,
747 this->my_virtual_ip);
748 }
749 DESTROY_IF(this->my_virtual_ip);
750 this->my_virtual_ip = ip->clone(ip);
751 }
752 else
753 {
754 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
755 this->my_virtual_ip = NULL;
756 }
757 }
758 else
759 {
760 DESTROY_IF(this->other_virtual_ip);
761 this->other_virtual_ip = ip->clone(ip);
762 }
763 }
764
765 METHOD(ike_sa_t, get_virtual_ip, host_t*,
766 private_ike_sa_t *this, bool local)
767 {
768 if (local)
769 {
770 return this->my_virtual_ip;
771 }
772 else
773 {
774 return this->other_virtual_ip;
775 }
776 }
777
778 METHOD(ike_sa_t, add_additional_address, void,
779 private_ike_sa_t *this, host_t *host)
780 {
781 this->additional_addresses->insert_last(this->additional_addresses, host);
782 }
783
784 METHOD(ike_sa_t, create_additional_address_enumerator, enumerator_t*,
785 private_ike_sa_t *this)
786 {
787 return this->additional_addresses->create_enumerator(
788 this->additional_addresses);
789 }
790
791 METHOD(ike_sa_t, remove_additional_addresses, void,
792 private_ike_sa_t *this)
793 {
794 enumerator_t *enumerator = create_additional_address_enumerator(this);
795 host_t *host;
796 while (enumerator->enumerate(enumerator, (void**)&host))
797 {
798 this->additional_addresses->remove_at(this->additional_addresses,
799 enumerator);
800 host->destroy(host);
801 }
802 enumerator->destroy(enumerator);
803 }
804
805 METHOD(ike_sa_t, has_mapping_changed, bool,
806 private_ike_sa_t *this, chunk_t hash)
807 {
808 if (this->nat_detection_dest.ptr == NULL)
809 {
810 this->nat_detection_dest = chunk_clone(hash);
811 return FALSE;
812 }
813 if (chunk_equals(hash, this->nat_detection_dest))
814 {
815 return FALSE;
816 }
817 free(this->nat_detection_dest.ptr);
818 this->nat_detection_dest = chunk_clone(hash);
819 return TRUE;
820 }
821
822 METHOD(ike_sa_t, set_pending_updates, void,
823 private_ike_sa_t *this, u_int32_t updates)
824 {
825 this->pending_updates = updates;
826 }
827
828 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
829 private_ike_sa_t *this)
830 {
831 return this->pending_updates;
832 }
833
834 METHOD(ike_sa_t, float_ports, void,
835 private_ike_sa_t *this)
836 {
837 /* do not switch if we have a custom port from MOBIKE/NAT */
838 if (this->my_host->get_port(this->my_host) == IKEV2_UDP_PORT)
839 {
840 this->my_host->set_port(this->my_host, IKEV2_NATT_PORT);
841 }
842 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
843 {
844 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
845 }
846 }
847
848 METHOD(ike_sa_t, update_hosts, void,
849 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
850 {
851 bool update = FALSE;
852
853 if (me == NULL)
854 {
855 me = this->my_host;
856 }
857 if (other == NULL)
858 {
859 other = this->other_host;
860 }
861
862 /* apply hosts on first received message */
863 if (this->my_host->is_anyaddr(this->my_host) ||
864 this->other_host->is_anyaddr(this->other_host))
865 {
866 set_my_host(this, me->clone(me));
867 set_other_host(this, other->clone(other));
868 update = TRUE;
869 }
870 else
871 {
872 /* update our address in any case */
873 if (!me->equals(me, this->my_host))
874 {
875 set_my_host(this, me->clone(me));
876 update = TRUE;
877 }
878
879 if (!other->equals(other, this->other_host))
880 {
881 /* update others address if we are NOT NATed */
882 if (force || !has_condition(this, COND_NAT_HERE))
883 {
884 set_other_host(this, other->clone(other));
885 update = TRUE;
886 }
887 }
888 }
889
890 /* update all associated CHILD_SAs, if required */
891 if (update)
892 {
893 enumerator_t *enumerator;
894 child_sa_t *child_sa;
895
896 enumerator = this->child_sas->create_enumerator(this->child_sas);
897 while (enumerator->enumerate(enumerator, (void**)&child_sa))
898 {
899 if (child_sa->update(child_sa, this->my_host,
900 this->other_host, this->my_virtual_ip,
901 has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
902 {
903 this->public.rekey_child_sa(&this->public,
904 child_sa->get_protocol(child_sa),
905 child_sa->get_spi(child_sa, TRUE));
906 }
907 }
908 enumerator->destroy(enumerator);
909 }
910 }
911
912 METHOD(ike_sa_t, generate_message, status_t,
913 private_ike_sa_t *this, message_t *message, packet_t **packet)
914 {
915 if (message->is_encoded(message))
916 { /* already done */
917 *packet = message->get_packet(message);
918 return SUCCESS;
919 }
920 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
921 message->set_ike_sa_id(message, this->ike_sa_id);
922 charon->bus->message(charon->bus, message, FALSE);
923 return message->generate(message,
924 this->keymat->get_aead(this->keymat, FALSE), packet);
925 }
926
927 /**
928 * send a notify back to the sender
929 */
930 static void send_notify_response(private_ike_sa_t *this, message_t *request,
931 notify_type_t type, chunk_t data)
932 {
933 message_t *response;
934 packet_t *packet;
935
936 response = message_create(IKEV2_MAJOR_VERSION, IKEV2_MINOR_VERSION);
937 response->set_exchange_type(response, request->get_exchange_type(request));
938 response->set_request(response, FALSE);
939 response->set_message_id(response, request->get_message_id(request));
940 response->add_notify(response, FALSE, type, data);
941 if (this->my_host->is_anyaddr(this->my_host))
942 {
943 this->my_host->destroy(this->my_host);
944 this->my_host = request->get_destination(request);
945 this->my_host = this->my_host->clone(this->my_host);
946 }
947 if (this->other_host->is_anyaddr(this->other_host))
948 {
949 this->other_host->destroy(this->other_host);
950 this->other_host = request->get_source(request);
951 this->other_host = this->other_host->clone(this->other_host);
952 }
953 response->set_source(response, this->my_host->clone(this->my_host));
954 response->set_destination(response, this->other_host->clone(this->other_host));
955 if (generate_message(this, response, &packet) == SUCCESS)
956 {
957 charon->sender->send(charon->sender, packet);
958 }
959 response->destroy(response);
960 }
961
962 METHOD(ike_sa_t, set_kmaddress, void,
963 private_ike_sa_t *this, host_t *local, host_t *remote)
964 {
965 DESTROY_IF(this->local_host);
966 DESTROY_IF(this->remote_host);
967 this->local_host = local->clone(local);
968 this->remote_host = remote->clone(remote);
969 }
970
971 #ifdef ME
972 METHOD(ike_sa_t, act_as_mediation_server, void,
973 private_ike_sa_t *this)
974 {
975 charon->mediation_manager->update_sa_id(charon->mediation_manager,
976 this->other_id, this->ike_sa_id);
977 this->is_mediation_server = TRUE;
978 }
979
980 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
981 private_ike_sa_t *this)
982 {
983 return this->server_reflexive_host;
984 }
985
986 METHOD(ike_sa_t, set_server_reflexive_host, void,
987 private_ike_sa_t *this, host_t *host)
988 {
989 DESTROY_IF(this->server_reflexive_host);
990 this->server_reflexive_host = host;
991 }
992
993 METHOD(ike_sa_t, get_connect_id, chunk_t,
994 private_ike_sa_t *this)
995 {
996 return this->connect_id;
997 }
998
999 METHOD(ike_sa_t, respond, status_t,
1000 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
1001 {
1002 ike_me_t *task = ike_me_create(&this->public, TRUE);
1003 task->respond(task, peer_id, connect_id);
1004 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1005 return this->task_manager->initiate(this->task_manager);
1006 }
1007
1008 METHOD(ike_sa_t, callback, status_t,
1009 private_ike_sa_t *this, identification_t *peer_id)
1010 {
1011 ike_me_t *task = ike_me_create(&this->public, TRUE);
1012 task->callback(task, peer_id);
1013 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1014 return this->task_manager->initiate(this->task_manager);
1015 }
1016
1017 METHOD(ike_sa_t, relay, status_t,
1018 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
1019 chunk_t connect_key, linked_list_t *endpoints, bool response)
1020 {
1021 ike_me_t *task = ike_me_create(&this->public, TRUE);
1022 task->relay(task, requester, connect_id, connect_key, endpoints, response);
1023 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1024 return this->task_manager->initiate(this->task_manager);
1025 }
1026
1027 METHOD(ike_sa_t, initiate_mediation, status_t,
1028 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1029 {
1030 ike_me_t *task = ike_me_create(&this->public, TRUE);
1031 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1032 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1033 return this->task_manager->initiate(this->task_manager);
1034 }
1035
1036 METHOD(ike_sa_t, initiate_mediated, status_t,
1037 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1038 {
1039 set_my_host(this, me->clone(me));
1040 set_other_host(this, other->clone(other));
1041 chunk_free(&this->connect_id);
1042 this->connect_id = chunk_clone(connect_id);
1043 return this->task_manager->initiate(this->task_manager);
1044 }
1045 #endif /* ME */
1046
1047 /**
1048 * Resolve DNS host in configuration
1049 */
1050 static void resolve_hosts(private_ike_sa_t *this)
1051 {
1052 host_t *host;
1053
1054 if (this->remote_host)
1055 {
1056 host = this->remote_host->clone(this->remote_host);
1057 host->set_port(host, IKEV2_UDP_PORT);
1058 }
1059 else
1060 {
1061 host = host_create_from_dns(this->ike_cfg->get_other_addr(this->ike_cfg),
1062 0, this->ike_cfg->get_other_port(this->ike_cfg));
1063 }
1064 if (host)
1065 {
1066 set_other_host(this, host);
1067 }
1068
1069 if (this->local_host)
1070 {
1071 host = this->local_host->clone(this->local_host);
1072 host->set_port(host, IKEV2_UDP_PORT);
1073 }
1074 else
1075 {
1076 int family = 0;
1077
1078 /* use same address family as for other */
1079 if (!this->other_host->is_anyaddr(this->other_host))
1080 {
1081 family = this->other_host->get_family(this->other_host);
1082 }
1083 host = host_create_from_dns(this->ike_cfg->get_my_addr(this->ike_cfg),
1084 family, this->ike_cfg->get_my_port(this->ike_cfg));
1085
1086 if (host && host->is_anyaddr(host) &&
1087 !this->other_host->is_anyaddr(this->other_host))
1088 {
1089 host->destroy(host);
1090 host = hydra->kernel_interface->get_source_addr(
1091 hydra->kernel_interface, this->other_host, NULL);
1092 if (host)
1093 {
1094 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1095 }
1096 else
1097 { /* fallback to address family specific %any(6), if configured */
1098 host = host_create_from_dns(
1099 this->ike_cfg->get_my_addr(this->ike_cfg),
1100 0, this->ike_cfg->get_my_port(this->ike_cfg));
1101 }
1102 }
1103 }
1104 if (host)
1105 {
1106 set_my_host(this, host);
1107 }
1108 }
1109
1110 METHOD(ike_sa_t, initiate, status_t,
1111 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1112 traffic_selector_t *tsi, traffic_selector_t *tsr)
1113 {
1114 task_t *task;
1115
1116 if (this->state == IKE_CREATED)
1117 {
1118 resolve_hosts(this);
1119
1120 if (this->other_host->is_anyaddr(this->other_host)
1121 #ifdef ME
1122 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1123 #endif /* ME */
1124 )
1125 {
1126 child_cfg->destroy(child_cfg);
1127 DBG1(DBG_IKE, "unable to initiate to %%any");
1128 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1129 return DESTROY_ME;
1130 }
1131
1132 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1133
1134 if (this->version == IKEV1)
1135 {
1136 task = (task_t*)main_mode_create(&this->public, TRUE);
1137 this->task_manager->queue_task(this->task_manager, task);
1138 }
1139 else
1140 {
1141 task = (task_t*)ike_vendor_create(&this->public, TRUE);
1142 this->task_manager->queue_task(this->task_manager, task);
1143 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1144 this->task_manager->queue_task(this->task_manager, task);
1145 task = (task_t*)ike_natd_create(&this->public, TRUE);
1146 this->task_manager->queue_task(this->task_manager, task);
1147 task = (task_t*)ike_cert_pre_create(&this->public, TRUE);
1148 this->task_manager->queue_task(this->task_manager, task);
1149 task = (task_t*)ike_auth_create(&this->public, TRUE);
1150 this->task_manager->queue_task(this->task_manager, task);
1151 task = (task_t*)ike_cert_post_create(&this->public, TRUE);
1152 this->task_manager->queue_task(this->task_manager, task);
1153 task = (task_t*)ike_config_create(&this->public, TRUE);
1154 this->task_manager->queue_task(this->task_manager, task);
1155 task = (task_t*)ike_auth_lifetime_create(&this->public, TRUE);
1156 this->task_manager->queue_task(this->task_manager, task);
1157 if (this->peer_cfg->use_mobike(this->peer_cfg))
1158 {
1159 task = (task_t*)ike_mobike_create(&this->public, TRUE);
1160 this->task_manager->queue_task(this->task_manager, task);
1161 }
1162 #ifdef ME
1163 task = (task_t*)ike_me_create(&this->public, TRUE);
1164 this->task_manager->queue_task(this->task_manager, task);
1165 #endif /* ME */
1166 }
1167 }
1168
1169 #ifdef ME
1170 if (this->peer_cfg->is_mediation(this->peer_cfg))
1171 {
1172 if (this->state == IKE_ESTABLISHED)
1173 {
1174 /* mediation connection is already established, retrigger state
1175 * change to notify bus listeners */
1176 DBG1(DBG_IKE, "mediation connection is already up");
1177 set_state(this, IKE_ESTABLISHED);
1178 }
1179 DESTROY_IF(child_cfg);
1180 }
1181 else
1182 #endif /* ME */
1183 {
1184 /* normal IKE_SA with CHILD_SA */
1185 if (this->version == IKEV2)
1186 {
1187 task = (task_t*)child_create_create(&this->public, child_cfg, FALSE,
1188 tsi, tsr);
1189 if (reqid)
1190 {
1191 child_create_t *child_create = (child_create_t*)task;
1192 child_create->use_reqid(child_create, reqid);
1193 }
1194 }
1195 else
1196 {
1197 task = (task_t*)quick_mode_create(&this->public, child_cfg,
1198 tsi, tsr);
1199 }
1200 this->task_manager->queue_task(this->task_manager, task);
1201
1202 #ifdef ME
1203 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1204 {
1205 /* mediated connection, initiate mediation process */
1206 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1207 lib->processor->queue_job(lib->processor, job);
1208 return SUCCESS;
1209 }
1210 #endif /* ME */
1211 }
1212
1213 return this->task_manager->initiate(this->task_manager);
1214 }
1215
1216 METHOD(ike_sa_t, process_message, status_t,
1217 private_ike_sa_t *this, message_t *message)
1218 {
1219 status_t status;
1220 bool is_request;
1221 u_int8_t type = 0;
1222
1223 if (this->state == IKE_PASSIVE)
1224 { /* do not handle messages in passive state */
1225 return FAILED;
1226 }
1227
1228 is_request = message->get_request(message);
1229
1230 status = message->parse_body(message,
1231 this->keymat->get_aead(this->keymat, TRUE));
1232 if (status == SUCCESS)
1233 { /* check for unsupported critical payloads */
1234 enumerator_t *enumerator;
1235 unknown_payload_t *unknown;
1236 payload_t *payload;
1237
1238 enumerator = message->create_payload_enumerator(message);
1239 while (enumerator->enumerate(enumerator, &payload))
1240 {
1241 unknown = (unknown_payload_t*)payload;
1242 type = payload->get_type(payload);
1243 if (!payload_is_known(type) &&
1244 unknown->is_critical(unknown))
1245 {
1246 DBG1(DBG_ENC, "payload type %N is not supported, "
1247 "but its critical!", payload_type_names, type);
1248 status = NOT_SUPPORTED;
1249 }
1250 }
1251 enumerator->destroy(enumerator);
1252 }
1253 if (status != SUCCESS)
1254 {
1255 if (is_request)
1256 {
1257 switch (status)
1258 {
1259 case NOT_SUPPORTED:
1260 DBG1(DBG_IKE, "critical unknown payloads found");
1261 if (is_request)
1262 {
1263 send_notify_response(this, message,
1264 UNSUPPORTED_CRITICAL_PAYLOAD,
1265 chunk_from_thing(type));
1266 this->task_manager->incr_mid(this->task_manager, FALSE);
1267 }
1268 break;
1269 case PARSE_ERROR:
1270 DBG1(DBG_IKE, "message parsing failed");
1271 if (is_request)
1272 {
1273 send_notify_response(this, message,
1274 INVALID_SYNTAX, chunk_empty);
1275 this->task_manager->incr_mid(this->task_manager, FALSE);
1276 }
1277 break;
1278 case VERIFY_ERROR:
1279 DBG1(DBG_IKE, "message verification failed");
1280 if (is_request)
1281 {
1282 send_notify_response(this, message,
1283 INVALID_SYNTAX, chunk_empty);
1284 this->task_manager->incr_mid(this->task_manager, FALSE);
1285 }
1286 break;
1287 case FAILED:
1288 DBG1(DBG_IKE, "integrity check failed");
1289 /* ignored */
1290 break;
1291 case INVALID_STATE:
1292 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1293 default:
1294 break;
1295 }
1296 }
1297 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1298 exchange_type_names, message->get_exchange_type(message),
1299 message->get_request(message) ? "request" : "response",
1300 message->get_message_id(message));
1301
1302 if (this->state == IKE_CREATED)
1303 { /* invalid initiation attempt, close SA */
1304 return DESTROY_ME;
1305 }
1306 }
1307 else
1308 {
1309 /* if this IKE_SA is virgin, we check for a config */
1310 if (this->ike_cfg == NULL)
1311 {
1312 job_t *job;
1313 host_t *me = message->get_destination(message),
1314 *other = message->get_source(message);
1315 this->ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1316 me, other);
1317 if (this->ike_cfg == NULL)
1318 {
1319 /* no config found for these hosts, destroy */
1320 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1321 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1322 send_notify_response(this, message,
1323 NO_PROPOSAL_CHOSEN, chunk_empty);
1324 return DESTROY_ME;
1325 }
1326 /* add a timeout if peer does not establish it completely */
1327 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, FALSE);
1328 lib->scheduler->schedule_job(lib->scheduler, job,
1329 lib->settings->get_int(lib->settings,
1330 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
1331 }
1332 this->stats[STAT_INBOUND] = time_monotonic(NULL);
1333 status = this->task_manager->process_message(this->task_manager,
1334 message);
1335 if (message->get_exchange_type(message) == IKE_AUTH &&
1336 this->state == IKE_ESTABLISHED &&
1337 lib->settings->get_bool(lib->settings,
1338 "charon.flush_auth_cfg", FALSE))
1339 { /* authentication completed */
1340 flush_auth_cfgs(this);
1341 }
1342 }
1343 return status;
1344 }
1345
1346 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1347 private_ike_sa_t *this)
1348 {
1349 return this->ike_sa_id;
1350 }
1351
1352 METHOD(ike_sa_t, get_version, ike_version_t,
1353 private_ike_sa_t *this)
1354 {
1355 return this->version;
1356 }
1357
1358 METHOD(ike_sa_t, get_my_id, identification_t*,
1359 private_ike_sa_t *this)
1360 {
1361 return this->my_id;
1362 }
1363
1364 METHOD(ike_sa_t, set_my_id, void,
1365 private_ike_sa_t *this, identification_t *me)
1366 {
1367 DESTROY_IF(this->my_id);
1368 this->my_id = me;
1369 }
1370
1371 METHOD(ike_sa_t, get_other_id, identification_t*,
1372 private_ike_sa_t *this)
1373 {
1374 return this->other_id;
1375 }
1376
1377 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1378 private_ike_sa_t *this)
1379 {
1380 identification_t *id = NULL, *current;
1381 enumerator_t *enumerator;
1382 auth_cfg_t *cfg;
1383
1384 enumerator = this->other_auths->create_enumerator(this->other_auths);
1385 while (enumerator->enumerate(enumerator, &cfg))
1386 {
1387 /* prefer EAP-Identity of last round */
1388 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1389 if (!current || current->get_type(current) == ID_ANY)
1390 {
1391 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1392 }
1393 if (current && current->get_type(current) != ID_ANY)
1394 {
1395 id = current;
1396 continue;
1397 }
1398 }
1399 enumerator->destroy(enumerator);
1400 if (id)
1401 {
1402 return id;
1403 }
1404 return this->other_id;
1405 }
1406
1407 METHOD(ike_sa_t, set_other_id, void,
1408 private_ike_sa_t *this, identification_t *other)
1409 {
1410 DESTROY_IF(this->other_id);
1411 this->other_id = other;
1412 }
1413
1414 METHOD(ike_sa_t, add_child_sa, void,
1415 private_ike_sa_t *this, child_sa_t *child_sa)
1416 {
1417 this->child_sas->insert_last(this->child_sas, child_sa);
1418 }
1419
1420 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1421 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1422 {
1423 enumerator_t *enumerator;
1424 child_sa_t *current, *found = NULL;
1425
1426 enumerator = this->child_sas->create_enumerator(this->child_sas);
1427 while (enumerator->enumerate(enumerator, (void**)&current))
1428 {
1429 if (current->get_spi(current, inbound) == spi &&
1430 current->get_protocol(current) == protocol)
1431 {
1432 found = current;
1433 }
1434 }
1435 enumerator->destroy(enumerator);
1436 return found;
1437 }
1438
1439 METHOD(ike_sa_t, get_child_count, int,
1440 private_ike_sa_t *this)
1441 {
1442 return this->child_sas->get_count(this->child_sas);
1443 }
1444
1445 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1446 private_ike_sa_t *this)
1447 {
1448 return this->child_sas->create_enumerator(this->child_sas);
1449 }
1450
1451 METHOD(ike_sa_t, remove_child_sa, void,
1452 private_ike_sa_t *this, enumerator_t *enumerator)
1453 {
1454 this->child_sas->remove_at(this->child_sas, enumerator);
1455 }
1456
1457 METHOD(ike_sa_t, rekey_child_sa, status_t,
1458 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1459 {
1460 child_rekey_t *child_rekey;
1461
1462 child_rekey = child_rekey_create(&this->public, protocol, spi);
1463 this->task_manager->queue_task(this->task_manager, &child_rekey->task);
1464 return this->task_manager->initiate(this->task_manager);
1465 }
1466
1467 METHOD(ike_sa_t, delete_child_sa, status_t,
1468 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1469 {
1470 child_delete_t *child_delete;
1471
1472 child_delete = child_delete_create(&this->public, protocol, spi);
1473 this->task_manager->queue_task(this->task_manager, &child_delete->task);
1474 return this->task_manager->initiate(this->task_manager);
1475 }
1476
1477 METHOD(ike_sa_t, destroy_child_sa, status_t,
1478 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1479 {
1480 enumerator_t *enumerator;
1481 child_sa_t *child_sa;
1482 status_t status = NOT_FOUND;
1483
1484 enumerator = this->child_sas->create_enumerator(this->child_sas);
1485 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1486 {
1487 if (child_sa->get_protocol(child_sa) == protocol &&
1488 child_sa->get_spi(child_sa, TRUE) == spi)
1489 {
1490 this->child_sas->remove_at(this->child_sas, enumerator);
1491 child_sa->destroy(child_sa);
1492 status = SUCCESS;
1493 break;
1494 }
1495 }
1496 enumerator->destroy(enumerator);
1497 return status;
1498 }
1499
1500 METHOD(ike_sa_t, delete_, status_t,
1501 private_ike_sa_t *this)
1502 {
1503 ike_delete_t *ike_delete;
1504
1505 switch (this->state)
1506 {
1507 case IKE_ESTABLISHED:
1508 case IKE_REKEYING:
1509 ike_delete = ike_delete_create(&this->public, TRUE);
1510 this->task_manager->queue_task(this->task_manager, &ike_delete->task);
1511 return this->task_manager->initiate(this->task_manager);
1512 case IKE_CREATED:
1513 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1514 break;
1515 case IKE_PASSIVE:
1516 break;
1517 default:
1518 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1519 "without notification", ike_sa_state_names, this->state);
1520 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1521 break;
1522 }
1523 return DESTROY_ME;
1524 }
1525
1526 METHOD(ike_sa_t, rekey, status_t,
1527 private_ike_sa_t *this)
1528 {
1529 ike_rekey_t *ike_rekey;
1530
1531 ike_rekey = ike_rekey_create(&this->public, TRUE);
1532
1533 this->task_manager->queue_task(this->task_manager, &ike_rekey->task);
1534 return this->task_manager->initiate(this->task_manager);
1535 }
1536
1537 METHOD(ike_sa_t, reauth, status_t,
1538 private_ike_sa_t *this)
1539 {
1540 task_t *task;
1541
1542 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1543 * If the peer does not support RFC4478, there is no way to keep the
1544 * IKE_SA up. */
1545 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1546 {
1547 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1548 if (this->other_virtual_ip != NULL ||
1549 has_condition(this, COND_EAP_AUTHENTICATED)
1550 #ifdef ME
1551 /* as mediation server we too cannot reauth the IKE_SA */
1552 || this->is_mediation_server
1553 #endif /* ME */
1554 )
1555 {
1556 time_t now = time_monotonic(NULL);
1557
1558 DBG1(DBG_IKE, "IKE_SA will timeout in %V",
1559 &now, &this->stats[STAT_DELETE]);
1560 return FAILED;
1561 }
1562 else
1563 {
1564 DBG1(DBG_IKE, "reauthenticating actively");
1565 }
1566 }
1567 task = (task_t*)ike_reauth_create(&this->public);
1568 this->task_manager->queue_task(this->task_manager, task);
1569
1570 return this->task_manager->initiate(this->task_manager);
1571 }
1572
1573 METHOD(ike_sa_t, reestablish, status_t,
1574 private_ike_sa_t *this)
1575 {
1576 ike_sa_t *new;
1577 host_t *host;
1578 action_t action;
1579 enumerator_t *enumerator;
1580 child_sa_t *child_sa;
1581 child_cfg_t *child_cfg;
1582 bool restart = FALSE;
1583 status_t status = FAILED;
1584
1585 /* check if we have children to keep up at all */
1586 enumerator = this->child_sas->create_enumerator(this->child_sas);
1587 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1588 {
1589 if (this->state == IKE_DELETING)
1590 {
1591 action = child_sa->get_close_action(child_sa);
1592 }
1593 else
1594 {
1595 action = child_sa->get_dpd_action(child_sa);
1596 }
1597 switch (action)
1598 {
1599 case ACTION_RESTART:
1600 restart = TRUE;
1601 break;
1602 case ACTION_ROUTE:
1603 charon->traps->install(charon->traps, this->peer_cfg,
1604 child_sa->get_config(child_sa));
1605 break;
1606 default:
1607 break;
1608 }
1609 }
1610 enumerator->destroy(enumerator);
1611 #ifdef ME
1612 /* mediation connections have no children, keep them up anyway */
1613 if (this->peer_cfg->is_mediation(this->peer_cfg))
1614 {
1615 restart = TRUE;
1616 }
1617 #endif /* ME */
1618 if (!restart)
1619 {
1620 return FAILED;
1621 }
1622
1623 /* check if we are able to reestablish this IKE_SA */
1624 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1625 (this->other_virtual_ip != NULL ||
1626 has_condition(this, COND_EAP_AUTHENTICATED)
1627 #ifdef ME
1628 || this->is_mediation_server
1629 #endif /* ME */
1630 ))
1631 {
1632 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1633 return FAILED;
1634 }
1635
1636 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1637 this->version, TRUE);
1638 new->set_peer_cfg(new, this->peer_cfg);
1639 host = this->other_host;
1640 new->set_other_host(new, host->clone(host));
1641 host = this->my_host;
1642 new->set_my_host(new, host->clone(host));
1643 /* if we already have a virtual IP, we reuse it */
1644 host = this->my_virtual_ip;
1645 if (host)
1646 {
1647 new->set_virtual_ip(new, TRUE, host);
1648 }
1649
1650 #ifdef ME
1651 if (this->peer_cfg->is_mediation(this->peer_cfg))
1652 {
1653 status = new->initiate(new, NULL, 0, NULL, NULL);
1654 }
1655 else
1656 #endif /* ME */
1657 {
1658 enumerator = this->child_sas->create_enumerator(this->child_sas);
1659 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1660 {
1661 if (this->state == IKE_DELETING)
1662 {
1663 action = child_sa->get_close_action(child_sa);
1664 }
1665 else
1666 {
1667 action = child_sa->get_dpd_action(child_sa);
1668 }
1669 switch (action)
1670 {
1671 case ACTION_RESTART:
1672 child_cfg = child_sa->get_config(child_sa);
1673 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1674 child_cfg->get_name(child_cfg));
1675 child_cfg->get_ref(child_cfg);
1676 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1677 break;
1678 default:
1679 continue;
1680 }
1681 if (status == DESTROY_ME)
1682 {
1683 break;
1684 }
1685 }
1686 enumerator->destroy(enumerator);
1687 }
1688
1689 if (status == DESTROY_ME)
1690 {
1691 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1692 status = FAILED;
1693 }
1694 else
1695 {
1696 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1697 status = SUCCESS;
1698 }
1699 charon->bus->set_sa(charon->bus, &this->public);
1700 return status;
1701 }
1702
1703 /**
1704 * Requeue the IKE_SA_INIT tasks for initiation, if required
1705 */
1706 static void requeue_init_tasks(private_ike_sa_t *this)
1707 {
1708 enumerator_t *enumerator;
1709 bool has_init = FALSE;
1710 task_t *task;
1711
1712 /* if we have advanced to IKE_AUTH, the IKE_INIT and related tasks
1713 * have already completed. Recreate them if necessary. */
1714 enumerator = this->task_manager->create_task_enumerator(
1715 this->task_manager, TASK_QUEUE_QUEUED);
1716 while (enumerator->enumerate(enumerator, &task))
1717 {
1718 if (task->get_type(task) == IKE_INIT)
1719 {
1720 has_init = TRUE;
1721 break;
1722 }
1723 }
1724 enumerator->destroy(enumerator);
1725
1726 if (!has_init)
1727 {
1728 task = (task_t*)ike_vendor_create(&this->public, TRUE);
1729 this->task_manager->queue_task(this->task_manager, task);
1730 task = (task_t*)ike_natd_create(&this->public, TRUE);
1731 this->task_manager->queue_task(this->task_manager, task);
1732 task = (task_t*)ike_init_create(&this->public, TRUE, NULL);
1733 this->task_manager->queue_task(this->task_manager, task);
1734 }
1735 }
1736
1737 METHOD(ike_sa_t, retransmit, status_t,
1738 private_ike_sa_t *this, u_int32_t message_id)
1739 {
1740 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1741 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1742 {
1743 /* send a proper signal to brief interested bus listeners */
1744 switch (this->state)
1745 {
1746 case IKE_CONNECTING:
1747 {
1748 /* retry IKE_SA_INIT if we have multiple keyingtries */
1749 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1750 this->keyingtry++;
1751 if (tries == 0 || tries > this->keyingtry)
1752 {
1753 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1754 this->keyingtry + 1, tries);
1755 reset(this);
1756 requeue_init_tasks(this);
1757 return this->task_manager->initiate(this->task_manager);
1758 }
1759 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1760 break;
1761 }
1762 case IKE_DELETING:
1763 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1764 break;
1765 case IKE_REKEYING:
1766 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1767 /* FALL */
1768 default:
1769 reestablish(this);
1770 break;
1771 }
1772 return DESTROY_ME;
1773 }
1774 return SUCCESS;
1775 }
1776
1777 METHOD(ike_sa_t, set_auth_lifetime, void,
1778 private_ike_sa_t *this, u_int32_t lifetime)
1779 {
1780 u_int32_t reduction = this->peer_cfg->get_over_time(this->peer_cfg);
1781 u_int32_t reauth_time = time_monotonic(NULL) + lifetime - reduction;
1782
1783 if (lifetime < reduction)
1784 {
1785 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, starting reauthentication",
1786 lifetime);
1787 lib->processor->queue_job(lib->processor,
1788 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1789 }
1790 else if (this->stats[STAT_REAUTH] == 0 ||
1791 this->stats[STAT_REAUTH] > reauth_time)
1792 {
1793 this->stats[STAT_REAUTH] = reauth_time;
1794 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1795 " in %ds", lifetime, lifetime - reduction);
1796 lib->scheduler->schedule_job(lib->scheduler,
1797 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1798 lifetime - reduction);
1799 }
1800 else
1801 {
1802 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1803 "reauthentication already scheduled in %ds", lifetime,
1804 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1805 }
1806 }
1807
1808 /**
1809 * Check if the current combination of source and destination address is still
1810 * valid.
1811 */
1812 static bool is_current_path_valid(private_ike_sa_t *this)
1813 {
1814 bool valid = FALSE;
1815 host_t *src;
1816 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1817 this->other_host, this->my_host);
1818 if (src)
1819 {
1820 if (src->ip_equals(src, this->my_host))
1821 {
1822 valid = TRUE;
1823 }
1824 src->destroy(src);
1825 }
1826 return valid;
1827 }
1828
1829 /**
1830 * Check if we have any path avialable for this IKE SA.
1831 */
1832 static bool is_any_path_valid(private_ike_sa_t *this)
1833 {
1834 bool valid = FALSE;
1835 enumerator_t *enumerator;
1836 host_t *src, *addr;
1837 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1838 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1839 this->other_host, NULL);
1840 if (!src)
1841 {
1842 enumerator = this->additional_addresses->create_enumerator(
1843 this->additional_addresses);
1844 while (enumerator->enumerate(enumerator, &addr))
1845 {
1846 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1847 src = hydra->kernel_interface->get_source_addr(
1848 hydra->kernel_interface, addr, NULL);
1849 if (src)
1850 {
1851 break;
1852 }
1853 }
1854 enumerator->destroy(enumerator);
1855 }
1856 if (src)
1857 {
1858 valid = TRUE;
1859 src->destroy(src);
1860 }
1861 return valid;
1862 }
1863
1864 METHOD(ike_sa_t, roam, status_t,
1865 private_ike_sa_t *this, bool address)
1866 {
1867 ike_mobike_t *mobike;
1868
1869 switch (this->state)
1870 {
1871 case IKE_CREATED:
1872 case IKE_DELETING:
1873 case IKE_DESTROYING:
1874 case IKE_PASSIVE:
1875 return SUCCESS;
1876 default:
1877 break;
1878 }
1879
1880 /* keep existing path if possible */
1881 if (is_current_path_valid(this))
1882 {
1883 DBG2(DBG_IKE, "keeping connection path %H - %H",
1884 this->my_host, this->other_host);
1885 set_condition(this, COND_STALE, FALSE);
1886
1887 if (supports_extension(this, EXT_MOBIKE) && address)
1888 { /* if any addresses changed, send an updated list */
1889 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1890 mobike = ike_mobike_create(&this->public, TRUE);
1891 mobike->addresses(mobike);
1892 this->task_manager->queue_task(this->task_manager,
1893 (task_t*)mobike);
1894 return this->task_manager->initiate(this->task_manager);
1895 }
1896 return SUCCESS;
1897 }
1898
1899 if (!is_any_path_valid(this))
1900 {
1901 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1902 this->other_host);
1903 set_condition(this, COND_STALE, TRUE);
1904 return SUCCESS;
1905 }
1906 set_condition(this, COND_STALE, FALSE);
1907
1908 /* update addresses with mobike, if supported ... */
1909 if (supports_extension(this, EXT_MOBIKE))
1910 {
1911 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1912 { /* responder updates the peer about changed address config */
1913 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1914 "implicitly requesting an address change");
1915 address = TRUE;
1916 }
1917 else
1918 {
1919 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1920 }
1921 mobike = ike_mobike_create(&this->public, TRUE);
1922 mobike->roam(mobike, address);
1923 this->task_manager->queue_task(this->task_manager, (task_t*)mobike);
1924 return this->task_manager->initiate(this->task_manager);
1925 }
1926
1927 /* ... reauth if not */
1928 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1929 { /* responder does not reauthenticate */
1930 set_condition(this, COND_STALE, TRUE);
1931 return SUCCESS;
1932 }
1933 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1934 return reauth(this);
1935 }
1936
1937 METHOD(ike_sa_t, add_configuration_attribute, void,
1938 private_ike_sa_t *this, attribute_handler_t *handler,
1939 configuration_attribute_type_t type, chunk_t data)
1940 {
1941 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1942
1943 entry->handler = handler;
1944 entry->type = type;
1945 entry->data = chunk_clone(data);
1946
1947 this->attributes->insert_last(this->attributes, entry);
1948 }
1949
1950 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1951 private_ike_sa_t *this, task_queue_t queue)
1952 {
1953 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1954 }
1955
1956 METHOD(ike_sa_t, inherit, void,
1957 private_ike_sa_t *this, ike_sa_t *other_public)
1958 {
1959 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1960 child_sa_t *child_sa;
1961 attribute_entry_t *entry;
1962 enumerator_t *enumerator;
1963 auth_cfg_t *cfg;
1964
1965 /* apply hosts and ids */
1966 this->my_host->destroy(this->my_host);
1967 this->other_host->destroy(this->other_host);
1968 this->my_id->destroy(this->my_id);
1969 this->other_id->destroy(this->other_id);
1970 this->my_host = other->my_host->clone(other->my_host);
1971 this->other_host = other->other_host->clone(other->other_host);
1972 this->my_id = other->my_id->clone(other->my_id);
1973 this->other_id = other->other_id->clone(other->other_id);
1974
1975 /* apply virtual assigned IPs... */
1976 if (other->my_virtual_ip)
1977 {
1978 this->my_virtual_ip = other->my_virtual_ip;
1979 other->my_virtual_ip = NULL;
1980 }
1981 if (other->other_virtual_ip)
1982 {
1983 this->other_virtual_ip = other->other_virtual_ip;
1984 other->other_virtual_ip = NULL;
1985 }
1986
1987 /* authentication information */
1988 enumerator = other->my_auths->create_enumerator(other->my_auths);
1989 while (enumerator->enumerate(enumerator, &cfg))
1990 {
1991 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1992 }
1993 enumerator->destroy(enumerator);
1994 enumerator = other->other_auths->create_enumerator(other->other_auths);
1995 while (enumerator->enumerate(enumerator, &cfg))
1996 {
1997 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1998 }
1999 enumerator->destroy(enumerator);
2000
2001 /* ... and configuration attributes */
2002 while (other->attributes->remove_last(other->attributes,
2003 (void**)&entry) == SUCCESS)
2004 {
2005 this->attributes->insert_first(this->attributes, entry);
2006 }
2007
2008 /* inherit all conditions */
2009 this->conditions = other->conditions;
2010 if (this->conditions & COND_NAT_HERE)
2011 {
2012 send_keepalive(this);
2013 }
2014
2015 #ifdef ME
2016 if (other->is_mediation_server)
2017 {
2018 act_as_mediation_server(this);
2019 }
2020 else if (other->server_reflexive_host)
2021 {
2022 this->server_reflexive_host = other->server_reflexive_host->clone(
2023 other->server_reflexive_host);
2024 }
2025 #endif /* ME */
2026
2027 /* adopt all children */
2028 while (other->child_sas->remove_last(other->child_sas,
2029 (void**)&child_sa) == SUCCESS)
2030 {
2031 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
2032 }
2033
2034 /* move pending tasks to the new IKE_SA */
2035 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2036
2037 /* reauthentication timeout survives a rekeying */
2038 if (other->stats[STAT_REAUTH])
2039 {
2040 time_t reauth, delete, now = time_monotonic(NULL);
2041
2042 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2043 reauth = this->stats[STAT_REAUTH] - now;
2044 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2045 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2046 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2047 "lifetime reduced to %ds", reauth, delete);
2048 lib->scheduler->schedule_job(lib->scheduler,
2049 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2050 lib->scheduler->schedule_job(lib->scheduler,
2051 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2052 }
2053 }
2054
2055 METHOD(ike_sa_t, destroy, void,
2056 private_ike_sa_t *this)
2057 {
2058 attribute_entry_t *entry;
2059
2060 charon->bus->set_sa(charon->bus, &this->public);
2061
2062 set_state(this, IKE_DESTROYING);
2063 this->task_manager->destroy(this->task_manager);
2064
2065 /* remove attributes first, as we pass the IKE_SA to the handler */
2066 while (this->attributes->remove_last(this->attributes,
2067 (void**)&entry) == SUCCESS)
2068 {
2069 hydra->attributes->release(hydra->attributes, entry->handler,
2070 this->other_id, entry->type, entry->data);
2071 free(entry->data.ptr);
2072 free(entry);
2073 }
2074 this->attributes->destroy(this->attributes);
2075
2076 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
2077
2078 /* unset SA after here to avoid usage by the listeners */
2079 charon->bus->set_sa(charon->bus, NULL);
2080
2081 this->keymat->destroy(this->keymat);
2082
2083 if (this->my_virtual_ip)
2084 {
2085 hydra->kernel_interface->del_ip(hydra->kernel_interface,
2086 this->my_virtual_ip);
2087 this->my_virtual_ip->destroy(this->my_virtual_ip);
2088 }
2089 if (this->other_virtual_ip)
2090 {
2091 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
2092 {
2093 hydra->attributes->release_address(hydra->attributes,
2094 this->peer_cfg->get_pool(this->peer_cfg),
2095 this->other_virtual_ip, get_other_eap_id(this));
2096 }
2097 this->other_virtual_ip->destroy(this->other_virtual_ip);
2098 }
2099 this->additional_addresses->destroy_offset(this->additional_addresses,
2100 offsetof(host_t, destroy));
2101 #ifdef ME
2102 if (this->is_mediation_server)
2103 {
2104 charon->mediation_manager->remove(charon->mediation_manager,
2105 this->ike_sa_id);
2106 }
2107 DESTROY_IF(this->server_reflexive_host);
2108 chunk_free(&this->connect_id);
2109 #endif /* ME */
2110 free(this->nat_detection_dest.ptr);
2111
2112 DESTROY_IF(this->my_host);
2113 DESTROY_IF(this->other_host);
2114 DESTROY_IF(this->my_id);
2115 DESTROY_IF(this->other_id);
2116 DESTROY_IF(this->local_host);
2117 DESTROY_IF(this->remote_host);
2118
2119 DESTROY_IF(this->ike_cfg);
2120 DESTROY_IF(this->peer_cfg);
2121 DESTROY_IF(this->proposal);
2122 this->my_auth->destroy(this->my_auth);
2123 this->other_auth->destroy(this->other_auth);
2124 this->my_auths->destroy_offset(this->my_auths,
2125 offsetof(auth_cfg_t, destroy));
2126 this->other_auths->destroy_offset(this->other_auths,
2127 offsetof(auth_cfg_t, destroy));
2128
2129 this->ike_sa_id->destroy(this->ike_sa_id);
2130 free(this);
2131 }
2132
2133 /*
2134 * Described in header.
2135 */
2136 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, ike_version_t version)
2137 {
2138 private_ike_sa_t *this;
2139 static u_int32_t unique_id = 0;
2140
2141 INIT(this,
2142 .public = {
2143 .get_version = _get_version,
2144 .get_state = _get_state,
2145 .set_state = _set_state,
2146 .get_name = _get_name,
2147 .get_statistic = _get_statistic,
2148 .process_message = _process_message,
2149 .initiate = _initiate,
2150 .get_ike_cfg = _get_ike_cfg,
2151 .set_ike_cfg = _set_ike_cfg,
2152 .get_peer_cfg = _get_peer_cfg,
2153 .set_peer_cfg = _set_peer_cfg,
2154 .get_auth_cfg = _get_auth_cfg,
2155 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2156 .add_auth_cfg = _add_auth_cfg,
2157 .get_proposal = _get_proposal,
2158 .set_proposal = _set_proposal,
2159 .get_id = _get_id,
2160 .get_my_host = _get_my_host,
2161 .set_my_host = _set_my_host,
2162 .get_other_host = _get_other_host,
2163 .set_other_host = _set_other_host,
2164 .set_message_id = _set_message_id,
2165 .float_ports = _float_ports,
2166 .update_hosts = _update_hosts,
2167 .get_my_id = _get_my_id,
2168 .set_my_id = _set_my_id,
2169 .get_other_id = _get_other_id,
2170 .set_other_id = _set_other_id,
2171 .get_other_eap_id = _get_other_eap_id,
2172 .enable_extension = _enable_extension,
2173 .supports_extension = _supports_extension,
2174 .set_condition = _set_condition,
2175 .has_condition = _has_condition,
2176 .set_pending_updates = _set_pending_updates,
2177 .get_pending_updates = _get_pending_updates,
2178 .create_additional_address_enumerator = _create_additional_address_enumerator,
2179 .add_additional_address = _add_additional_address,
2180 .remove_additional_addresses = _remove_additional_addresses,
2181 .has_mapping_changed = _has_mapping_changed,
2182 .retransmit = _retransmit,
2183 .delete = _delete_,
2184 .destroy = _destroy,
2185 .send_dpd = _send_dpd,
2186 .send_keepalive = _send_keepalive,
2187 .get_keymat = _get_keymat,
2188 .add_child_sa = _add_child_sa,
2189 .get_child_sa = _get_child_sa,
2190 .get_child_count = _get_child_count,
2191 .create_child_sa_enumerator = _create_child_sa_enumerator,
2192 .remove_child_sa = _remove_child_sa,
2193 .rekey_child_sa = _rekey_child_sa,
2194 .delete_child_sa = _delete_child_sa,
2195 .destroy_child_sa = _destroy_child_sa,
2196 .rekey = _rekey,
2197 .reauth = _reauth,
2198 .reestablish = _reestablish,
2199 .set_auth_lifetime = _set_auth_lifetime,
2200 .roam = _roam,
2201 .inherit = _inherit,
2202 .generate_message = _generate_message,
2203 .reset = _reset,
2204 .get_unique_id = _get_unique_id,
2205 .set_virtual_ip = _set_virtual_ip,
2206 .get_virtual_ip = _get_virtual_ip,
2207 .add_configuration_attribute = _add_configuration_attribute,
2208 .set_kmaddress = _set_kmaddress,
2209 .create_task_enumerator = _create_task_enumerator,
2210 #ifdef ME
2211 .act_as_mediation_server = _act_as_mediation_server,
2212 .get_server_reflexive_host = _get_server_reflexive_host,
2213 .set_server_reflexive_host = _set_server_reflexive_host,
2214 .get_connect_id = _get_connect_id,
2215 .initiate_mediation = _initiate_mediation,
2216 .initiate_mediated = _initiate_mediated,
2217 .relay = _relay,
2218 .callback = _callback,
2219 .respond = _respond,
2220 #endif /* ME */
2221 },
2222 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2223 .version = version,
2224 .child_sas = linked_list_create(),
2225 .my_host = host_create_any(AF_INET),
2226 .other_host = host_create_any(AF_INET),
2227 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2228 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2229 .keymat = keymat_create(version, ike_sa_id->is_initiator(ike_sa_id)),
2230 .state = IKE_CREATED,
2231 .stats[STAT_INBOUND] = time_monotonic(NULL),
2232 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2233 .my_auth = auth_cfg_create(),
2234 .other_auth = auth_cfg_create(),
2235 .my_auths = linked_list_create(),
2236 .other_auths = linked_list_create(),
2237 .unique_id = ++unique_id,
2238 .additional_addresses = linked_list_create(),
2239 .attributes = linked_list_create(),
2240 .keepalive_interval = lib->settings->get_time(lib->settings,
2241 "charon.keep_alive", KEEPALIVE_INTERVAL),
2242 );
2243
2244 this->task_manager = task_manager_create(&this->public);
2245 this->my_host->set_port(this->my_host, IKEV2_UDP_PORT);
2246
2247 return &this->public;
2248 }