009277ddd20288105392ae4e287e804d2c79775b
[strongswan.git] / src / libcharon / sa / ike_sa.c
1 /*
2 * Copyright (C) 2006-2016 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2009 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * HSR 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 /*
20 * Copyright (c) 2014 Volker RĂ¼melin
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44 #include <time.h>
45
46 #include "ike_sa.h"
47
48 #include <library.h>
49 #include <daemon.h>
50 #include <collections/array.h>
51 #include <utils/lexparser.h>
52 #include <processing/jobs/retransmit_job.h>
53 #include <processing/jobs/delete_ike_sa_job.h>
54 #include <processing/jobs/send_dpd_job.h>
55 #include <processing/jobs/send_keepalive_job.h>
56 #include <processing/jobs/rekey_ike_sa_job.h>
57 #include <processing/jobs/retry_initiate_job.h>
58 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
59 #include <sa/ikev2/tasks/ike_reauth_complete.h>
60 #include <sa/ikev2/tasks/ike_redirect.h>
61 #include <credentials/sets/auth_cfg_wrapper.h>
62
63 #ifdef ME
64 #include <sa/ikev2/tasks/ike_me.h>
65 #include <processing/jobs/initiate_mediation_job.h>
66 #endif
67
68 ENUM(ike_sa_state_names, IKE_CREATED, IKE_DESTROYING,
69 "CREATED",
70 "CONNECTING",
71 "ESTABLISHED",
72 "PASSIVE",
73 "REKEYING",
74 "REKEYED",
75 "DELETING",
76 "DESTROYING",
77 );
78
79 typedef struct private_ike_sa_t private_ike_sa_t;
80 typedef struct attribute_entry_t attribute_entry_t;
81
82 /**
83 * Private data of an ike_sa_t object.
84 */
85 struct private_ike_sa_t {
86
87 /**
88 * Public members
89 */
90 ike_sa_t public;
91
92 /**
93 * Identifier for the current IKE_SA.
94 */
95 ike_sa_id_t *ike_sa_id;
96
97 /**
98 * IKE version of this SA.
99 */
100 ike_version_t version;
101
102 /**
103 * unique numerical ID for this IKE_SA.
104 */
105 uint32_t unique_id;
106
107 /**
108 * Current state of the IKE_SA
109 */
110 ike_sa_state_t state;
111
112 /**
113 * IKE configuration used to set up this IKE_SA
114 */
115 ike_cfg_t *ike_cfg;
116
117 /**
118 * Peer and authentication information to establish IKE_SA.
119 */
120 peer_cfg_t *peer_cfg;
121
122 /**
123 * currently used authentication ruleset, local
124 */
125 auth_cfg_t *my_auth;
126
127 /**
128 * currently used authentication constraints, remote
129 */
130 auth_cfg_t *other_auth;
131
132 /**
133 * Array of completed local authentication rounds (as auth_cfg_t)
134 */
135 array_t *my_auths;
136
137 /**
138 * Array of completed remote authentication rounds (as auth_cfg_t)
139 */
140 array_t *other_auths;
141
142 /**
143 * Selected IKE proposal
144 */
145 proposal_t *proposal;
146
147 /**
148 * Juggles tasks to process messages
149 */
150 task_manager_t *task_manager;
151
152 /**
153 * Address of local host
154 */
155 host_t *my_host;
156
157 /**
158 * Address of remote host
159 */
160 host_t *other_host;
161
162 #ifdef ME
163 /**
164 * Are we mediation server
165 */
166 bool is_mediation_server;
167
168 /**
169 * Server reflexive host
170 */
171 host_t *server_reflexive_host;
172
173 /**
174 * Connect ID
175 */
176 chunk_t connect_id;
177 #endif /* ME */
178
179 /**
180 * Identification used for us
181 */
182 identification_t *my_id;
183
184 /**
185 * Identification used for other
186 */
187 identification_t *other_id;
188
189 /**
190 * set of extensions the peer supports
191 */
192 ike_extension_t extensions;
193
194 /**
195 * set of condition flags currently enabled for this IKE_SA
196 */
197 ike_condition_t conditions;
198
199 /**
200 * Array containing the child sa's of the current IKE_SA.
201 */
202 array_t *child_sas;
203
204 /**
205 * keymat of this IKE_SA
206 */
207 keymat_t *keymat;
208
209 /**
210 * Virtual IPs on local host
211 */
212 array_t *my_vips;
213
214 /**
215 * Virtual IPs on remote host
216 */
217 array_t *other_vips;
218
219 /**
220 * List of configuration attributes (attribute_entry_t)
221 */
222 array_t *attributes;
223
224 /**
225 * list of peer's addresses, additional ones transmitted via MOBIKE
226 */
227 array_t *peer_addresses;
228
229 /**
230 * previously value of received DESTINATION_IP hash
231 */
232 chunk_t nat_detection_dest;
233
234 /**
235 * number pending UPDATE_SA_ADDRESS (MOBIKE)
236 */
237 uint32_t pending_updates;
238
239 /**
240 * NAT keep alive interval
241 */
242 uint32_t keepalive_interval;
243
244 /**
245 * The schedueld keep alive job, if any
246 */
247 send_keepalive_job_t *keepalive_job;
248
249 /**
250 * interval for retries during initiation (e.g. if DNS resolution failed),
251 * 0 to disable (default)
252 */
253 uint32_t retry_initiate_interval;
254
255 /**
256 * TRUE if a retry_initiate_job has been queued
257 */
258 bool retry_initiate_queued;
259
260 /**
261 * Timestamps for this IKE_SA
262 */
263 uint32_t stats[STAT_MAX];
264
265 /**
266 * how many times we have retried so far (keyingtries)
267 */
268 uint32_t keyingtry;
269
270 /**
271 * local host address to be used for IKE, set via MIGRATE kernel message
272 */
273 host_t *local_host;
274
275 /**
276 * remote host address to be used for IKE, set via MIGRATE kernel message
277 */
278 host_t *remote_host;
279
280 /**
281 * Flush auth configs once established?
282 */
283 bool flush_auth_cfg;
284
285 /**
286 * Maximum length of a single fragment, 0 for address-specific defaults
287 */
288 size_t fragment_size;
289
290 /**
291 * Whether to follow IKEv2 redirects
292 */
293 bool follow_redirects;
294
295 /**
296 * Original gateway address from which we got redirected
297 */
298 host_t *redirected_from;
299
300 /**
301 * Timestamps of redirect attempts to handle loops
302 */
303 array_t *redirected_at;
304 };
305
306 /**
307 * Entry to maintain install configuration attributes during IKE_SA lifetime
308 */
309 struct attribute_entry_t {
310 /** handler used to install this attribute */
311 attribute_handler_t *handler;
312 /** attribute type */
313 configuration_attribute_type_t type;
314 /** attribute data */
315 chunk_t data;
316 };
317
318 /**
319 * get the time of the latest traffic processed by the kernel
320 */
321 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
322 {
323 enumerator_t *enumerator;
324 child_sa_t *child_sa;
325 time_t use_time, current;
326
327 if (inbound)
328 {
329 use_time = this->stats[STAT_INBOUND];
330 }
331 else
332 {
333 use_time = this->stats[STAT_OUTBOUND];
334 }
335
336 enumerator = array_create_enumerator(this->child_sas);
337 while (enumerator->enumerate(enumerator, &child_sa))
338 {
339 child_sa->get_usestats(child_sa, inbound, &current, NULL, NULL);
340 use_time = max(use_time, current);
341 }
342 enumerator->destroy(enumerator);
343
344 return use_time;
345 }
346
347 METHOD(ike_sa_t, get_unique_id, uint32_t,
348 private_ike_sa_t *this)
349 {
350 return this->unique_id;
351 }
352
353 METHOD(ike_sa_t, get_name, char*,
354 private_ike_sa_t *this)
355 {
356 if (this->peer_cfg)
357 {
358 return this->peer_cfg->get_name(this->peer_cfg);
359 }
360 return "(unnamed)";
361 }
362
363 METHOD(ike_sa_t, get_statistic, uint32_t,
364 private_ike_sa_t *this, statistic_t kind)
365 {
366 if (kind < STAT_MAX)
367 {
368 return this->stats[kind];
369 }
370 return 0;
371 }
372
373 METHOD(ike_sa_t, set_statistic, void,
374 private_ike_sa_t *this, statistic_t kind, uint32_t value)
375 {
376 if (kind < STAT_MAX)
377 {
378 this->stats[kind] = value;
379 }
380 }
381
382 METHOD(ike_sa_t, get_my_host, host_t*,
383 private_ike_sa_t *this)
384 {
385 return this->my_host;
386 }
387
388 METHOD(ike_sa_t, set_my_host, void,
389 private_ike_sa_t *this, host_t *me)
390 {
391 DESTROY_IF(this->my_host);
392 this->my_host = me;
393 }
394
395 METHOD(ike_sa_t, get_other_host, host_t*,
396 private_ike_sa_t *this)
397 {
398 return this->other_host;
399 }
400
401 METHOD(ike_sa_t, set_other_host, void,
402 private_ike_sa_t *this, host_t *other)
403 {
404 DESTROY_IF(this->other_host);
405 this->other_host = other;
406 }
407
408 METHOD(ike_sa_t, get_redirected_from, host_t*,
409 private_ike_sa_t *this)
410 {
411 return this->redirected_from;
412 }
413
414 METHOD(ike_sa_t, get_peer_cfg, peer_cfg_t*,
415 private_ike_sa_t *this)
416 {
417 return this->peer_cfg;
418 }
419
420 METHOD(ike_sa_t, set_peer_cfg, void,
421 private_ike_sa_t *this, peer_cfg_t *peer_cfg)
422 {
423 peer_cfg->get_ref(peer_cfg);
424 DESTROY_IF(this->peer_cfg);
425 this->peer_cfg = peer_cfg;
426
427 if (this->ike_cfg == NULL)
428 {
429 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
430 this->ike_cfg->get_ref(this->ike_cfg);
431 }
432 }
433
434 METHOD(ike_sa_t, get_auth_cfg, auth_cfg_t*,
435 private_ike_sa_t *this, bool local)
436 {
437 if (local)
438 {
439 return this->my_auth;
440 }
441 return this->other_auth;
442 }
443
444 METHOD(ike_sa_t, add_auth_cfg, void,
445 private_ike_sa_t *this, bool local, auth_cfg_t *cfg)
446 {
447 if (local)
448 {
449 array_insert(this->my_auths, ARRAY_TAIL, cfg);
450 }
451 else
452 {
453 array_insert(this->other_auths, ARRAY_TAIL, cfg);
454 }
455 }
456
457 METHOD(ike_sa_t, create_auth_cfg_enumerator, enumerator_t*,
458 private_ike_sa_t *this, bool local)
459 {
460 if (local)
461 {
462 return array_create_enumerator(this->my_auths);
463 }
464 return array_create_enumerator(this->other_auths);
465 }
466
467 /**
468 * Flush the stored authentication round information
469 */
470 static void flush_auth_cfgs(private_ike_sa_t *this)
471 {
472 auth_cfg_t *cfg;
473
474 this->my_auth->purge(this->my_auth, FALSE);
475 this->other_auth->purge(this->other_auth, FALSE);
476
477 while (array_remove(this->my_auths, ARRAY_TAIL, &cfg))
478 {
479 cfg->destroy(cfg);
480 }
481 while (array_remove(this->other_auths, ARRAY_TAIL, &cfg))
482 {
483 cfg->destroy(cfg);
484 }
485 }
486
487 METHOD(ike_sa_t, verify_peer_certificate, bool,
488 private_ike_sa_t *this)
489 {
490 enumerator_t *e1, *e2, *certs;
491 auth_cfg_t *cfg, *cfg_done;
492 certificate_t *peer, *cert;
493 public_key_t *key;
494 auth_cfg_t *auth;
495 auth_cfg_wrapper_t *wrapper;
496 time_t not_before, not_after;
497 bool valid = TRUE, found;
498
499 if (this->state != IKE_ESTABLISHED)
500 {
501 DBG1(DBG_IKE, "unable to verify peer certificate in state %N",
502 ike_sa_state_names, this->state);
503 return FALSE;
504 }
505
506 if (!this->flush_auth_cfg &&
507 lib->settings->get_bool(lib->settings,
508 "%s.flush_auth_cfg", FALSE, lib->ns))
509 { /* we can do this check only once if auth configs are flushed */
510 DBG1(DBG_IKE, "unable to verify peer certificate as authentication "
511 "information has been flushed");
512 return FALSE;
513 }
514 this->public.set_condition(&this->public, COND_ONLINE_VALIDATION_SUSPENDED,
515 FALSE);
516
517 e1 = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, FALSE);
518 e2 = array_create_enumerator(this->other_auths);
519 while (e1->enumerate(e1, &cfg))
520 {
521 if (!e2->enumerate(e2, &cfg_done))
522 { /* this should not happen as the authentication should never have
523 * succeeded */
524 valid = FALSE;
525 break;
526 }
527 if ((uintptr_t)cfg_done->get(cfg_done,
528 AUTH_RULE_AUTH_CLASS) != AUTH_CLASS_PUBKEY)
529 {
530 continue;
531 }
532 peer = cfg_done->get(cfg_done, AUTH_RULE_SUBJECT_CERT);
533 if (!peer)
534 {
535 DBG1(DBG_IKE, "no subject certificate found, skipping certificate "
536 "verification");
537 continue;
538 }
539 if (!peer->get_validity(peer, NULL, &not_before, &not_after))
540 {
541 DBG1(DBG_IKE, "peer certificate invalid (valid from %T to %T)",
542 &not_before, FALSE, &not_after, FALSE);
543 valid = FALSE;
544 break;
545 }
546 key = peer->get_public_key(peer);
547 if (!key)
548 {
549 DBG1(DBG_IKE, "unable to retrieve public key, skipping certificate "
550 "verification");
551 continue;
552 }
553 DBG1(DBG_IKE, "verifying peer certificate");
554 /* serve received certificates */
555 wrapper = auth_cfg_wrapper_create(cfg_done);
556 lib->credmgr->add_local_set(lib->credmgr, &wrapper->set, FALSE);
557 certs = lib->credmgr->create_trusted_enumerator(lib->credmgr,
558 key->get_type(key), peer->get_subject(peer), TRUE);
559 key->destroy(key);
560
561 found = FALSE;
562 while (certs->enumerate(certs, &cert, &auth))
563 {
564 if (peer->equals(peer, cert))
565 {
566 cfg_done->add(cfg_done, AUTH_RULE_CERT_VALIDATION_SUSPENDED,
567 FALSE);
568 cfg_done->merge(cfg_done, auth, FALSE);
569 valid = cfg_done->complies(cfg_done, cfg, TRUE);
570 found = TRUE;
571 break;
572 }
573 }
574 certs->destroy(certs);
575 lib->credmgr->remove_local_set(lib->credmgr, &wrapper->set);
576 wrapper->destroy(wrapper);
577 if (!found || !valid)
578 {
579 valid = FALSE;
580 break;
581 }
582 }
583 e1->destroy(e1);
584 e2->destroy(e2);
585
586 if (this->flush_auth_cfg)
587 {
588 this->flush_auth_cfg = FALSE;
589 flush_auth_cfgs(this);
590 }
591 return valid;
592 }
593
594 METHOD(ike_sa_t, get_proposal, proposal_t*,
595 private_ike_sa_t *this)
596 {
597 return this->proposal;
598 }
599
600 METHOD(ike_sa_t, set_proposal, void,
601 private_ike_sa_t *this, proposal_t *proposal)
602 {
603 DESTROY_IF(this->proposal);
604 this->proposal = proposal->clone(proposal);
605 }
606
607 METHOD(ike_sa_t, set_message_id, void,
608 private_ike_sa_t *this, bool initiate, uint32_t mid)
609 {
610 if (initiate)
611 {
612 this->task_manager->reset(this->task_manager, mid, UINT_MAX);
613 }
614 else
615 {
616 this->task_manager->reset(this->task_manager, UINT_MAX, mid);
617 }
618 }
619
620 METHOD(ike_sa_t, send_keepalive, void,
621 private_ike_sa_t *this, bool scheduled)
622 {
623 time_t last_out, now, diff;
624
625 if (scheduled)
626 {
627 this->keepalive_job = NULL;
628 }
629 if (!this->keepalive_interval || this->state == IKE_PASSIVE)
630 { /* keepalives disabled either by configuration or for passive IKE_SAs */
631 return;
632 }
633 if (!(this->conditions & COND_NAT_HERE) || (this->conditions & COND_STALE))
634 { /* disable keepalives if we are not NATed anymore, or the SA is stale */
635 return;
636 }
637
638 last_out = get_use_time(this, FALSE);
639 now = time_monotonic(NULL);
640
641 diff = now - last_out;
642
643 if (diff >= this->keepalive_interval)
644 {
645 packet_t *packet;
646 chunk_t data;
647
648 packet = packet_create();
649 packet->set_source(packet, this->my_host->clone(this->my_host));
650 packet->set_destination(packet, this->other_host->clone(this->other_host));
651 data.ptr = malloc(1);
652 data.ptr[0] = 0xFF;
653 data.len = 1;
654 packet->set_data(packet, data);
655 DBG1(DBG_IKE, "sending keep alive to %#H", this->other_host);
656 charon->sender->send_no_marker(charon->sender, packet);
657 diff = 0;
658 }
659 if (!this->keepalive_job)
660 {
661 this->keepalive_job = send_keepalive_job_create(this->ike_sa_id);
662 lib->scheduler->schedule_job(lib->scheduler, (job_t*)this->keepalive_job,
663 this->keepalive_interval - diff);
664 }
665 }
666
667 METHOD(ike_sa_t, get_ike_cfg, ike_cfg_t*,
668 private_ike_sa_t *this)
669 {
670 return this->ike_cfg;
671 }
672
673 METHOD(ike_sa_t, set_ike_cfg, void,
674 private_ike_sa_t *this, ike_cfg_t *ike_cfg)
675 {
676 ike_cfg->get_ref(ike_cfg);
677 this->ike_cfg = ike_cfg;
678 }
679
680 METHOD(ike_sa_t, enable_extension, void,
681 private_ike_sa_t *this, ike_extension_t extension)
682 {
683 this->extensions |= extension;
684 }
685
686 METHOD(ike_sa_t, supports_extension, bool,
687 private_ike_sa_t *this, ike_extension_t extension)
688 {
689 return (this->extensions & extension) != FALSE;
690 }
691
692 METHOD(ike_sa_t, has_condition, bool,
693 private_ike_sa_t *this, ike_condition_t condition)
694 {
695 return (this->conditions & condition) != FALSE;
696 }
697
698 METHOD(ike_sa_t, set_condition, void,
699 private_ike_sa_t *this, ike_condition_t condition, bool enable)
700 {
701 if (has_condition(this, condition) != enable)
702 {
703 if (enable)
704 {
705 this->conditions |= condition;
706 switch (condition)
707 {
708 case COND_NAT_HERE:
709 DBG1(DBG_IKE, "local host is behind NAT, sending keep alives");
710 this->conditions |= COND_NAT_ANY;
711 send_keepalive(this, FALSE);
712 break;
713 case COND_NAT_THERE:
714 DBG1(DBG_IKE, "remote host is behind NAT");
715 this->conditions |= COND_NAT_ANY;
716 break;
717 case COND_NAT_FAKE:
718 DBG1(DBG_IKE, "faking NAT situation to enforce UDP encapsulation");
719 this->conditions |= COND_NAT_ANY;
720 break;
721 default:
722 break;
723 }
724 }
725 else
726 {
727 this->conditions &= ~condition;
728 switch (condition)
729 {
730 case COND_NAT_HERE:
731 case COND_NAT_FAKE:
732 case COND_NAT_THERE:
733 set_condition(this, COND_NAT_ANY,
734 has_condition(this, COND_NAT_HERE) ||
735 has_condition(this, COND_NAT_THERE) ||
736 has_condition(this, COND_NAT_FAKE));
737 break;
738 case COND_STALE:
739 send_keepalive(this, FALSE);
740 break;
741 default:
742 break;
743 }
744 }
745 }
746 }
747
748 METHOD(ike_sa_t, send_dpd, status_t,
749 private_ike_sa_t *this)
750 {
751 job_t *job;
752 time_t diff, delay;
753 bool task_queued = FALSE;
754
755 if (this->state == IKE_PASSIVE)
756 {
757 return INVALID_STATE;
758 }
759 delay = this->peer_cfg->get_dpd(this->peer_cfg);
760 if (this->task_manager->busy(this->task_manager))
761 {
762 /* an exchange is in the air, no need to start a DPD check */
763 diff = 0;
764 }
765 else
766 {
767 /* check if there was any inbound traffic */
768 time_t last_in, now;
769 last_in = get_use_time(this, TRUE);
770 now = time_monotonic(NULL);
771 diff = now - last_in;
772 if (!delay || diff >= delay)
773 {
774 /* too long ago, initiate dead peer detection */
775 DBG1(DBG_IKE, "sending DPD request");
776 this->task_manager->queue_dpd(this->task_manager);
777 task_queued = TRUE;
778 diff = 0;
779 }
780 }
781 /* recheck in "interval" seconds */
782 if (delay)
783 {
784 job = (job_t*)send_dpd_job_create(this->ike_sa_id);
785 lib->scheduler->schedule_job(lib->scheduler, job, delay - diff);
786 }
787 if (task_queued)
788 {
789 return this->task_manager->initiate(this->task_manager);
790 }
791 return SUCCESS;
792 }
793
794 METHOD(ike_sa_t, get_state, ike_sa_state_t,
795 private_ike_sa_t *this)
796 {
797 return this->state;
798 }
799
800 METHOD(ike_sa_t, set_state, void,
801 private_ike_sa_t *this, ike_sa_state_t state)
802 {
803 bool trigger_dpd = FALSE, keepalives = FALSE;
804
805 DBG2(DBG_IKE, "IKE_SA %s[%d] state change: %N => %N",
806 get_name(this), this->unique_id,
807 ike_sa_state_names, this->state,
808 ike_sa_state_names, state);
809
810 switch (state)
811 {
812 case IKE_ESTABLISHED:
813 {
814 if (this->state == IKE_CONNECTING ||
815 this->state == IKE_PASSIVE)
816 {
817 job_t *job;
818 uint32_t t;
819
820 /* calculate rekey, reauth and lifetime */
821 this->stats[STAT_ESTABLISHED] = time_monotonic(NULL);
822
823 /* schedule rekeying if we have a time which is smaller than
824 * an already scheduled rekeying */
825 t = this->peer_cfg->get_rekey_time(this->peer_cfg, TRUE);
826 if (t && (this->stats[STAT_REKEY] == 0 ||
827 (this->stats[STAT_REKEY] > t + this->stats[STAT_ESTABLISHED])))
828 {
829 this->stats[STAT_REKEY] = t + this->stats[STAT_ESTABLISHED];
830 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, FALSE);
831 lib->scheduler->schedule_job(lib->scheduler, job, t);
832 DBG1(DBG_IKE, "scheduling rekeying in %ds", t);
833 }
834 t = this->peer_cfg->get_reauth_time(this->peer_cfg, TRUE);
835 if (t && (this->stats[STAT_REAUTH] == 0 ||
836 (this->stats[STAT_REAUTH] > t + this->stats[STAT_ESTABLISHED])))
837 {
838 this->stats[STAT_REAUTH] = t + this->stats[STAT_ESTABLISHED];
839 job = (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE);
840 lib->scheduler->schedule_job(lib->scheduler, job, t);
841 DBG1(DBG_IKE, "scheduling reauthentication in %ds", t);
842 }
843 t = this->peer_cfg->get_over_time(this->peer_cfg);
844 if (this->stats[STAT_REKEY] || this->stats[STAT_REAUTH])
845 {
846 if (this->stats[STAT_REAUTH] == 0)
847 {
848 this->stats[STAT_DELETE] = this->stats[STAT_REKEY];
849 }
850 else if (this->stats[STAT_REKEY] == 0)
851 {
852 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH];
853 }
854 else
855 {
856 this->stats[STAT_DELETE] = min(this->stats[STAT_REKEY],
857 this->stats[STAT_REAUTH]);
858 }
859 this->stats[STAT_DELETE] += t;
860 t = this->stats[STAT_DELETE] - this->stats[STAT_ESTABLISHED];
861 job = (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE);
862 lib->scheduler->schedule_job(lib->scheduler, job, t);
863 DBG1(DBG_IKE, "maximum IKE_SA lifetime %ds", t);
864 }
865 trigger_dpd = this->peer_cfg->get_dpd(this->peer_cfg);
866 if (trigger_dpd)
867 {
868 /* Some peers delay the DELETE after rekeying an IKE_SA.
869 * If this delay is longer than our DPD delay, we would
870 * send a DPD request here. The IKE_SA is not ready to do
871 * so yet, so prevent that. */
872 this->stats[STAT_INBOUND] = this->stats[STAT_ESTABLISHED];
873 }
874 if (this->state == IKE_PASSIVE)
875 {
876 keepalives = TRUE;
877 }
878 DESTROY_IF(this->redirected_from);
879 this->redirected_from = NULL;
880 }
881 break;
882 }
883 default:
884 break;
885 }
886 charon->bus->ike_state_change(charon->bus, &this->public, state);
887 this->state = state;
888
889 if (trigger_dpd)
890 {
891 if (supports_extension(this, EXT_DPD))
892 {
893 send_dpd(this);
894 }
895 else
896 {
897 DBG1(DBG_IKE, "DPD not supported by peer, disabled");
898 }
899 }
900 if (keepalives)
901 {
902 send_keepalive(this, FALSE);
903 }
904 }
905
906 METHOD(ike_sa_t, reset, void,
907 private_ike_sa_t *this)
908 {
909 /* the responder ID is reset, as peer may choose another one */
910 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
911 {
912 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
913 }
914
915 set_state(this, IKE_CREATED);
916
917 flush_auth_cfgs(this);
918
919 this->keymat->destroy(this->keymat);
920 this->keymat = keymat_create(this->version,
921 this->ike_sa_id->is_initiator(this->ike_sa_id));
922
923 this->task_manager->reset(this->task_manager, 0, 0);
924 }
925
926 METHOD(ike_sa_t, get_keymat, keymat_t*,
927 private_ike_sa_t *this)
928 {
929 return this->keymat;
930 }
931
932 METHOD(ike_sa_t, add_virtual_ip, void,
933 private_ike_sa_t *this, bool local, host_t *ip)
934 {
935 if (local)
936 {
937 char *iface;
938
939 if (charon->kernel->get_interface(charon->kernel, this->my_host,
940 &iface))
941 {
942 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
943 if (charon->kernel->add_ip(charon->kernel, ip, -1,
944 iface) == SUCCESS)
945 {
946 array_insert_create(&this->my_vips, ARRAY_TAIL, ip->clone(ip));
947 }
948 else
949 {
950 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
951 }
952 free(iface);
953 }
954 else
955 {
956 DBG1(DBG_IKE, "looking up interface for virtual IP %H failed", ip);
957 }
958 }
959 else
960 {
961 array_insert_create(&this->other_vips, ARRAY_TAIL, ip->clone(ip));
962 }
963 }
964
965
966 METHOD(ike_sa_t, clear_virtual_ips, void,
967 private_ike_sa_t *this, bool local)
968 {
969 array_t *vips;
970 host_t *vip;
971
972 vips = local ? this->my_vips : this->other_vips;
973 if (!local && array_count(vips))
974 {
975 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
976 }
977 while (array_remove(vips, ARRAY_HEAD, &vip))
978 {
979 if (local)
980 {
981 charon->kernel->del_ip(charon->kernel, vip, -1, TRUE);
982 }
983 vip->destroy(vip);
984 }
985 }
986
987 METHOD(ike_sa_t, create_virtual_ip_enumerator, enumerator_t*,
988 private_ike_sa_t *this, bool local)
989 {
990 if (local)
991 {
992 return array_create_enumerator(this->my_vips);
993 }
994 return array_create_enumerator(this->other_vips);
995 }
996
997 METHOD(ike_sa_t, add_peer_address, void,
998 private_ike_sa_t *this, host_t *host)
999 {
1000 array_insert_create(&this->peer_addresses, ARRAY_TAIL, host);
1001 }
1002
1003 METHOD(ike_sa_t, create_peer_address_enumerator, enumerator_t*,
1004 private_ike_sa_t *this)
1005 {
1006 if (this->peer_addresses)
1007 {
1008 return array_create_enumerator(this->peer_addresses);
1009 }
1010 /* in case we don't have MOBIKE */
1011 return enumerator_create_single(this->other_host, NULL);
1012 }
1013
1014 METHOD(ike_sa_t, clear_peer_addresses, void,
1015 private_ike_sa_t *this)
1016 {
1017 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
1018 this->peer_addresses = NULL;
1019 }
1020
1021 METHOD(ike_sa_t, has_mapping_changed, bool,
1022 private_ike_sa_t *this, chunk_t hash)
1023 {
1024 if (this->nat_detection_dest.ptr == NULL)
1025 {
1026 this->nat_detection_dest = chunk_clone(hash);
1027 return FALSE;
1028 }
1029 if (chunk_equals(hash, this->nat_detection_dest))
1030 {
1031 return FALSE;
1032 }
1033 free(this->nat_detection_dest.ptr);
1034 this->nat_detection_dest = chunk_clone(hash);
1035 return TRUE;
1036 }
1037
1038 METHOD(ike_sa_t, set_pending_updates, void,
1039 private_ike_sa_t *this, uint32_t updates)
1040 {
1041 this->pending_updates = updates;
1042 }
1043
1044 METHOD(ike_sa_t, get_pending_updates, uint32_t,
1045 private_ike_sa_t *this)
1046 {
1047 return this->pending_updates;
1048 }
1049
1050 METHOD(ike_sa_t, float_ports, void,
1051 private_ike_sa_t *this)
1052 {
1053 /* do not switch if we have a custom port from MOBIKE/NAT */
1054 if (this->my_host->get_port(this->my_host) ==
1055 charon->socket->get_port(charon->socket, FALSE))
1056 {
1057 this->my_host->set_port(this->my_host,
1058 charon->socket->get_port(charon->socket, TRUE));
1059 }
1060 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
1061 {
1062 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
1063 }
1064 }
1065
1066 METHOD(ike_sa_t, update_hosts, void,
1067 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
1068 {
1069 bool update = FALSE;
1070
1071 if (me == NULL)
1072 {
1073 me = this->my_host;
1074 }
1075 if (other == NULL)
1076 {
1077 other = this->other_host;
1078 }
1079
1080 /* apply hosts on first received message */
1081 if (this->my_host->is_anyaddr(this->my_host) ||
1082 this->other_host->is_anyaddr(this->other_host))
1083 {
1084 set_my_host(this, me->clone(me));
1085 set_other_host(this, other->clone(other));
1086 update = TRUE;
1087 }
1088 else
1089 {
1090 /* update our address in any case */
1091 if (force && !me->equals(me, this->my_host))
1092 {
1093 charon->bus->ike_update(charon->bus, &this->public, TRUE, me);
1094 set_my_host(this, me->clone(me));
1095 update = TRUE;
1096 }
1097
1098 if (!other->equals(other, this->other_host) &&
1099 (force || has_condition(this, COND_NAT_THERE)))
1100 {
1101 /* only update other's address if we are behind a static NAT,
1102 * which we assume is the case if we are not initiator */
1103 if (force ||
1104 (!has_condition(this, COND_NAT_HERE) ||
1105 !has_condition(this, COND_ORIGINAL_INITIATOR)))
1106 {
1107 charon->bus->ike_update(charon->bus, &this->public, FALSE, other);
1108 set_other_host(this, other->clone(other));
1109 update = TRUE;
1110 }
1111 }
1112 }
1113
1114 /* update all associated CHILD_SAs, if required */
1115 if (update)
1116 {
1117 enumerator_t *enumerator;
1118 child_sa_t *child_sa;
1119 linked_list_t *vips;
1120
1121 vips = linked_list_create_from_enumerator(
1122 array_create_enumerator(this->my_vips));
1123
1124 enumerator = array_create_enumerator(this->child_sas);
1125 while (enumerator->enumerate(enumerator, &child_sa))
1126 {
1127 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
1128 charon->child_sa_manager->add(charon->child_sa_manager,
1129 child_sa, &this->public);
1130
1131 if (child_sa->update(child_sa, this->my_host, this->other_host,
1132 vips, has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
1133 {
1134 this->public.rekey_child_sa(&this->public,
1135 child_sa->get_protocol(child_sa),
1136 child_sa->get_spi(child_sa, TRUE));
1137 }
1138
1139 }
1140 enumerator->destroy(enumerator);
1141
1142 vips->destroy(vips);
1143 }
1144 }
1145
1146 /**
1147 * Set configured DSCP value on packet
1148 */
1149 static void set_dscp(private_ike_sa_t *this, packet_t *packet)
1150 {
1151 ike_cfg_t *ike_cfg;
1152
1153 /* prefer IKE config on peer_cfg, as its selection is more accurate
1154 * then the initial IKE config */
1155 if (this->peer_cfg)
1156 {
1157 ike_cfg = this->peer_cfg->get_ike_cfg(this->peer_cfg);
1158 }
1159 else
1160 {
1161 ike_cfg = this->ike_cfg;
1162 }
1163 if (ike_cfg)
1164 {
1165 packet->set_dscp(packet, ike_cfg->get_dscp(ike_cfg));
1166 }
1167 }
1168
1169 METHOD(ike_sa_t, generate_message, status_t,
1170 private_ike_sa_t *this, message_t *message, packet_t **packet)
1171 {
1172 status_t status;
1173
1174 if (message->is_encoded(message))
1175 { /* already encoded in task, but set DSCP value */
1176 *packet = message->get_packet(message);
1177 set_dscp(this, *packet);
1178 return SUCCESS;
1179 }
1180 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1181 message->set_ike_sa_id(message, this->ike_sa_id);
1182 charon->bus->message(charon->bus, message, FALSE, TRUE);
1183 status = message->generate(message, this->keymat, packet);
1184 if (status == SUCCESS)
1185 {
1186 set_dscp(this, *packet);
1187 charon->bus->message(charon->bus, message, FALSE, FALSE);
1188 }
1189 return status;
1190 }
1191
1192 static bool filter_fragments(private_ike_sa_t *this, packet_t **fragment,
1193 packet_t **packet)
1194 {
1195 *packet = (*fragment)->clone(*fragment);
1196 set_dscp(this, *packet);
1197 return TRUE;
1198 }
1199
1200 METHOD(ike_sa_t, generate_message_fragmented, status_t,
1201 private_ike_sa_t *this, message_t *message, enumerator_t **packets)
1202 {
1203 enumerator_t *fragments;
1204 packet_t *packet;
1205 status_t status;
1206 bool use_frags = FALSE;
1207 bool pre_generated = FALSE;
1208
1209 if (this->ike_cfg)
1210 {
1211 switch (this->ike_cfg->fragmentation(this->ike_cfg))
1212 {
1213 case FRAGMENTATION_FORCE:
1214 use_frags = TRUE;
1215 break;
1216 case FRAGMENTATION_YES:
1217 use_frags = supports_extension(this, EXT_IKE_FRAGMENTATION);
1218 if (use_frags && this->version == IKEV1 &&
1219 supports_extension(this, EXT_MS_WINDOWS))
1220 {
1221 /* It seems Windows 7 and 8 peers only accept proprietary
1222 * fragmented messages if they expect certificates. */
1223 use_frags = message->get_payload(message,
1224 PLV1_CERTIFICATE) != NULL;
1225 }
1226 break;
1227 default:
1228 break;
1229 }
1230 }
1231 if (!use_frags)
1232 {
1233 status = generate_message(this, message, &packet);
1234 if (status != SUCCESS)
1235 {
1236 return status;
1237 }
1238 *packets = enumerator_create_single(packet, NULL);
1239 return SUCCESS;
1240 }
1241
1242 pre_generated = message->is_encoded(message);
1243 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1244 message->set_ike_sa_id(message, this->ike_sa_id);
1245 if (!pre_generated)
1246 {
1247 charon->bus->message(charon->bus, message, FALSE, TRUE);
1248 }
1249 status = message->fragment(message, this->keymat, this->fragment_size,
1250 &fragments);
1251 if (status == SUCCESS)
1252 {
1253 if (!pre_generated)
1254 {
1255 charon->bus->message(charon->bus, message, FALSE, FALSE);
1256 }
1257 *packets = enumerator_create_filter(fragments, (void*)filter_fragments,
1258 this, NULL);
1259 }
1260 return status;
1261 }
1262
1263 METHOD(ike_sa_t, set_kmaddress, void,
1264 private_ike_sa_t *this, host_t *local, host_t *remote)
1265 {
1266 DESTROY_IF(this->local_host);
1267 DESTROY_IF(this->remote_host);
1268 this->local_host = local->clone(local);
1269 this->remote_host = remote->clone(remote);
1270 }
1271
1272 #ifdef ME
1273 METHOD(ike_sa_t, act_as_mediation_server, void,
1274 private_ike_sa_t *this)
1275 {
1276 charon->mediation_manager->update_sa_id(charon->mediation_manager,
1277 this->other_id, this->ike_sa_id);
1278 this->is_mediation_server = TRUE;
1279 }
1280
1281 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
1282 private_ike_sa_t *this)
1283 {
1284 return this->server_reflexive_host;
1285 }
1286
1287 METHOD(ike_sa_t, set_server_reflexive_host, void,
1288 private_ike_sa_t *this, host_t *host)
1289 {
1290 DESTROY_IF(this->server_reflexive_host);
1291 this->server_reflexive_host = host;
1292 }
1293
1294 METHOD(ike_sa_t, get_connect_id, chunk_t,
1295 private_ike_sa_t *this)
1296 {
1297 return this->connect_id;
1298 }
1299
1300 METHOD(ike_sa_t, respond, status_t,
1301 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
1302 {
1303 ike_me_t *task = ike_me_create(&this->public, TRUE);
1304 task->respond(task, peer_id, connect_id);
1305 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1306 return this->task_manager->initiate(this->task_manager);
1307 }
1308
1309 METHOD(ike_sa_t, callback, status_t,
1310 private_ike_sa_t *this, identification_t *peer_id)
1311 {
1312 ike_me_t *task = ike_me_create(&this->public, TRUE);
1313 task->callback(task, peer_id);
1314 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1315 return this->task_manager->initiate(this->task_manager);
1316 }
1317
1318 METHOD(ike_sa_t, relay, status_t,
1319 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
1320 chunk_t connect_key, linked_list_t *endpoints, bool response)
1321 {
1322 ike_me_t *task = ike_me_create(&this->public, TRUE);
1323 task->relay(task, requester, connect_id, connect_key, endpoints, response);
1324 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1325 return this->task_manager->initiate(this->task_manager);
1326 }
1327
1328 METHOD(ike_sa_t, initiate_mediation, status_t,
1329 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1330 {
1331 ike_me_t *task = ike_me_create(&this->public, TRUE);
1332 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1333 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1334 return this->task_manager->initiate(this->task_manager);
1335 }
1336
1337 METHOD(ike_sa_t, initiate_mediated, status_t,
1338 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1339 {
1340 set_my_host(this, me->clone(me));
1341 set_other_host(this, other->clone(other));
1342 chunk_free(&this->connect_id);
1343 this->connect_id = chunk_clone(connect_id);
1344 return this->task_manager->initiate(this->task_manager);
1345 }
1346 #endif /* ME */
1347
1348 /**
1349 * Resolve DNS host in configuration
1350 */
1351 static void resolve_hosts(private_ike_sa_t *this)
1352 {
1353 host_t *host;
1354 int family = AF_UNSPEC;
1355
1356 switch (charon->socket->supported_families(charon->socket))
1357 {
1358 case SOCKET_FAMILY_IPV4:
1359 family = AF_INET;
1360 break;
1361 case SOCKET_FAMILY_IPV6:
1362 family = AF_INET6;
1363 break;
1364 case SOCKET_FAMILY_BOTH:
1365 case SOCKET_FAMILY_NONE:
1366 break;
1367 }
1368
1369 /* if an IP address is set locally, use the same family to resolve remote */
1370 if (family == AF_UNSPEC && !this->remote_host)
1371 {
1372 if (this->local_host)
1373 {
1374 family = this->local_host->get_family(this->local_host);
1375 }
1376 else
1377 {
1378 family = ike_cfg_get_family(this->ike_cfg, TRUE);
1379 }
1380 }
1381
1382 if (this->remote_host)
1383 {
1384 host = this->remote_host->clone(this->remote_host);
1385 host->set_port(host, IKEV2_UDP_PORT);
1386 }
1387 else
1388 {
1389 host = this->ike_cfg->resolve_other(this->ike_cfg, family);
1390 }
1391 if (host)
1392 {
1393 if (!host->is_anyaddr(host) ||
1394 this->other_host->is_anyaddr(this->other_host))
1395 { /* don't set to %any if we currently have an address, but the
1396 * address family might have changed */
1397 set_other_host(this, host);
1398 }
1399 else
1400 { /* reuse the original port as some implementations might not like
1401 * initial IKE messages on other ports */
1402 this->other_host->set_port(this->other_host, host->get_port(host));
1403 host->destroy(host);
1404 }
1405 }
1406
1407 if (this->local_host)
1408 {
1409 host = this->local_host->clone(this->local_host);
1410 host->set_port(host, charon->socket->get_port(charon->socket, FALSE));
1411 }
1412 else
1413 {
1414 /* use same address family as for other */
1415 if (!this->other_host->is_anyaddr(this->other_host))
1416 {
1417 family = this->other_host->get_family(this->other_host);
1418 }
1419 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1420
1421 if (host && host->is_anyaddr(host) &&
1422 !this->other_host->is_anyaddr(this->other_host))
1423 {
1424 host->destroy(host);
1425 host = charon->kernel->get_source_addr(charon->kernel,
1426 this->other_host, NULL);
1427 if (host)
1428 {
1429 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1430 }
1431 else
1432 { /* fallback to address family specific %any(6), if configured */
1433 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1434 }
1435 }
1436 }
1437 if (host)
1438 {
1439 set_my_host(this, host);
1440 }
1441 }
1442
1443 METHOD(ike_sa_t, initiate, status_t,
1444 private_ike_sa_t *this, child_cfg_t *child_cfg, uint32_t reqid,
1445 traffic_selector_t *tsi, traffic_selector_t *tsr)
1446 {
1447 bool defer_initiate = FALSE;
1448
1449 if (this->state == IKE_CREATED)
1450 {
1451 if (this->my_host->is_anyaddr(this->my_host) ||
1452 this->other_host->is_anyaddr(this->other_host))
1453 {
1454 resolve_hosts(this);
1455 }
1456
1457 if (this->other_host->is_anyaddr(this->other_host)
1458 #ifdef ME
1459 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1460 #endif /* ME */
1461 )
1462 {
1463 char *addr;
1464
1465 addr = this->ike_cfg->get_other_addr(this->ike_cfg);
1466 if (!this->retry_initiate_interval)
1467 {
1468 DBG1(DBG_IKE, "unable to resolve %s, initiate aborted",
1469 addr);
1470 DESTROY_IF(child_cfg);
1471 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1472 return DESTROY_ME;
1473 }
1474 DBG1(DBG_IKE, "unable to resolve %s, retrying in %ds",
1475 addr, this->retry_initiate_interval);
1476 defer_initiate = TRUE;
1477 }
1478
1479 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1480 this->task_manager->queue_ike(this->task_manager);
1481 }
1482
1483 #ifdef ME
1484 if (this->peer_cfg->is_mediation(this->peer_cfg))
1485 {
1486 if (this->state == IKE_ESTABLISHED)
1487 {
1488 /* mediation connection is already established, retrigger state
1489 * change to notify bus listeners */
1490 DBG1(DBG_IKE, "mediation connection is already up");
1491 set_state(this, IKE_ESTABLISHED);
1492 }
1493 DESTROY_IF(child_cfg);
1494 }
1495 else
1496 #endif /* ME */
1497 if (child_cfg)
1498 {
1499 /* normal IKE_SA with CHILD_SA */
1500 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1501 tsi, tsr);
1502 #ifdef ME
1503 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1504 {
1505 /* mediated connection, initiate mediation process */
1506 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1507 lib->processor->queue_job(lib->processor, job);
1508 return SUCCESS;
1509 }
1510 #endif /* ME */
1511 }
1512
1513 if (defer_initiate)
1514 {
1515 if (!this->retry_initiate_queued)
1516 {
1517 job_t *job = (job_t*)retry_initiate_job_create(this->ike_sa_id);
1518 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
1519 this->retry_initiate_interval);
1520 this->retry_initiate_queued = TRUE;
1521 }
1522 return SUCCESS;
1523 }
1524 this->retry_initiate_queued = FALSE;
1525 return this->task_manager->initiate(this->task_manager);
1526 }
1527
1528 METHOD(ike_sa_t, retry_initiate, status_t,
1529 private_ike_sa_t *this)
1530 {
1531 if (this->retry_initiate_queued)
1532 {
1533 this->retry_initiate_queued = FALSE;
1534 return initiate(this, NULL, 0, NULL, NULL);
1535 }
1536 return SUCCESS;
1537 }
1538
1539 METHOD(ike_sa_t, process_message, status_t,
1540 private_ike_sa_t *this, message_t *message)
1541 {
1542 status_t status;
1543
1544 if (this->state == IKE_PASSIVE)
1545 { /* do not handle messages in passive state */
1546 return FAILED;
1547 }
1548 if (message->get_major_version(message) != this->version)
1549 {
1550 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1551 exchange_type_names, message->get_exchange_type(message),
1552 message->get_major_version(message),
1553 ike_version_names, this->version);
1554 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1555 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1556 return FAILED;
1557 }
1558 status = this->task_manager->process_message(this->task_manager, message);
1559 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1560 {
1561 /* authentication completed but if the online validation is suspended we
1562 * need the auth cfgs until we did the delayed verification, we flush
1563 * them afterwards */
1564 if (!has_condition(this, COND_ONLINE_VALIDATION_SUSPENDED))
1565 {
1566 this->flush_auth_cfg = FALSE;
1567 flush_auth_cfgs(this);
1568 }
1569 }
1570 return status;
1571 }
1572
1573 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1574 private_ike_sa_t *this)
1575 {
1576 return this->ike_sa_id;
1577 }
1578
1579 METHOD(ike_sa_t, get_version, ike_version_t,
1580 private_ike_sa_t *this)
1581 {
1582 return this->version;
1583 }
1584
1585 METHOD(ike_sa_t, get_my_id, identification_t*,
1586 private_ike_sa_t *this)
1587 {
1588 return this->my_id;
1589 }
1590
1591 METHOD(ike_sa_t, set_my_id, void,
1592 private_ike_sa_t *this, identification_t *me)
1593 {
1594 DESTROY_IF(this->my_id);
1595 this->my_id = me;
1596 }
1597
1598 METHOD(ike_sa_t, get_other_id, identification_t*,
1599 private_ike_sa_t *this)
1600 {
1601 return this->other_id;
1602 }
1603
1604 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1605 private_ike_sa_t *this)
1606 {
1607 identification_t *id = NULL, *current;
1608 enumerator_t *enumerator;
1609 auth_cfg_t *cfg;
1610
1611 enumerator = array_create_enumerator(this->other_auths);
1612 while (enumerator->enumerate(enumerator, &cfg))
1613 {
1614 /* prefer EAP-Identity of last round */
1615 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1616 if (!current || current->get_type(current) == ID_ANY)
1617 {
1618 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1619 }
1620 if (!current || current->get_type(current) == ID_ANY)
1621 {
1622 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1623 }
1624 if (current && current->get_type(current) != ID_ANY)
1625 {
1626 id = current;
1627 continue;
1628 }
1629 }
1630 enumerator->destroy(enumerator);
1631 if (id)
1632 {
1633 return id;
1634 }
1635 return this->other_id;
1636 }
1637
1638 METHOD(ike_sa_t, set_other_id, void,
1639 private_ike_sa_t *this, identification_t *other)
1640 {
1641 DESTROY_IF(this->other_id);
1642 this->other_id = other;
1643 }
1644
1645 METHOD(ike_sa_t, add_child_sa, void,
1646 private_ike_sa_t *this, child_sa_t *child_sa)
1647 {
1648 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
1649 charon->child_sa_manager->add(charon->child_sa_manager,
1650 child_sa, &this->public);
1651 }
1652
1653 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1654 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi, bool inbound)
1655 {
1656 enumerator_t *enumerator;
1657 child_sa_t *current, *found = NULL;
1658
1659 enumerator = array_create_enumerator(this->child_sas);
1660 while (enumerator->enumerate(enumerator, (void**)&current))
1661 {
1662 if (current->get_spi(current, inbound) == spi &&
1663 current->get_protocol(current) == protocol)
1664 {
1665 found = current;
1666 }
1667 }
1668 enumerator->destroy(enumerator);
1669 return found;
1670 }
1671
1672 METHOD(ike_sa_t, get_child_count, int,
1673 private_ike_sa_t *this)
1674 {
1675 return array_count(this->child_sas);
1676 }
1677
1678 /**
1679 * Private data of a create_child_sa_enumerator()
1680 */
1681 typedef struct {
1682 /** implements enumerator */
1683 enumerator_t public;
1684 /** inner array enumerator */
1685 enumerator_t *inner;
1686 /** current item */
1687 child_sa_t *current;
1688 } child_enumerator_t;
1689
1690 METHOD(enumerator_t, child_enumerate, bool,
1691 child_enumerator_t *this, child_sa_t **child_sa)
1692 {
1693 if (this->inner->enumerate(this->inner, &this->current))
1694 {
1695 *child_sa = this->current;
1696 return TRUE;
1697 }
1698 return FALSE;
1699 }
1700
1701 METHOD(enumerator_t, child_enumerator_destroy, void,
1702 child_enumerator_t *this)
1703 {
1704 this->inner->destroy(this->inner);
1705 free(this);
1706 }
1707
1708 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1709 private_ike_sa_t *this)
1710 {
1711 child_enumerator_t *enumerator;
1712
1713 INIT(enumerator,
1714 .public = {
1715 .enumerate = (void*)_child_enumerate,
1716 .destroy = _child_enumerator_destroy,
1717 },
1718 .inner = array_create_enumerator(this->child_sas),
1719 );
1720 return &enumerator->public;
1721 }
1722
1723 METHOD(ike_sa_t, remove_child_sa, void,
1724 private_ike_sa_t *this, enumerator_t *enumerator)
1725 {
1726 child_enumerator_t *ce = (child_enumerator_t*)enumerator;
1727
1728 charon->child_sa_manager->remove(charon->child_sa_manager, ce->current);
1729 array_remove_at(this->child_sas, ce->inner);
1730 }
1731
1732 METHOD(ike_sa_t, rekey_child_sa, status_t,
1733 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi)
1734 {
1735 if (this->state == IKE_PASSIVE)
1736 {
1737 return INVALID_STATE;
1738 }
1739 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1740 return this->task_manager->initiate(this->task_manager);
1741 }
1742
1743 METHOD(ike_sa_t, delete_child_sa, status_t,
1744 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi, bool expired)
1745 {
1746 if (this->state == IKE_PASSIVE)
1747 {
1748 return INVALID_STATE;
1749 }
1750 this->task_manager->queue_child_delete(this->task_manager,
1751 protocol, spi, expired);
1752 return this->task_manager->initiate(this->task_manager);
1753 }
1754
1755 METHOD(ike_sa_t, destroy_child_sa, status_t,
1756 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi)
1757 {
1758 enumerator_t *enumerator;
1759 child_sa_t *child_sa;
1760 status_t status = NOT_FOUND;
1761
1762 enumerator = create_child_sa_enumerator(this);
1763 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1764 {
1765 if (child_sa->get_protocol(child_sa) == protocol &&
1766 child_sa->get_spi(child_sa, TRUE) == spi)
1767 {
1768 remove_child_sa(this, enumerator);
1769 child_sa->destroy(child_sa);
1770 status = SUCCESS;
1771 break;
1772 }
1773 }
1774 enumerator->destroy(enumerator);
1775 return status;
1776 }
1777
1778 METHOD(ike_sa_t, delete_, status_t,
1779 private_ike_sa_t *this)
1780 {
1781 switch (this->state)
1782 {
1783 case IKE_REKEYING:
1784 if (this->version == IKEV1)
1785 { /* SA has been reauthenticated, delete */
1786 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1787 break;
1788 }
1789 /* FALL */
1790 case IKE_ESTABLISHED:
1791 if (time_monotonic(NULL) >= this->stats[STAT_DELETE])
1792 { /* IKE_SA hard lifetime hit */
1793 charon->bus->alert(charon->bus, ALERT_IKE_SA_EXPIRED);
1794 }
1795 this->task_manager->queue_ike_delete(this->task_manager);
1796 return this->task_manager->initiate(this->task_manager);
1797 case IKE_CREATED:
1798 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1799 break;
1800 case IKE_PASSIVE:
1801 break;
1802 default:
1803 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1804 "without notification", ike_sa_state_names, this->state);
1805 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1806 break;
1807 }
1808 return DESTROY_ME;
1809 }
1810
1811 METHOD(ike_sa_t, rekey, status_t,
1812 private_ike_sa_t *this)
1813 {
1814 if (this->state == IKE_PASSIVE)
1815 {
1816 return INVALID_STATE;
1817 }
1818 this->task_manager->queue_ike_rekey(this->task_manager);
1819 return this->task_manager->initiate(this->task_manager);
1820 }
1821
1822 METHOD(ike_sa_t, reauth, status_t,
1823 private_ike_sa_t *this)
1824 {
1825 if (this->state == IKE_PASSIVE)
1826 {
1827 return INVALID_STATE;
1828 }
1829 if (this->state == IKE_CONNECTING)
1830 {
1831 DBG0(DBG_IKE, "reinitiating IKE_SA %s[%d]",
1832 get_name(this), this->unique_id);
1833 reset(this);
1834 this->task_manager->queue_ike(this->task_manager);
1835 return this->task_manager->initiate(this->task_manager);
1836 }
1837 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1838 * If the peer does not support RFC4478, there is no way to keep the
1839 * IKE_SA up. */
1840 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1841 {
1842 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1843 if (array_count(this->other_vips) != 0 ||
1844 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1845 has_condition(this, COND_EAP_AUTHENTICATED)
1846 #ifdef ME
1847 /* as mediation server we too cannot reauth the IKE_SA */
1848 || this->is_mediation_server
1849 #endif /* ME */
1850 )
1851 {
1852 time_t del, now;
1853
1854 del = this->stats[STAT_DELETE];
1855 now = time_monotonic(NULL);
1856 DBG1(DBG_IKE, "IKE_SA %s[%d] will timeout in %V",
1857 get_name(this), this->unique_id, &now, &del);
1858 return FAILED;
1859 }
1860 else
1861 {
1862 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d] actively",
1863 get_name(this), this->unique_id);
1864 }
1865 }
1866 else
1867 {
1868 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d]",
1869 get_name(this), this->unique_id);
1870 }
1871 set_condition(this, COND_REAUTHENTICATING, TRUE);
1872 this->task_manager->queue_ike_reauth(this->task_manager);
1873 return this->task_manager->initiate(this->task_manager);
1874 }
1875
1876 /**
1877 * Check if tasks to create CHILD_SAs are queued in the given queue
1878 */
1879 static bool is_child_queued(private_ike_sa_t *this, task_queue_t queue)
1880 {
1881 enumerator_t *enumerator;
1882 task_t *task;
1883 bool found = FALSE;
1884
1885 enumerator = this->task_manager->create_task_enumerator(this->task_manager,
1886 queue);
1887 while (enumerator->enumerate(enumerator, &task))
1888 {
1889 if (task->get_type(task) == TASK_CHILD_CREATE ||
1890 task->get_type(task) == TASK_QUICK_MODE)
1891 {
1892 found = TRUE;
1893 break;
1894 }
1895 }
1896 enumerator->destroy(enumerator);
1897 return found;
1898 }
1899
1900 /**
1901 * Reestablish CHILD_SAs and migrate queued tasks.
1902 *
1903 * If force is true all SAs are restarted, otherwise their close/dpd_action
1904 * is followed.
1905 */
1906 static status_t reestablish_children(private_ike_sa_t *this, ike_sa_t *new,
1907 bool force)
1908 {
1909 enumerator_t *enumerator;
1910 child_sa_t *child_sa;
1911 child_cfg_t *child_cfg;
1912 action_t action;
1913 status_t status = FAILED;
1914
1915 /* handle existing CHILD_SAs */
1916 enumerator = create_child_sa_enumerator(this);
1917 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1918 {
1919 if (force)
1920 {
1921 switch (child_sa->get_state(child_sa))
1922 {
1923 case CHILD_ROUTED:
1924 { /* move routed child directly */
1925 remove_child_sa(this, enumerator);
1926 new->add_child_sa(new, child_sa);
1927 action = ACTION_NONE;
1928 break;
1929 }
1930 default:
1931 { /* initiate/queue all other CHILD_SAs */
1932 action = ACTION_RESTART;
1933 break;
1934 }
1935 }
1936 }
1937 else
1938 { /* only restart CHILD_SAs that are configured accordingly */
1939 if (this->state == IKE_DELETING)
1940 {
1941 action = child_sa->get_close_action(child_sa);
1942 }
1943 else
1944 {
1945 action = child_sa->get_dpd_action(child_sa);
1946 }
1947 }
1948 switch (action)
1949 {
1950 case ACTION_RESTART:
1951 child_cfg = child_sa->get_config(child_sa);
1952 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1953 child_cfg->get_name(child_cfg));
1954 child_cfg->get_ref(child_cfg);
1955 status = new->initiate(new, child_cfg,
1956 child_sa->get_reqid(child_sa), NULL, NULL);
1957 break;
1958 default:
1959 continue;
1960 }
1961 if (status == DESTROY_ME)
1962 {
1963 break;
1964 }
1965 }
1966 enumerator->destroy(enumerator);
1967 /* adopt any active or queued CHILD-creating tasks */
1968 if (status != DESTROY_ME)
1969 {
1970 task_manager_t *other_tasks = ((private_ike_sa_t*)new)->task_manager;
1971 other_tasks->adopt_child_tasks(other_tasks, this->task_manager);
1972 if (new->get_state(new) == IKE_CREATED)
1973 {
1974 status = new->initiate(new, NULL, 0, NULL, NULL);
1975 }
1976 }
1977 return status;
1978 }
1979
1980 METHOD(ike_sa_t, reestablish, status_t,
1981 private_ike_sa_t *this)
1982 {
1983 ike_sa_t *new;
1984 host_t *host;
1985 action_t action;
1986 enumerator_t *enumerator;
1987 child_sa_t *child_sa;
1988 bool restart = FALSE;
1989 status_t status = FAILED;
1990
1991 if (has_condition(this, COND_REAUTHENTICATING))
1992 { /* only reauthenticate if we have children */
1993 if (array_count(this->child_sas) == 0
1994 #ifdef ME
1995 /* allow reauth of mediation connections without CHILD_SAs */
1996 && !this->peer_cfg->is_mediation(this->peer_cfg)
1997 #endif /* ME */
1998 )
1999 {
2000 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA "
2001 "to recreate");
2002 }
2003 else
2004 {
2005 restart = TRUE;
2006 }
2007 }
2008 else
2009 { /* check if we have children to keep up at all */
2010 enumerator = array_create_enumerator(this->child_sas);
2011 while (enumerator->enumerate(enumerator, (void**)&child_sa))
2012 {
2013 if (this->state == IKE_DELETING)
2014 {
2015 action = child_sa->get_close_action(child_sa);
2016 }
2017 else
2018 {
2019 action = child_sa->get_dpd_action(child_sa);
2020 }
2021 switch (action)
2022 {
2023 case ACTION_RESTART:
2024 restart = TRUE;
2025 break;
2026 case ACTION_ROUTE:
2027 charon->traps->install(charon->traps, this->peer_cfg,
2028 child_sa->get_config(child_sa),
2029 child_sa->get_reqid(child_sa));
2030 break;
2031 default:
2032 break;
2033 }
2034 }
2035 enumerator->destroy(enumerator);
2036 /* check if we have tasks that recreate children */
2037 if (!restart)
2038 {
2039 restart = is_child_queued(this, TASK_QUEUE_ACTIVE) ||
2040 is_child_queued(this, TASK_QUEUE_QUEUED);
2041 }
2042 #ifdef ME
2043 /* mediation connections have no children, keep them up anyway */
2044 if (this->peer_cfg->is_mediation(this->peer_cfg))
2045 {
2046 restart = TRUE;
2047 }
2048 #endif /* ME */
2049 }
2050 if (!restart)
2051 {
2052 return FAILED;
2053 }
2054
2055 /* check if we are able to reestablish this IKE_SA */
2056 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
2057 (array_count(this->other_vips) != 0 ||
2058 has_condition(this, COND_EAP_AUTHENTICATED)
2059 #ifdef ME
2060 || this->is_mediation_server
2061 #endif /* ME */
2062 ))
2063 {
2064 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
2065 return FAILED;
2066 }
2067
2068 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
2069 this->version, TRUE);
2070 if (!new)
2071 {
2072 return FAILED;
2073 }
2074 new->set_peer_cfg(new, this->peer_cfg);
2075 host = this->other_host;
2076 new->set_other_host(new, host->clone(host));
2077 host = this->my_host;
2078 new->set_my_host(new, host->clone(host));
2079 charon->bus->ike_reestablish_pre(charon->bus, &this->public, new);
2080 if (!has_condition(this, COND_REAUTHENTICATING))
2081 { /* reauthenticate to the same addresses, but resolve hosts if
2082 * reestablishing (old addresses serve as fallback) */
2083 resolve_hosts((private_ike_sa_t*)new);
2084 }
2085 /* if we already have a virtual IP, we reuse it */
2086 enumerator = array_create_enumerator(this->my_vips);
2087 while (enumerator->enumerate(enumerator, &host))
2088 {
2089 new->add_virtual_ip(new, TRUE, host);
2090 }
2091 enumerator->destroy(enumerator);
2092
2093 #ifdef ME
2094 if (this->peer_cfg->is_mediation(this->peer_cfg))
2095 {
2096 status = new->initiate(new, NULL, 0, NULL, NULL);
2097 }
2098 else
2099 #endif /* ME */
2100 {
2101 status = reestablish_children(this, new,
2102 has_condition(this, COND_REAUTHENTICATING));
2103 }
2104
2105 if (status == DESTROY_ME)
2106 {
2107 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
2108 FALSE);
2109 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
2110 status = FAILED;
2111 }
2112 else
2113 {
2114 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
2115 TRUE);
2116 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
2117 status = SUCCESS;
2118 }
2119 charon->bus->set_sa(charon->bus, &this->public);
2120 return status;
2121 }
2122
2123 /**
2124 * Resolve the given gateway ID
2125 */
2126 static host_t *resolve_gateway_id(identification_t *gateway)
2127 {
2128 char gw[BUF_LEN];
2129 host_t *addr;
2130
2131 snprintf(gw, sizeof(gw), "%Y", gateway);
2132 gw[sizeof(gw)-1] = '\0';
2133 addr = host_create_from_dns(gw, AF_UNSPEC, IKEV2_UDP_PORT);
2134 if (!addr)
2135 {
2136 DBG1(DBG_IKE, "unable to resolve gateway ID '%Y', redirect failed",
2137 gateway);
2138 }
2139 return addr;
2140 }
2141
2142 /**
2143 * Redirect the current SA to the given target host
2144 */
2145 static bool redirect_established(private_ike_sa_t *this, identification_t *to)
2146 {
2147 private_ike_sa_t *new_priv;
2148 ike_sa_t *new;
2149 host_t *other;
2150 time_t redirect;
2151
2152 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
2153 this->version, TRUE);
2154 if (!new)
2155 {
2156 return FALSE;
2157 }
2158 new_priv = (private_ike_sa_t*)new;
2159 new->set_peer_cfg(new, this->peer_cfg);
2160 new_priv->redirected_from = this->other_host->clone(this->other_host);
2161 charon->bus->ike_reestablish_pre(charon->bus, &this->public, new);
2162 other = resolve_gateway_id(to);
2163 if (other)
2164 {
2165 set_my_host(new_priv, this->my_host->clone(this->my_host));
2166 /* this allows us to force the remote address while we still properly
2167 * resolve the local address */
2168 new_priv->remote_host = other;
2169 resolve_hosts(new_priv);
2170 new_priv->redirected_at = array_create(sizeof(time_t), MAX_REDIRECTS);
2171 while (array_remove(this->redirected_at, ARRAY_HEAD, &redirect))
2172 {
2173 array_insert(new_priv->redirected_at, ARRAY_TAIL, &redirect);
2174 }
2175 if (reestablish_children(this, new, TRUE) != DESTROY_ME)
2176 {
2177 #ifdef USE_IKEV2
2178 new->queue_task(new, (task_t*)ike_reauth_complete_create(new,
2179 this->ike_sa_id));
2180 #endif
2181 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
2182 TRUE);
2183 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
2184 charon->bus->set_sa(charon->bus, &this->public);
2185 return TRUE;
2186 }
2187 }
2188 charon->bus->ike_reestablish_post(charon->bus, &this->public, new,
2189 FALSE);
2190 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
2191 charon->bus->set_sa(charon->bus, &this->public);
2192 return FALSE;
2193 }
2194
2195 /**
2196 * Redirect the current connecting SA to the given target host
2197 */
2198 static bool redirect_connecting(private_ike_sa_t *this, identification_t *to)
2199 {
2200 host_t *other;
2201
2202 other = resolve_gateway_id(to);
2203 if (!other)
2204 {
2205 return FALSE;
2206 }
2207 reset(this);
2208 DESTROY_IF(this->redirected_from);
2209 this->redirected_from = this->other_host->clone(this->other_host);
2210 DESTROY_IF(this->remote_host);
2211 /* this allows us to force the remote address while we still properly
2212 * resolve the local address */
2213 this->remote_host = other;
2214 resolve_hosts(this);
2215 return TRUE;
2216 }
2217
2218 /**
2219 * Check if the current redirect exceeds the limits for redirects
2220 */
2221 static bool redirect_count_exceeded(private_ike_sa_t *this)
2222 {
2223 time_t now, redirect;
2224
2225 now = time_monotonic(NULL);
2226 /* remove entries outside the defined period */
2227 while (array_get(this->redirected_at, ARRAY_HEAD, &redirect) &&
2228 now - redirect >= REDIRECT_LOOP_DETECT_PERIOD)
2229 {
2230 array_remove(this->redirected_at, ARRAY_HEAD, NULL);
2231 }
2232 if (array_count(this->redirected_at) < MAX_REDIRECTS)
2233 {
2234 if (!this->redirected_at)
2235 {
2236 this->redirected_at = array_create(sizeof(time_t), MAX_REDIRECTS);
2237 }
2238 array_insert(this->redirected_at, ARRAY_TAIL, &now);
2239 return FALSE;
2240 }
2241 return TRUE;
2242 }
2243
2244 METHOD(ike_sa_t, handle_redirect, bool,
2245 private_ike_sa_t *this, identification_t *gateway)
2246 {
2247 DBG1(DBG_IKE, "redirected to %Y", gateway);
2248 if (!this->follow_redirects)
2249 {
2250 DBG1(DBG_IKE, "server sent REDIRECT even though we disabled it");
2251 return FALSE;
2252 }
2253 if (redirect_count_exceeded(this))
2254 {
2255 DBG1(DBG_IKE, "only %d redirects are allowed within %d seconds",
2256 MAX_REDIRECTS, REDIRECT_LOOP_DETECT_PERIOD);
2257 return FALSE;
2258 }
2259
2260 switch (this->state)
2261 {
2262 case IKE_CONNECTING:
2263 return redirect_connecting(this, gateway);
2264 case IKE_ESTABLISHED:
2265 return redirect_established(this, gateway);
2266 default:
2267 DBG1(DBG_IKE, "unable to handle redirect for IKE_SA in state %N",
2268 ike_sa_state_names, this->state);
2269 return FALSE;
2270 }
2271 }
2272
2273 METHOD(ike_sa_t, redirect, status_t,
2274 private_ike_sa_t *this, identification_t *gateway)
2275 {
2276 switch (this->state)
2277 {
2278 case IKE_CONNECTING:
2279 case IKE_ESTABLISHED:
2280 case IKE_REKEYING:
2281 if (has_condition(this, COND_REDIRECTED))
2282 { /* IKE_SA already got redirected */
2283 return SUCCESS;
2284 }
2285 if (has_condition(this, COND_ORIGINAL_INITIATOR))
2286 {
2287 DBG1(DBG_IKE, "unable to redirect IKE_SA as initiator");
2288 return FAILED;
2289 }
2290 if (this->version == IKEV1)
2291 {
2292 DBG1(DBG_IKE, "unable to redirect IKEv1 SA");
2293 return FAILED;
2294 }
2295 if (!supports_extension(this, EXT_IKE_REDIRECTION))
2296 {
2297 DBG1(DBG_IKE, "client does not support IKE redirection");
2298 return FAILED;
2299 }
2300 #ifdef USE_IKEV2
2301 this->task_manager->queue_task(this->task_manager,
2302 (task_t*)ike_redirect_create(&this->public, gateway));
2303 #endif
2304 return this->task_manager->initiate(this->task_manager);
2305 default:
2306 DBG1(DBG_IKE, "unable to redirect IKE_SA in state %N",
2307 ike_sa_state_names, this->state);
2308 return INVALID_STATE;
2309 }
2310 }
2311
2312 METHOD(ike_sa_t, retransmit, status_t,
2313 private_ike_sa_t *this, uint32_t message_id)
2314 {
2315 if (this->state == IKE_PASSIVE)
2316 {
2317 return INVALID_STATE;
2318 }
2319 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
2320 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
2321 {
2322 /* send a proper signal to brief interested bus listeners */
2323 switch (this->state)
2324 {
2325 case IKE_CONNECTING:
2326 {
2327 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
2328 uint32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
2329 charon->bus->alert(charon->bus, ALERT_PEER_INIT_UNREACHABLE,
2330 this->keyingtry);
2331 this->keyingtry++;
2332 if (tries == 0 || tries > this->keyingtry)
2333 {
2334 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
2335 this->keyingtry + 1, tries);
2336 reset(this);
2337 resolve_hosts(this);
2338 this->task_manager->queue_ike(this->task_manager);
2339 return this->task_manager->initiate(this->task_manager);
2340 }
2341 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
2342 break;
2343 }
2344 case IKE_DELETING:
2345 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
2346 if (has_condition(this, COND_REAUTHENTICATING))
2347 {
2348 DBG1(DBG_IKE, "delete during reauthentication failed, "
2349 "trying to reestablish IKE_SA anyway");
2350 reestablish(this);
2351 }
2352 break;
2353 case IKE_REKEYING:
2354 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
2355 /* FALL */
2356 default:
2357 reestablish(this);
2358 break;
2359 }
2360 if (this->state != IKE_CONNECTING &&
2361 this->state != IKE_REKEYED)
2362 {
2363 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
2364 }
2365 return DESTROY_ME;
2366 }
2367 return SUCCESS;
2368 }
2369
2370 METHOD(ike_sa_t, set_auth_lifetime, status_t,
2371 private_ike_sa_t *this, uint32_t lifetime)
2372 {
2373 uint32_t diff, hard, soft, now;
2374 bool send_update;
2375
2376 diff = this->peer_cfg->get_over_time(this->peer_cfg);
2377 now = time_monotonic(NULL);
2378 hard = now + lifetime;
2379 soft = hard - diff;
2380
2381 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
2382 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
2383 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
2384 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
2385 (array_count(this->other_vips) != 0 ||
2386 has_condition(this, COND_EAP_AUTHENTICATED));
2387
2388 if (lifetime < diff)
2389 {
2390 this->stats[STAT_REAUTH] = now;
2391
2392 if (!send_update)
2393 {
2394 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
2395 "starting reauthentication", lifetime);
2396 lib->processor->queue_job(lib->processor,
2397 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
2398 }
2399 }
2400 else if (this->stats[STAT_REAUTH] == 0 ||
2401 this->stats[STAT_REAUTH] > soft)
2402 {
2403 this->stats[STAT_REAUTH] = soft;
2404 if (!send_update)
2405 {
2406 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
2407 "reauthentication in %ds", lifetime, lifetime - diff);
2408 lib->scheduler->schedule_job(lib->scheduler,
2409 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2410 lifetime - diff);
2411 }
2412 }
2413 else
2414 {
2415 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
2416 "reauthentication already scheduled in %ds", lifetime,
2417 this->stats[STAT_REAUTH] - time_monotonic(NULL));
2418 send_update = FALSE;
2419 }
2420 /* give at least some seconds to reauthenticate */
2421 this->stats[STAT_DELETE] = max(hard, now + 10);
2422
2423 #ifdef USE_IKEV2
2424 if (send_update)
2425 {
2426 ike_auth_lifetime_t *task;
2427
2428 task = ike_auth_lifetime_create(&this->public, TRUE);
2429 this->task_manager->queue_task(this->task_manager, &task->task);
2430 return this->task_manager->initiate(this->task_manager);
2431 }
2432 #endif
2433 return SUCCESS;
2434 }
2435
2436 /**
2437 * Check if the current combination of source and destination address is still
2438 * valid.
2439 */
2440 static bool is_current_path_valid(private_ike_sa_t *this)
2441 {
2442 bool valid = FALSE;
2443 host_t *src;
2444 src = charon->kernel->get_source_addr(charon->kernel, this->other_host,
2445 this->my_host);
2446 if (src)
2447 {
2448 if (src->ip_equals(src, this->my_host))
2449 {
2450 valid = TRUE;
2451 }
2452 src->destroy(src);
2453 }
2454 return valid;
2455 }
2456
2457 /**
2458 * Check if we have any path avialable for this IKE SA.
2459 */
2460 static bool is_any_path_valid(private_ike_sa_t *this)
2461 {
2462 bool valid = FALSE;
2463 enumerator_t *enumerator;
2464 host_t *src = NULL, *addr;
2465 int family = AF_UNSPEC;
2466
2467 switch (charon->socket->supported_families(charon->socket))
2468 {
2469 case SOCKET_FAMILY_IPV4:
2470 family = AF_INET;
2471 break;
2472 case SOCKET_FAMILY_IPV6:
2473 family = AF_INET6;
2474 break;
2475 case SOCKET_FAMILY_BOTH:
2476 case SOCKET_FAMILY_NONE:
2477 break;
2478 }
2479
2480 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
2481 enumerator = create_peer_address_enumerator(this);
2482 while (enumerator->enumerate(enumerator, &addr))
2483 {
2484 if (family != AF_UNSPEC && addr->get_family(addr) != family)
2485 {
2486 continue;
2487 }
2488 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
2489 src = charon->kernel->get_source_addr(charon->kernel, addr, NULL);
2490 if (src)
2491 {
2492 break;
2493 }
2494 }
2495 enumerator->destroy(enumerator);
2496 if (src)
2497 {
2498 valid = TRUE;
2499 src->destroy(src);
2500 }
2501 return valid;
2502 }
2503
2504 METHOD(ike_sa_t, roam, status_t,
2505 private_ike_sa_t *this, bool address)
2506 {
2507 switch (this->state)
2508 {
2509 case IKE_CREATED:
2510 case IKE_DELETING:
2511 case IKE_DESTROYING:
2512 case IKE_PASSIVE:
2513 case IKE_REKEYED:
2514 return SUCCESS;
2515 default:
2516 break;
2517 }
2518
2519 /* keep existing path if possible */
2520 if (is_current_path_valid(this))
2521 {
2522 DBG2(DBG_IKE, "keeping connection path %H - %H",
2523 this->my_host, this->other_host);
2524 set_condition(this, COND_STALE, FALSE);
2525
2526 if (supports_extension(this, EXT_MOBIKE) && address)
2527 { /* if any addresses changed, send an updated list */
2528 DBG1(DBG_IKE, "sending address list update using MOBIKE");
2529 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
2530 return this->task_manager->initiate(this->task_manager);
2531 }
2532 return SUCCESS;
2533 }
2534
2535 if (!is_any_path_valid(this))
2536 {
2537 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
2538 this->other_host);
2539 set_condition(this, COND_STALE, TRUE);
2540 return SUCCESS;
2541 }
2542 set_condition(this, COND_STALE, FALSE);
2543
2544 /* update addresses with mobike, if supported ... */
2545 if (supports_extension(this, EXT_MOBIKE))
2546 {
2547 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2548 { /* responder updates the peer about changed address config */
2549 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
2550 "implicitly requesting an address change");
2551 address = TRUE;
2552 }
2553 else
2554 {
2555 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2556 }
2557 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
2558 return this->task_manager->initiate(this->task_manager);
2559 }
2560
2561 /* ... reauth if not */
2562 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2563 { /* responder does not reauthenticate */
2564 set_condition(this, COND_STALE, TRUE);
2565 return SUCCESS;
2566 }
2567 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
2568 /* since our previous path is not valid anymore, try and find a new one */
2569 resolve_hosts(this);
2570 return reauth(this);
2571 }
2572
2573 METHOD(ike_sa_t, add_configuration_attribute, void,
2574 private_ike_sa_t *this, attribute_handler_t *handler,
2575 configuration_attribute_type_t type, chunk_t data)
2576 {
2577 attribute_entry_t entry = {
2578 .handler = handler,
2579 .type = type,
2580 .data = chunk_clone(data),
2581 };
2582 array_insert(this->attributes, ARRAY_TAIL, &entry);
2583 }
2584
2585 /**
2586 * Enumerator filter for attributes
2587 */
2588 static bool filter_attribute(void *null, attribute_entry_t **in,
2589 configuration_attribute_type_t *type, void *in2,
2590 chunk_t *data, void *in3, bool *handled)
2591 {
2592 *type = (*in)->type;
2593 *data = (*in)->data;
2594 *handled = (*in)->handler != NULL;
2595 return TRUE;
2596 }
2597
2598 METHOD(ike_sa_t, create_attribute_enumerator, enumerator_t*,
2599 private_ike_sa_t *this)
2600 {
2601 return enumerator_create_filter(array_create_enumerator(this->attributes),
2602 (void*)filter_attribute, NULL, NULL);
2603 }
2604
2605 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
2606 private_ike_sa_t *this, task_queue_t queue)
2607 {
2608 return this->task_manager->create_task_enumerator(this->task_manager, queue);
2609 }
2610
2611 METHOD(ike_sa_t, flush_queue, void,
2612 private_ike_sa_t *this, task_queue_t queue)
2613 {
2614 this->task_manager->flush_queue(this->task_manager, queue);
2615 }
2616
2617 METHOD(ike_sa_t, queue_task, void,
2618 private_ike_sa_t *this, task_t *task)
2619 {
2620 this->task_manager->queue_task(this->task_manager, task);
2621 }
2622
2623 METHOD(ike_sa_t, queue_task_delayed, void,
2624 private_ike_sa_t *this, task_t *task, uint32_t delay)
2625 {
2626 this->task_manager->queue_task_delayed(this->task_manager, task, delay);
2627 }
2628
2629 METHOD(ike_sa_t, inherit_pre, void,
2630 private_ike_sa_t *this, ike_sa_t *other_public)
2631 {
2632 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2633
2634 /* apply config and hosts */
2635 set_peer_cfg(this, other->peer_cfg);
2636 set_my_host(this, other->my_host->clone(other->my_host));
2637 set_other_host(this, other->other_host->clone(other->other_host));
2638
2639 /* apply extensions and conditions with a few exceptions */
2640 this->extensions = other->extensions;
2641 this->conditions = other->conditions;
2642 this->conditions &= ~COND_STALE;
2643 this->conditions &= ~COND_REAUTHENTICATING;
2644 }
2645
2646 METHOD(ike_sa_t, inherit_post, void,
2647 private_ike_sa_t *this, ike_sa_t *other_public)
2648 {
2649 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2650 child_sa_t *child_sa;
2651 enumerator_t *enumerator;
2652 attribute_entry_t entry;
2653 auth_cfg_t *cfg;
2654 host_t *vip;
2655
2656 /* apply hosts and ids */
2657 this->my_host->destroy(this->my_host);
2658 this->other_host->destroy(this->other_host);
2659 this->my_id->destroy(this->my_id);
2660 this->other_id->destroy(this->other_id);
2661 this->my_host = other->my_host->clone(other->my_host);
2662 this->other_host = other->other_host->clone(other->other_host);
2663 this->my_id = other->my_id->clone(other->my_id);
2664 this->other_id = other->other_id->clone(other->other_id);
2665
2666 /* apply assigned virtual IPs... */
2667 while (array_remove(other->my_vips, ARRAY_HEAD, &vip))
2668 {
2669 array_insert_create(&this->my_vips, ARRAY_TAIL, vip);
2670 }
2671 while (array_remove(other->other_vips, ARRAY_HEAD, &vip))
2672 {
2673 array_insert_create(&this->other_vips, ARRAY_TAIL, vip);
2674 }
2675
2676 /* MOBIKE additional addresses */
2677 while (array_remove(other->peer_addresses, ARRAY_HEAD, &vip))
2678 {
2679 array_insert_create(&this->peer_addresses, ARRAY_TAIL, vip);
2680 }
2681
2682 /* authentication information */
2683 enumerator = array_create_enumerator(other->my_auths);
2684 while (enumerator->enumerate(enumerator, &cfg))
2685 {
2686 array_insert(this->my_auths, ARRAY_TAIL, cfg->clone(cfg));
2687 }
2688 enumerator->destroy(enumerator);
2689 enumerator = array_create_enumerator(other->other_auths);
2690 while (enumerator->enumerate(enumerator, &cfg))
2691 {
2692 array_insert(this->other_auths, ARRAY_TAIL, cfg->clone(cfg));
2693 }
2694 enumerator->destroy(enumerator);
2695
2696 /* ... and configuration attributes */
2697 while (array_remove(other->attributes, ARRAY_HEAD, &entry))
2698 {
2699 array_insert(this->attributes, ARRAY_TAIL, &entry);
2700 }
2701
2702 /* inherit all conditions */
2703 this->conditions = other->conditions;
2704 if (this->conditions & COND_NAT_HERE)
2705 {
2706 send_keepalive(this, FALSE);
2707 }
2708
2709 #ifdef ME
2710 if (other->is_mediation_server)
2711 {
2712 act_as_mediation_server(this);
2713 }
2714 else if (other->server_reflexive_host)
2715 {
2716 this->server_reflexive_host = other->server_reflexive_host->clone(
2717 other->server_reflexive_host);
2718 }
2719 #endif /* ME */
2720
2721 /* adopt all children */
2722 while (array_remove(other->child_sas, ARRAY_HEAD, &child_sa))
2723 {
2724 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
2725 add_child_sa(this, child_sa);
2726 }
2727
2728 /* move pending tasks to the new IKE_SA */
2729 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2730
2731 /* reauthentication timeout survives a rekeying */
2732 if (other->stats[STAT_REAUTH])
2733 {
2734 time_t reauth, delete, now = time_monotonic(NULL);
2735
2736 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2737 reauth = this->stats[STAT_REAUTH] - now;
2738 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2739 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2740 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2741 "lifetime reduced to %ds", reauth, delete);
2742 lib->scheduler->schedule_job(lib->scheduler,
2743 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2744 lib->scheduler->schedule_job(lib->scheduler,
2745 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2746 }
2747 }
2748
2749 METHOD(ike_sa_t, destroy, void,
2750 private_ike_sa_t *this)
2751 {
2752 attribute_entry_t entry;
2753 child_sa_t *child_sa;
2754 host_t *vip;
2755
2756 charon->bus->set_sa(charon->bus, &this->public);
2757
2758 set_state(this, IKE_DESTROYING);
2759 if (this->task_manager)
2760 {
2761 this->task_manager->flush(this->task_manager);
2762 }
2763
2764 /* remove attributes first, as we pass the IKE_SA to the handler */
2765 charon->bus->handle_vips(charon->bus, &this->public, FALSE);
2766 while (array_remove(this->attributes, ARRAY_TAIL, &entry))
2767 {
2768 if (entry.handler)
2769 {
2770 charon->attributes->release(charon->attributes, entry.handler,
2771 &this->public, entry.type, entry.data);
2772 }
2773 free(entry.data.ptr);
2774 }
2775 /* uninstall CHILD_SAs before virtual IPs, otherwise we might kill
2776 * routes that the CHILD_SA tries to uninstall. */
2777 while (array_remove(this->child_sas, ARRAY_TAIL, &child_sa))
2778 {
2779 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
2780 child_sa->destroy(child_sa);
2781 }
2782 while (array_remove(this->my_vips, ARRAY_TAIL, &vip))
2783 {
2784 charon->kernel->del_ip(charon->kernel, vip, -1, TRUE);
2785 vip->destroy(vip);
2786 }
2787 if (array_count(this->other_vips))
2788 {
2789 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
2790 }
2791 while (array_remove(this->other_vips, ARRAY_TAIL, &vip))
2792 {
2793 if (this->peer_cfg)
2794 {
2795 linked_list_t *pools;
2796
2797 pools = linked_list_create_from_enumerator(
2798 this->peer_cfg->create_pool_enumerator(this->peer_cfg));
2799 charon->attributes->release_address(charon->attributes,
2800 pools, vip, &this->public);
2801 pools->destroy(pools);
2802 }
2803 vip->destroy(vip);
2804 }
2805
2806 /* unset SA after here to avoid usage by the listeners */
2807 charon->bus->set_sa(charon->bus, NULL);
2808
2809 array_destroy(this->child_sas);
2810 DESTROY_IF(this->task_manager);
2811 DESTROY_IF(this->keymat);
2812 array_destroy(this->attributes);
2813 array_destroy(this->my_vips);
2814 array_destroy(this->other_vips);
2815 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
2816 #ifdef ME
2817 if (this->is_mediation_server)
2818 {
2819 charon->mediation_manager->remove(charon->mediation_manager,
2820 this->ike_sa_id);
2821 }
2822 DESTROY_IF(this->server_reflexive_host);
2823 chunk_free(&this->connect_id);
2824 #endif /* ME */
2825 free(this->nat_detection_dest.ptr);
2826
2827 DESTROY_IF(this->my_host);
2828 DESTROY_IF(this->other_host);
2829 DESTROY_IF(this->my_id);
2830 DESTROY_IF(this->other_id);
2831 DESTROY_IF(this->local_host);
2832 DESTROY_IF(this->remote_host);
2833 DESTROY_IF(this->redirected_from);
2834 array_destroy(this->redirected_at);
2835
2836 DESTROY_IF(this->ike_cfg);
2837 DESTROY_IF(this->peer_cfg);
2838 DESTROY_IF(this->proposal);
2839 this->my_auth->destroy(this->my_auth);
2840 this->other_auth->destroy(this->other_auth);
2841 array_destroy_offset(this->my_auths, offsetof(auth_cfg_t, destroy));
2842 array_destroy_offset(this->other_auths, offsetof(auth_cfg_t, destroy));
2843
2844 this->ike_sa_id->destroy(this->ike_sa_id);
2845 free(this);
2846 }
2847
2848 /*
2849 * Described in header.
2850 */
2851 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
2852 ike_version_t version)
2853 {
2854 private_ike_sa_t *this;
2855 static refcount_t unique_id = 0;
2856
2857 if (version == IKE_ANY)
2858 { /* prefer IKEv2 if protocol not specified */
2859 #ifdef USE_IKEV2
2860 version = IKEV2;
2861 #else
2862 version = IKEV1;
2863 #endif
2864 }
2865
2866 INIT(this,
2867 .public = {
2868 .get_version = _get_version,
2869 .get_state = _get_state,
2870 .set_state = _set_state,
2871 .get_name = _get_name,
2872 .get_statistic = _get_statistic,
2873 .set_statistic = _set_statistic,
2874 .process_message = _process_message,
2875 .initiate = _initiate,
2876 .retry_initiate = _retry_initiate,
2877 .get_ike_cfg = _get_ike_cfg,
2878 .set_ike_cfg = _set_ike_cfg,
2879 .get_peer_cfg = _get_peer_cfg,
2880 .set_peer_cfg = _set_peer_cfg,
2881 .get_auth_cfg = _get_auth_cfg,
2882 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2883 .verify_peer_certificate = _verify_peer_certificate,
2884 .add_auth_cfg = _add_auth_cfg,
2885 .get_proposal = _get_proposal,
2886 .set_proposal = _set_proposal,
2887 .get_id = _get_id,
2888 .get_my_host = _get_my_host,
2889 .set_my_host = _set_my_host,
2890 .get_other_host = _get_other_host,
2891 .set_other_host = _set_other_host,
2892 .set_message_id = _set_message_id,
2893 .float_ports = _float_ports,
2894 .update_hosts = _update_hosts,
2895 .get_my_id = _get_my_id,
2896 .set_my_id = _set_my_id,
2897 .get_other_id = _get_other_id,
2898 .set_other_id = _set_other_id,
2899 .get_other_eap_id = _get_other_eap_id,
2900 .enable_extension = _enable_extension,
2901 .supports_extension = _supports_extension,
2902 .set_condition = _set_condition,
2903 .has_condition = _has_condition,
2904 .set_pending_updates = _set_pending_updates,
2905 .get_pending_updates = _get_pending_updates,
2906 .create_peer_address_enumerator = _create_peer_address_enumerator,
2907 .add_peer_address = _add_peer_address,
2908 .clear_peer_addresses = _clear_peer_addresses,
2909 .has_mapping_changed = _has_mapping_changed,
2910 .retransmit = _retransmit,
2911 .delete = _delete_,
2912 .destroy = _destroy,
2913 .send_dpd = _send_dpd,
2914 .send_keepalive = _send_keepalive,
2915 .redirect = _redirect,
2916 .handle_redirect = _handle_redirect,
2917 .get_redirected_from = _get_redirected_from,
2918 .get_keymat = _get_keymat,
2919 .add_child_sa = _add_child_sa,
2920 .get_child_sa = _get_child_sa,
2921 .get_child_count = _get_child_count,
2922 .create_child_sa_enumerator = _create_child_sa_enumerator,
2923 .remove_child_sa = _remove_child_sa,
2924 .rekey_child_sa = _rekey_child_sa,
2925 .delete_child_sa = _delete_child_sa,
2926 .destroy_child_sa = _destroy_child_sa,
2927 .rekey = _rekey,
2928 .reauth = _reauth,
2929 .reestablish = _reestablish,
2930 .set_auth_lifetime = _set_auth_lifetime,
2931 .roam = _roam,
2932 .inherit_pre = _inherit_pre,
2933 .inherit_post = _inherit_post,
2934 .generate_message = _generate_message,
2935 .generate_message_fragmented = _generate_message_fragmented,
2936 .reset = _reset,
2937 .get_unique_id = _get_unique_id,
2938 .add_virtual_ip = _add_virtual_ip,
2939 .clear_virtual_ips = _clear_virtual_ips,
2940 .create_virtual_ip_enumerator = _create_virtual_ip_enumerator,
2941 .add_configuration_attribute = _add_configuration_attribute,
2942 .create_attribute_enumerator = _create_attribute_enumerator,
2943 .set_kmaddress = _set_kmaddress,
2944 .create_task_enumerator = _create_task_enumerator,
2945 .flush_queue = _flush_queue,
2946 .queue_task = _queue_task,
2947 .queue_task_delayed = _queue_task_delayed,
2948 #ifdef ME
2949 .act_as_mediation_server = _act_as_mediation_server,
2950 .get_server_reflexive_host = _get_server_reflexive_host,
2951 .set_server_reflexive_host = _set_server_reflexive_host,
2952 .get_connect_id = _get_connect_id,
2953 .initiate_mediation = _initiate_mediation,
2954 .initiate_mediated = _initiate_mediated,
2955 .relay = _relay,
2956 .callback = _callback,
2957 .respond = _respond,
2958 #endif /* ME */
2959 },
2960 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2961 .version = version,
2962 .my_host = host_create_any(AF_INET),
2963 .other_host = host_create_any(AF_INET),
2964 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2965 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2966 .keymat = keymat_create(version, initiator),
2967 .state = IKE_CREATED,
2968 .stats[STAT_INBOUND] = time_monotonic(NULL),
2969 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2970 .my_auth = auth_cfg_create(),
2971 .other_auth = auth_cfg_create(),
2972 .my_auths = array_create(0, 0),
2973 .other_auths = array_create(0, 0),
2974 .attributes = array_create(sizeof(attribute_entry_t), 0),
2975 .unique_id = ref_get(&unique_id),
2976 .keepalive_interval = lib->settings->get_time(lib->settings,
2977 "%s.keep_alive", KEEPALIVE_INTERVAL, lib->ns),
2978 .retry_initiate_interval = lib->settings->get_time(lib->settings,
2979 "%s.retry_initiate_interval", 0, lib->ns),
2980 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2981 "%s.flush_auth_cfg", FALSE, lib->ns),
2982 .fragment_size = lib->settings->get_int(lib->settings,
2983 "%s.fragment_size", 0, lib->ns),
2984 .follow_redirects = lib->settings->get_bool(lib->settings,
2985 "%s.follow_redirects", TRUE, lib->ns),
2986 );
2987
2988 if (version == IKEV2)
2989 { /* always supported with IKEv2 */
2990 enable_extension(this, EXT_DPD);
2991 }
2992
2993 this->task_manager = task_manager_create(&this->public);
2994 this->my_host->set_port(this->my_host,
2995 charon->socket->get_port(charon->socket, FALSE));
2996
2997 if (!this->task_manager || !this->keymat)
2998 {
2999 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
3000 destroy(this);
3001 return NULL;
3002 }
3003 return &this->public;
3004 }