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