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