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