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