ikev2: (Re-)Queue tasks used to establish an IKE_SA in reset()
[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 this->task_manager->queue_ike(this->task_manager);
925 }
926
927 METHOD(ike_sa_t, get_keymat, keymat_t*,
928 private_ike_sa_t *this)
929 {
930 return this->keymat;
931 }
932
933 METHOD(ike_sa_t, add_virtual_ip, void,
934 private_ike_sa_t *this, bool local, host_t *ip)
935 {
936 if (local)
937 {
938 char *iface;
939
940 if (charon->kernel->get_interface(charon->kernel, this->my_host,
941 &iface))
942 {
943 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
944 if (charon->kernel->add_ip(charon->kernel, ip, -1,
945 iface) == SUCCESS)
946 {
947 array_insert_create(&this->my_vips, ARRAY_TAIL, ip->clone(ip));
948 }
949 else
950 {
951 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
952 }
953 free(iface);
954 }
955 else
956 {
957 DBG1(DBG_IKE, "looking up interface for virtual IP %H failed", ip);
958 }
959 }
960 else
961 {
962 array_insert_create(&this->other_vips, ARRAY_TAIL, ip->clone(ip));
963 }
964 }
965
966
967 METHOD(ike_sa_t, clear_virtual_ips, void,
968 private_ike_sa_t *this, bool local)
969 {
970 array_t *vips;
971 host_t *vip;
972
973 vips = local ? this->my_vips : this->other_vips;
974 if (!local && array_count(vips))
975 {
976 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
977 }
978 while (array_remove(vips, ARRAY_HEAD, &vip))
979 {
980 if (local)
981 {
982 charon->kernel->del_ip(charon->kernel, vip, -1, TRUE);
983 }
984 vip->destroy(vip);
985 }
986 }
987
988 METHOD(ike_sa_t, create_virtual_ip_enumerator, enumerator_t*,
989 private_ike_sa_t *this, bool local)
990 {
991 if (local)
992 {
993 return array_create_enumerator(this->my_vips);
994 }
995 return array_create_enumerator(this->other_vips);
996 }
997
998 METHOD(ike_sa_t, add_peer_address, void,
999 private_ike_sa_t *this, host_t *host)
1000 {
1001 array_insert_create(&this->peer_addresses, ARRAY_TAIL, host);
1002 }
1003
1004 METHOD(ike_sa_t, create_peer_address_enumerator, enumerator_t*,
1005 private_ike_sa_t *this)
1006 {
1007 if (this->peer_addresses)
1008 {
1009 return array_create_enumerator(this->peer_addresses);
1010 }
1011 /* in case we don't have MOBIKE */
1012 return enumerator_create_single(this->other_host, NULL);
1013 }
1014
1015 METHOD(ike_sa_t, clear_peer_addresses, void,
1016 private_ike_sa_t *this)
1017 {
1018 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
1019 this->peer_addresses = NULL;
1020 }
1021
1022 METHOD(ike_sa_t, has_mapping_changed, bool,
1023 private_ike_sa_t *this, chunk_t hash)
1024 {
1025 if (this->nat_detection_dest.ptr == NULL)
1026 {
1027 this->nat_detection_dest = chunk_clone(hash);
1028 return FALSE;
1029 }
1030 if (chunk_equals(hash, this->nat_detection_dest))
1031 {
1032 return FALSE;
1033 }
1034 free(this->nat_detection_dest.ptr);
1035 this->nat_detection_dest = chunk_clone(hash);
1036 return TRUE;
1037 }
1038
1039 METHOD(ike_sa_t, set_pending_updates, void,
1040 private_ike_sa_t *this, uint32_t updates)
1041 {
1042 this->pending_updates = updates;
1043 }
1044
1045 METHOD(ike_sa_t, get_pending_updates, uint32_t,
1046 private_ike_sa_t *this)
1047 {
1048 return this->pending_updates;
1049 }
1050
1051 METHOD(ike_sa_t, float_ports, void,
1052 private_ike_sa_t *this)
1053 {
1054 /* do not switch if we have a custom port from MOBIKE/NAT */
1055 if (this->my_host->get_port(this->my_host) ==
1056 charon->socket->get_port(charon->socket, FALSE))
1057 {
1058 this->my_host->set_port(this->my_host,
1059 charon->socket->get_port(charon->socket, TRUE));
1060 }
1061 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
1062 {
1063 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
1064 }
1065 }
1066
1067 METHOD(ike_sa_t, update_hosts, void,
1068 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
1069 {
1070 bool update = FALSE;
1071
1072 if (me == NULL)
1073 {
1074 me = this->my_host;
1075 }
1076 if (other == NULL)
1077 {
1078 other = this->other_host;
1079 }
1080
1081 /* apply hosts on first received message */
1082 if (this->my_host->is_anyaddr(this->my_host) ||
1083 this->other_host->is_anyaddr(this->other_host))
1084 {
1085 set_my_host(this, me->clone(me));
1086 set_other_host(this, other->clone(other));
1087 update = TRUE;
1088 }
1089 else
1090 {
1091 /* update our address in any case */
1092 if (force && !me->equals(me, this->my_host))
1093 {
1094 charon->bus->ike_update(charon->bus, &this->public, TRUE, me);
1095 set_my_host(this, me->clone(me));
1096 update = TRUE;
1097 }
1098
1099 if (!other->equals(other, this->other_host) &&
1100 (force || has_condition(this, COND_NAT_THERE)))
1101 {
1102 /* only update other's address if we are behind a static NAT,
1103 * which we assume is the case if we are not initiator */
1104 if (force ||
1105 (!has_condition(this, COND_NAT_HERE) ||
1106 !has_condition(this, COND_ORIGINAL_INITIATOR)))
1107 {
1108 charon->bus->ike_update(charon->bus, &this->public, FALSE, other);
1109 set_other_host(this, other->clone(other));
1110 update = TRUE;
1111 }
1112 }
1113 }
1114
1115 /* update all associated CHILD_SAs, if required */
1116 if (update)
1117 {
1118 enumerator_t *enumerator;
1119 child_sa_t *child_sa;
1120 linked_list_t *vips;
1121
1122 vips = linked_list_create_from_enumerator(
1123 array_create_enumerator(this->my_vips));
1124
1125 enumerator = array_create_enumerator(this->child_sas);
1126 while (enumerator->enumerate(enumerator, &child_sa))
1127 {
1128 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
1129 charon->child_sa_manager->add(charon->child_sa_manager,
1130 child_sa, &this->public);
1131
1132 if (child_sa->update(child_sa, this->my_host, this->other_host,
1133 vips, has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
1134 {
1135 this->public.rekey_child_sa(&this->public,
1136 child_sa->get_protocol(child_sa),
1137 child_sa->get_spi(child_sa, TRUE));
1138 }
1139
1140 }
1141 enumerator->destroy(enumerator);
1142
1143 vips->destroy(vips);
1144 }
1145 }
1146
1147 /**
1148 * Set configured DSCP value on packet
1149 */
1150 static void set_dscp(private_ike_sa_t *this, packet_t *packet)
1151 {
1152 ike_cfg_t *ike_cfg;
1153
1154 /* prefer IKE config on peer_cfg, as its selection is more accurate
1155 * then the initial IKE config */
1156 if (this->peer_cfg)
1157 {
1158 ike_cfg = this->peer_cfg->get_ike_cfg(this->peer_cfg);
1159 }
1160 else
1161 {
1162 ike_cfg = this->ike_cfg;
1163 }
1164 if (ike_cfg)
1165 {
1166 packet->set_dscp(packet, ike_cfg->get_dscp(ike_cfg));
1167 }
1168 }
1169
1170 METHOD(ike_sa_t, generate_message, status_t,
1171 private_ike_sa_t *this, message_t *message, packet_t **packet)
1172 {
1173 status_t status;
1174
1175 if (message->is_encoded(message))
1176 { /* already encoded in task, but set DSCP value */
1177 *packet = message->get_packet(message);
1178 set_dscp(this, *packet);
1179 return SUCCESS;
1180 }
1181 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1182 message->set_ike_sa_id(message, this->ike_sa_id);
1183 charon->bus->message(charon->bus, message, FALSE, TRUE);
1184 status = message->generate(message, this->keymat, packet);
1185 if (status == SUCCESS)
1186 {
1187 set_dscp(this, *packet);
1188 charon->bus->message(charon->bus, message, FALSE, FALSE);
1189 }
1190 return status;
1191 }
1192
1193 static bool filter_fragments(private_ike_sa_t *this, packet_t **fragment,
1194 packet_t **packet)
1195 {
1196 *packet = (*fragment)->clone(*fragment);
1197 set_dscp(this, *packet);
1198 return TRUE;
1199 }
1200
1201 METHOD(ike_sa_t, generate_message_fragmented, status_t,
1202 private_ike_sa_t *this, message_t *message, enumerator_t **packets)
1203 {
1204 enumerator_t *fragments;
1205 packet_t *packet;
1206 status_t status;
1207 bool use_frags = FALSE;
1208 bool pre_generated = FALSE;
1209
1210 if (this->ike_cfg)
1211 {
1212 switch (this->ike_cfg->fragmentation(this->ike_cfg))
1213 {
1214 case FRAGMENTATION_FORCE:
1215 use_frags = TRUE;
1216 break;
1217 case FRAGMENTATION_YES:
1218 use_frags = supports_extension(this, EXT_IKE_FRAGMENTATION);
1219 if (use_frags && this->version == IKEV1 &&
1220 supports_extension(this, EXT_MS_WINDOWS))
1221 {
1222 /* It seems Windows 7 and 8 peers only accept proprietary
1223 * fragmented messages if they expect certificates. */
1224 use_frags = message->get_payload(message,
1225 PLV1_CERTIFICATE) != NULL;
1226 }
1227 break;
1228 default:
1229 break;
1230 }
1231 }
1232 if (!use_frags)
1233 {
1234 status = generate_message(this, message, &packet);
1235 if (status != SUCCESS)
1236 {
1237 return status;
1238 }
1239 *packets = enumerator_create_single(packet, NULL);
1240 return SUCCESS;
1241 }
1242
1243 pre_generated = message->is_encoded(message);
1244 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1245 message->set_ike_sa_id(message, this->ike_sa_id);
1246 if (!pre_generated)
1247 {
1248 charon->bus->message(charon->bus, message, FALSE, TRUE);
1249 }
1250 status = message->fragment(message, this->keymat, this->fragment_size,
1251 &fragments);
1252 if (status == SUCCESS)
1253 {
1254 if (!pre_generated)
1255 {
1256 charon->bus->message(charon->bus, message, FALSE, FALSE);
1257 }
1258 *packets = enumerator_create_filter(fragments, (void*)filter_fragments,
1259 this, NULL);
1260 }
1261 return status;
1262 }
1263
1264 METHOD(ike_sa_t, set_kmaddress, void,
1265 private_ike_sa_t *this, host_t *local, host_t *remote)
1266 {
1267 DESTROY_IF(this->local_host);
1268 DESTROY_IF(this->remote_host);
1269 this->local_host = local->clone(local);
1270 this->remote_host = remote->clone(remote);
1271 }
1272
1273 #ifdef ME
1274 METHOD(ike_sa_t, act_as_mediation_server, void,
1275 private_ike_sa_t *this)
1276 {
1277 charon->mediation_manager->update_sa_id(charon->mediation_manager,
1278 this->other_id, this->ike_sa_id);
1279 this->is_mediation_server = TRUE;
1280 }
1281
1282 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
1283 private_ike_sa_t *this)
1284 {
1285 return this->server_reflexive_host;
1286 }
1287
1288 METHOD(ike_sa_t, set_server_reflexive_host, void,
1289 private_ike_sa_t *this, host_t *host)
1290 {
1291 DESTROY_IF(this->server_reflexive_host);
1292 this->server_reflexive_host = host;
1293 }
1294
1295 METHOD(ike_sa_t, get_connect_id, chunk_t,
1296 private_ike_sa_t *this)
1297 {
1298 return this->connect_id;
1299 }
1300
1301 METHOD(ike_sa_t, respond, status_t,
1302 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
1303 {
1304 ike_me_t *task = ike_me_create(&this->public, TRUE);
1305 task->respond(task, peer_id, connect_id);
1306 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1307 return this->task_manager->initiate(this->task_manager);
1308 }
1309
1310 METHOD(ike_sa_t, callback, status_t,
1311 private_ike_sa_t *this, identification_t *peer_id)
1312 {
1313 ike_me_t *task = ike_me_create(&this->public, TRUE);
1314 task->callback(task, peer_id);
1315 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1316 return this->task_manager->initiate(this->task_manager);
1317 }
1318
1319 METHOD(ike_sa_t, relay, status_t,
1320 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
1321 chunk_t connect_key, linked_list_t *endpoints, bool response)
1322 {
1323 ike_me_t *task = ike_me_create(&this->public, TRUE);
1324 task->relay(task, requester, connect_id, connect_key, endpoints, response);
1325 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1326 return this->task_manager->initiate(this->task_manager);
1327 }
1328
1329 METHOD(ike_sa_t, initiate_mediation, status_t,
1330 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1331 {
1332 ike_me_t *task = ike_me_create(&this->public, TRUE);
1333 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1334 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1335 return this->task_manager->initiate(this->task_manager);
1336 }
1337
1338 METHOD(ike_sa_t, initiate_mediated, status_t,
1339 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1340 {
1341 set_my_host(this, me->clone(me));
1342 set_other_host(this, other->clone(other));
1343 chunk_free(&this->connect_id);
1344 this->connect_id = chunk_clone(connect_id);
1345 return this->task_manager->initiate(this->task_manager);
1346 }
1347 #endif /* ME */
1348
1349 /**
1350 * Resolve DNS host in configuration
1351 */
1352 static void resolve_hosts(private_ike_sa_t *this)
1353 {
1354 host_t *host;
1355 int family = AF_UNSPEC;
1356
1357 switch (charon->socket->supported_families(charon->socket))
1358 {
1359 case SOCKET_FAMILY_IPV4:
1360 family = AF_INET;
1361 break;
1362 case SOCKET_FAMILY_IPV6:
1363 family = AF_INET6;
1364 break;
1365 case SOCKET_FAMILY_BOTH:
1366 case SOCKET_FAMILY_NONE:
1367 break;
1368 }
1369
1370 /* if an IP address is set locally, use the same family to resolve remote */
1371 if (family == AF_UNSPEC && !this->remote_host)
1372 {
1373 if (this->local_host)
1374 {
1375 family = this->local_host->get_family(this->local_host);
1376 }
1377 else
1378 {
1379 family = ike_cfg_get_family(this->ike_cfg, TRUE);
1380 }
1381 }
1382
1383 if (this->remote_host)
1384 {
1385 host = this->remote_host->clone(this->remote_host);
1386 host->set_port(host, IKEV2_UDP_PORT);
1387 }
1388 else
1389 {
1390 host = this->ike_cfg->resolve_other(this->ike_cfg, family);
1391 }
1392 if (host)
1393 {
1394 if (!host->is_anyaddr(host) ||
1395 this->other_host->is_anyaddr(this->other_host))
1396 { /* don't set to %any if we currently have an address, but the
1397 * address family might have changed */
1398 set_other_host(this, host);
1399 }
1400 else
1401 { /* reuse the original port as some implementations might not like
1402 * initial IKE messages on other ports */
1403 this->other_host->set_port(this->other_host, host->get_port(host));
1404 host->destroy(host);
1405 }
1406 }
1407
1408 if (this->local_host)
1409 {
1410 host = this->local_host->clone(this->local_host);
1411 host->set_port(host, charon->socket->get_port(charon->socket, FALSE));
1412 }
1413 else
1414 {
1415 /* use same address family as for other */
1416 if (!this->other_host->is_anyaddr(this->other_host))
1417 {
1418 family = this->other_host->get_family(this->other_host);
1419 }
1420 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1421
1422 if (host && host->is_anyaddr(host) &&
1423 !this->other_host->is_anyaddr(this->other_host))
1424 {
1425 host->destroy(host);
1426 host = charon->kernel->get_source_addr(charon->kernel,
1427 this->other_host, NULL);
1428 if (host)
1429 {
1430 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1431 }
1432 else
1433 { /* fallback to address family specific %any(6), if configured */
1434 host = this->ike_cfg->resolve_me(this->ike_cfg, family);
1435 }
1436 }
1437 }
1438 if (host)
1439 {
1440 set_my_host(this, host);
1441 }
1442 }
1443
1444 METHOD(ike_sa_t, initiate, status_t,
1445 private_ike_sa_t *this, child_cfg_t *child_cfg, uint32_t reqid,
1446 traffic_selector_t *tsi, traffic_selector_t *tsr)
1447 {
1448 bool defer_initiate = FALSE;
1449
1450 if (this->state == IKE_CREATED)
1451 {
1452 if (this->my_host->is_anyaddr(this->my_host) ||
1453 this->other_host->is_anyaddr(this->other_host))
1454 {
1455 resolve_hosts(this);
1456 }
1457
1458 if (this->other_host->is_anyaddr(this->other_host)
1459 #ifdef ME
1460 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1461 #endif /* ME */
1462 )
1463 {
1464 char *addr;
1465
1466 addr = this->ike_cfg->get_other_addr(this->ike_cfg);
1467 if (!this->retry_initiate_interval)
1468 {
1469 DBG1(DBG_IKE, "unable to resolve %s, initiate aborted",
1470 addr);
1471 DESTROY_IF(child_cfg);
1472 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1473 return DESTROY_ME;
1474 }
1475 DBG1(DBG_IKE, "unable to resolve %s, retrying in %ds",
1476 addr, this->retry_initiate_interval);
1477 defer_initiate = TRUE;
1478 }
1479
1480 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1481 this->task_manager->queue_ike(this->task_manager);
1482 }
1483
1484 #ifdef ME
1485 if (this->peer_cfg->is_mediation(this->peer_cfg))
1486 {
1487 if (this->state == IKE_ESTABLISHED)
1488 {
1489 /* mediation connection is already established, retrigger state
1490 * change to notify bus listeners */
1491 DBG1(DBG_IKE, "mediation connection is already up");
1492 set_state(this, IKE_ESTABLISHED);
1493 }
1494 DESTROY_IF(child_cfg);
1495 }
1496 else
1497 #endif /* ME */
1498 if (child_cfg)
1499 {
1500 /* normal IKE_SA with CHILD_SA */
1501 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1502 tsi, tsr);
1503 #ifdef ME
1504 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1505 {
1506 /* mediated connection, initiate mediation process */
1507 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1508 lib->processor->queue_job(lib->processor, job);
1509 return SUCCESS;
1510 }
1511 #endif /* ME */
1512 }
1513
1514 if (defer_initiate)
1515 {
1516 if (!this->retry_initiate_queued)
1517 {
1518 job_t *job = (job_t*)retry_initiate_job_create(this->ike_sa_id);
1519 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
1520 this->retry_initiate_interval);
1521 this->retry_initiate_queued = TRUE;
1522 }
1523 return SUCCESS;
1524 }
1525 this->retry_initiate_queued = FALSE;
1526 return this->task_manager->initiate(this->task_manager);
1527 }
1528
1529 METHOD(ike_sa_t, retry_initiate, status_t,
1530 private_ike_sa_t *this)
1531 {
1532 if (this->retry_initiate_queued)
1533 {
1534 this->retry_initiate_queued = FALSE;
1535 return initiate(this, NULL, 0, NULL, NULL);
1536 }
1537 return SUCCESS;
1538 }
1539
1540 METHOD(ike_sa_t, process_message, status_t,
1541 private_ike_sa_t *this, message_t *message)
1542 {
1543 status_t status;
1544
1545 if (this->state == IKE_PASSIVE)
1546 { /* do not handle messages in passive state */
1547 return FAILED;
1548 }
1549 if (message->get_major_version(message) != this->version)
1550 {
1551 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1552 exchange_type_names, message->get_exchange_type(message),
1553 message->get_major_version(message),
1554 ike_version_names, this->version);
1555 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1556 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1557 return FAILED;
1558 }
1559 status = this->task_manager->process_message(this->task_manager, message);
1560 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1561 {
1562 /* authentication completed but if the online validation is suspended we
1563 * need the auth cfgs until we did the delayed verification, we flush
1564 * them afterwards */
1565 if (!has_condition(this, COND_ONLINE_VALIDATION_SUSPENDED))
1566 {
1567 this->flush_auth_cfg = FALSE;
1568 flush_auth_cfgs(this);
1569 }
1570 }
1571 return status;
1572 }
1573
1574 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1575 private_ike_sa_t *this)
1576 {
1577 return this->ike_sa_id;
1578 }
1579
1580 METHOD(ike_sa_t, get_version, ike_version_t,
1581 private_ike_sa_t *this)
1582 {
1583 return this->version;
1584 }
1585
1586 METHOD(ike_sa_t, get_my_id, identification_t*,
1587 private_ike_sa_t *this)
1588 {
1589 return this->my_id;
1590 }
1591
1592 METHOD(ike_sa_t, set_my_id, void,
1593 private_ike_sa_t *this, identification_t *me)
1594 {
1595 DESTROY_IF(this->my_id);
1596 this->my_id = me;
1597 }
1598
1599 METHOD(ike_sa_t, get_other_id, identification_t*,
1600 private_ike_sa_t *this)
1601 {
1602 return this->other_id;
1603 }
1604
1605 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1606 private_ike_sa_t *this)
1607 {
1608 identification_t *id = NULL, *current;
1609 enumerator_t *enumerator;
1610 auth_cfg_t *cfg;
1611
1612 enumerator = array_create_enumerator(this->other_auths);
1613 while (enumerator->enumerate(enumerator, &cfg))
1614 {
1615 /* prefer EAP-Identity of last round */
1616 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1617 if (!current || current->get_type(current) == ID_ANY)
1618 {
1619 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1620 }
1621 if (!current || current->get_type(current) == ID_ANY)
1622 {
1623 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1624 }
1625 if (current && current->get_type(current) != ID_ANY)
1626 {
1627 id = current;
1628 continue;
1629 }
1630 }
1631 enumerator->destroy(enumerator);
1632 if (id)
1633 {
1634 return id;
1635 }
1636 return this->other_id;
1637 }
1638
1639 METHOD(ike_sa_t, set_other_id, void,
1640 private_ike_sa_t *this, identification_t *other)
1641 {
1642 DESTROY_IF(this->other_id);
1643 this->other_id = other;
1644 }
1645
1646 METHOD(ike_sa_t, add_child_sa, void,
1647 private_ike_sa_t *this, child_sa_t *child_sa)
1648 {
1649 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
1650 charon->child_sa_manager->add(charon->child_sa_manager,
1651 child_sa, &this->public);
1652 }
1653
1654 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1655 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi, bool inbound)
1656 {
1657 enumerator_t *enumerator;
1658 child_sa_t *current, *found = NULL;
1659
1660 enumerator = array_create_enumerator(this->child_sas);
1661 while (enumerator->enumerate(enumerator, (void**)&current))
1662 {
1663 if (current->get_spi(current, inbound) == spi &&
1664 current->get_protocol(current) == protocol)
1665 {
1666 found = current;
1667 }
1668 }
1669 enumerator->destroy(enumerator);
1670 return found;
1671 }
1672
1673 METHOD(ike_sa_t, get_child_count, int,
1674 private_ike_sa_t *this)
1675 {
1676 return array_count(this->child_sas);
1677 }
1678
1679 /**
1680 * Private data of a create_child_sa_enumerator()
1681 */
1682 typedef struct {
1683 /** implements enumerator */
1684 enumerator_t public;
1685 /** inner array enumerator */
1686 enumerator_t *inner;
1687 /** current item */
1688 child_sa_t *current;
1689 } child_enumerator_t;
1690
1691 METHOD(enumerator_t, child_enumerate, bool,
1692 child_enumerator_t *this, child_sa_t **child_sa)
1693 {
1694 if (this->inner->enumerate(this->inner, &this->current))
1695 {
1696 *child_sa = this->current;
1697 return TRUE;
1698 }
1699 return FALSE;
1700 }
1701
1702 METHOD(enumerator_t, child_enumerator_destroy, void,
1703 child_enumerator_t *this)
1704 {
1705 this->inner->destroy(this->inner);
1706 free(this);
1707 }
1708
1709 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1710 private_ike_sa_t *this)
1711 {
1712 child_enumerator_t *enumerator;
1713
1714 INIT(enumerator,
1715 .public = {
1716 .enumerate = (void*)_child_enumerate,
1717 .destroy = _child_enumerator_destroy,
1718 },
1719 .inner = array_create_enumerator(this->child_sas),
1720 );
1721 return &enumerator->public;
1722 }
1723
1724 METHOD(ike_sa_t, remove_child_sa, void,
1725 private_ike_sa_t *this, enumerator_t *enumerator)
1726 {
1727 child_enumerator_t *ce = (child_enumerator_t*)enumerator;
1728
1729 charon->child_sa_manager->remove(charon->child_sa_manager, ce->current);
1730 array_remove_at(this->child_sas, ce->inner);
1731 }
1732
1733 METHOD(ike_sa_t, rekey_child_sa, status_t,
1734 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi)
1735 {
1736 if (this->state == IKE_PASSIVE)
1737 {
1738 return INVALID_STATE;
1739 }
1740 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1741 return this->task_manager->initiate(this->task_manager);
1742 }
1743
1744 METHOD(ike_sa_t, delete_child_sa, status_t,
1745 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi, bool expired)
1746 {
1747 if (this->state == IKE_PASSIVE)
1748 {
1749 return INVALID_STATE;
1750 }
1751 this->task_manager->queue_child_delete(this->task_manager,
1752 protocol, spi, expired);
1753 return this->task_manager->initiate(this->task_manager);
1754 }
1755
1756 METHOD(ike_sa_t, destroy_child_sa, status_t,
1757 private_ike_sa_t *this, protocol_id_t protocol, uint32_t spi)
1758 {
1759 enumerator_t *enumerator;
1760 child_sa_t *child_sa;
1761 status_t status = NOT_FOUND;
1762
1763 enumerator = create_child_sa_enumerator(this);
1764 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1765 {
1766 if (child_sa->get_protocol(child_sa) == protocol &&
1767 child_sa->get_spi(child_sa, TRUE) == spi)
1768 {
1769 remove_child_sa(this, enumerator);
1770 child_sa->destroy(child_sa);
1771 status = SUCCESS;
1772 break;
1773 }
1774 }
1775 enumerator->destroy(enumerator);
1776 return status;
1777 }
1778
1779 METHOD(ike_sa_t, delete_, status_t,
1780 private_ike_sa_t *this)
1781 {
1782 switch (this->state)
1783 {
1784 case IKE_REKEYING:
1785 if (this->version == IKEV1)
1786 { /* SA has been reauthenticated, delete */
1787 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1788 break;
1789 }
1790 /* FALL */
1791 case IKE_ESTABLISHED:
1792 if (time_monotonic(NULL) >= this->stats[STAT_DELETE])
1793 { /* IKE_SA hard lifetime hit */
1794 charon->bus->alert(charon->bus, ALERT_IKE_SA_EXPIRED);
1795 }
1796 this->task_manager->queue_ike_delete(this->task_manager);
1797 return this->task_manager->initiate(this->task_manager);
1798 case IKE_CREATED:
1799 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1800 break;
1801 case IKE_PASSIVE:
1802 break;
1803 default:
1804 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1805 "without notification", ike_sa_state_names, this->state);
1806 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1807 break;
1808 }
1809 return DESTROY_ME;
1810 }
1811
1812 METHOD(ike_sa_t, rekey, status_t,
1813 private_ike_sa_t *this)
1814 {
1815 if (this->state == IKE_PASSIVE)
1816 {
1817 return INVALID_STATE;
1818 }
1819 this->task_manager->queue_ike_rekey(this->task_manager);
1820 return this->task_manager->initiate(this->task_manager);
1821 }
1822
1823 METHOD(ike_sa_t, reauth, status_t,
1824 private_ike_sa_t *this)
1825 {
1826 if (this->state == IKE_PASSIVE)
1827 {
1828 return INVALID_STATE;
1829 }
1830 if (this->state == IKE_CONNECTING)
1831 {
1832 DBG0(DBG_IKE, "reinitiating IKE_SA %s[%d]",
1833 get_name(this), this->unique_id);
1834 reset(this);
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 return this->task_manager->initiate(this->task_manager);
2339 }
2340 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
2341 break;
2342 }
2343 case IKE_DELETING:
2344 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
2345 if (has_condition(this, COND_REAUTHENTICATING))
2346 {
2347 DBG1(DBG_IKE, "delete during reauthentication failed, "
2348 "trying to reestablish IKE_SA anyway");
2349 reestablish(this);
2350 }
2351 break;
2352 case IKE_REKEYING:
2353 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
2354 /* FALL */
2355 default:
2356 reestablish(this);
2357 break;
2358 }
2359 if (this->state != IKE_CONNECTING &&
2360 this->state != IKE_REKEYED)
2361 {
2362 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
2363 }
2364 return DESTROY_ME;
2365 }
2366 return SUCCESS;
2367 }
2368
2369 METHOD(ike_sa_t, set_auth_lifetime, status_t,
2370 private_ike_sa_t *this, uint32_t lifetime)
2371 {
2372 uint32_t diff, hard, soft, now;
2373 bool send_update;
2374
2375 diff = this->peer_cfg->get_over_time(this->peer_cfg);
2376 now = time_monotonic(NULL);
2377 hard = now + lifetime;
2378 soft = hard - diff;
2379
2380 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
2381 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
2382 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
2383 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
2384 (array_count(this->other_vips) != 0 ||
2385 has_condition(this, COND_EAP_AUTHENTICATED));
2386
2387 if (lifetime < diff)
2388 {
2389 this->stats[STAT_REAUTH] = now;
2390
2391 if (!send_update)
2392 {
2393 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
2394 "starting reauthentication", lifetime);
2395 lib->processor->queue_job(lib->processor,
2396 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
2397 }
2398 }
2399 else if (this->stats[STAT_REAUTH] == 0 ||
2400 this->stats[STAT_REAUTH] > soft)
2401 {
2402 this->stats[STAT_REAUTH] = soft;
2403 if (!send_update)
2404 {
2405 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
2406 "reauthentication in %ds", lifetime, lifetime - diff);
2407 lib->scheduler->schedule_job(lib->scheduler,
2408 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
2409 lifetime - diff);
2410 }
2411 }
2412 else
2413 {
2414 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
2415 "reauthentication already scheduled in %ds", lifetime,
2416 this->stats[STAT_REAUTH] - time_monotonic(NULL));
2417 send_update = FALSE;
2418 }
2419 /* give at least some seconds to reauthenticate */
2420 this->stats[STAT_DELETE] = max(hard, now + 10);
2421
2422 #ifdef USE_IKEV2
2423 if (send_update)
2424 {
2425 ike_auth_lifetime_t *task;
2426
2427 task = ike_auth_lifetime_create(&this->public, TRUE);
2428 this->task_manager->queue_task(this->task_manager, &task->task);
2429 return this->task_manager->initiate(this->task_manager);
2430 }
2431 #endif
2432 return SUCCESS;
2433 }
2434
2435 /**
2436 * Check if the current combination of source and destination address is still
2437 * valid.
2438 */
2439 static bool is_current_path_valid(private_ike_sa_t *this)
2440 {
2441 bool valid = FALSE;
2442 host_t *src;
2443 src = charon->kernel->get_source_addr(charon->kernel, this->other_host,
2444 this->my_host);
2445 if (src)
2446 {
2447 if (src->ip_equals(src, this->my_host))
2448 {
2449 valid = TRUE;
2450 }
2451 src->destroy(src);
2452 }
2453 return valid;
2454 }
2455
2456 /**
2457 * Check if we have any path avialable for this IKE SA.
2458 */
2459 static bool is_any_path_valid(private_ike_sa_t *this)
2460 {
2461 bool valid = FALSE;
2462 enumerator_t *enumerator;
2463 host_t *src = NULL, *addr;
2464 int family = AF_UNSPEC;
2465
2466 switch (charon->socket->supported_families(charon->socket))
2467 {
2468 case SOCKET_FAMILY_IPV4:
2469 family = AF_INET;
2470 break;
2471 case SOCKET_FAMILY_IPV6:
2472 family = AF_INET6;
2473 break;
2474 case SOCKET_FAMILY_BOTH:
2475 case SOCKET_FAMILY_NONE:
2476 break;
2477 }
2478
2479 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
2480 enumerator = create_peer_address_enumerator(this);
2481 while (enumerator->enumerate(enumerator, &addr))
2482 {
2483 if (family != AF_UNSPEC && addr->get_family(addr) != family)
2484 {
2485 continue;
2486 }
2487 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
2488 src = charon->kernel->get_source_addr(charon->kernel, addr, NULL);
2489 if (src)
2490 {
2491 break;
2492 }
2493 }
2494 enumerator->destroy(enumerator);
2495 if (src)
2496 {
2497 valid = TRUE;
2498 src->destroy(src);
2499 }
2500 return valid;
2501 }
2502
2503 METHOD(ike_sa_t, roam, status_t,
2504 private_ike_sa_t *this, bool address)
2505 {
2506 switch (this->state)
2507 {
2508 case IKE_CREATED:
2509 case IKE_DELETING:
2510 case IKE_DESTROYING:
2511 case IKE_PASSIVE:
2512 case IKE_REKEYED:
2513 return SUCCESS;
2514 default:
2515 break;
2516 }
2517
2518 /* keep existing path if possible */
2519 if (is_current_path_valid(this))
2520 {
2521 DBG2(DBG_IKE, "keeping connection path %H - %H",
2522 this->my_host, this->other_host);
2523 set_condition(this, COND_STALE, FALSE);
2524
2525 if (supports_extension(this, EXT_MOBIKE) && address)
2526 { /* if any addresses changed, send an updated list */
2527 DBG1(DBG_IKE, "sending address list update using MOBIKE");
2528 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
2529 return this->task_manager->initiate(this->task_manager);
2530 }
2531 return SUCCESS;
2532 }
2533
2534 if (!is_any_path_valid(this))
2535 {
2536 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
2537 this->other_host);
2538 set_condition(this, COND_STALE, TRUE);
2539 return SUCCESS;
2540 }
2541 set_condition(this, COND_STALE, FALSE);
2542
2543 /* update addresses with mobike, if supported ... */
2544 if (supports_extension(this, EXT_MOBIKE))
2545 {
2546 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2547 { /* responder updates the peer about changed address config */
2548 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
2549 "implicitly requesting an address change");
2550 address = TRUE;
2551 }
2552 else
2553 {
2554 DBG1(DBG_IKE, "requesting address change using MOBIKE");
2555 }
2556 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
2557 return this->task_manager->initiate(this->task_manager);
2558 }
2559
2560 /* ... reauth if not */
2561 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
2562 { /* responder does not reauthenticate */
2563 set_condition(this, COND_STALE, TRUE);
2564 return SUCCESS;
2565 }
2566 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
2567 /* since our previous path is not valid anymore, try and find a new one */
2568 resolve_hosts(this);
2569 return reauth(this);
2570 }
2571
2572 METHOD(ike_sa_t, add_configuration_attribute, void,
2573 private_ike_sa_t *this, attribute_handler_t *handler,
2574 configuration_attribute_type_t type, chunk_t data)
2575 {
2576 attribute_entry_t entry = {
2577 .handler = handler,
2578 .type = type,
2579 .data = chunk_clone(data),
2580 };
2581 array_insert(this->attributes, ARRAY_TAIL, &entry);
2582 }
2583
2584 /**
2585 * Enumerator filter for attributes
2586 */
2587 static bool filter_attribute(void *null, attribute_entry_t **in,
2588 configuration_attribute_type_t *type, void *in2,
2589 chunk_t *data, void *in3, bool *handled)
2590 {
2591 *type = (*in)->type;
2592 *data = (*in)->data;
2593 *handled = (*in)->handler != NULL;
2594 return TRUE;
2595 }
2596
2597 METHOD(ike_sa_t, create_attribute_enumerator, enumerator_t*,
2598 private_ike_sa_t *this)
2599 {
2600 return enumerator_create_filter(array_create_enumerator(this->attributes),
2601 (void*)filter_attribute, NULL, NULL);
2602 }
2603
2604 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
2605 private_ike_sa_t *this, task_queue_t queue)
2606 {
2607 return this->task_manager->create_task_enumerator(this->task_manager, queue);
2608 }
2609
2610 METHOD(ike_sa_t, flush_queue, void,
2611 private_ike_sa_t *this, task_queue_t queue)
2612 {
2613 this->task_manager->flush_queue(this->task_manager, queue);
2614 }
2615
2616 METHOD(ike_sa_t, queue_task, void,
2617 private_ike_sa_t *this, task_t *task)
2618 {
2619 this->task_manager->queue_task(this->task_manager, task);
2620 }
2621
2622 METHOD(ike_sa_t, queue_task_delayed, void,
2623 private_ike_sa_t *this, task_t *task, uint32_t delay)
2624 {
2625 this->task_manager->queue_task_delayed(this->task_manager, task, delay);
2626 }
2627
2628 METHOD(ike_sa_t, inherit_pre, void,
2629 private_ike_sa_t *this, ike_sa_t *other_public)
2630 {
2631 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2632
2633 /* apply config and hosts */
2634 set_peer_cfg(this, other->peer_cfg);
2635 set_my_host(this, other->my_host->clone(other->my_host));
2636 set_other_host(this, other->other_host->clone(other->other_host));
2637
2638 /* apply extensions and conditions with a few exceptions */
2639 this->extensions = other->extensions;
2640 this->conditions = other->conditions;
2641 this->conditions &= ~COND_STALE;
2642 this->conditions &= ~COND_REAUTHENTICATING;
2643 }
2644
2645 METHOD(ike_sa_t, inherit_post, void,
2646 private_ike_sa_t *this, ike_sa_t *other_public)
2647 {
2648 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2649 child_sa_t *child_sa;
2650 enumerator_t *enumerator;
2651 attribute_entry_t entry;
2652 auth_cfg_t *cfg;
2653 host_t *vip;
2654
2655 /* apply hosts and ids */
2656 this->my_host->destroy(this->my_host);
2657 this->other_host->destroy(this->other_host);
2658 this->my_id->destroy(this->my_id);
2659 this->other_id->destroy(this->other_id);
2660 this->my_host = other->my_host->clone(other->my_host);
2661 this->other_host = other->other_host->clone(other->other_host);
2662 this->my_id = other->my_id->clone(other->my_id);
2663 this->other_id = other->other_id->clone(other->other_id);
2664
2665 /* apply assigned virtual IPs... */
2666 while (array_remove(other->my_vips, ARRAY_HEAD, &vip))
2667 {
2668 array_insert_create(&this->my_vips, ARRAY_TAIL, vip);
2669 }
2670 while (array_remove(other->other_vips, ARRAY_HEAD, &vip))
2671 {
2672 array_insert_create(&this->other_vips, ARRAY_TAIL, vip);
2673 }
2674
2675 /* MOBIKE additional addresses */
2676 while (array_remove(other->peer_addresses, ARRAY_HEAD, &vip))
2677 {
2678 array_insert_create(&this->peer_addresses, ARRAY_TAIL, vip);
2679 }
2680
2681 /* authentication information */
2682 enumerator = array_create_enumerator(other->my_auths);
2683 while (enumerator->enumerate(enumerator, &cfg))
2684 {
2685 array_insert(this->my_auths, ARRAY_TAIL, cfg->clone(cfg));
2686 }
2687 enumerator->destroy(enumerator);
2688 enumerator = array_create_enumerator(other->other_auths);
2689 while (enumerator->enumerate(enumerator, &cfg))
2690 {
2691 array_insert(this->other_auths, ARRAY_TAIL, cfg->clone(cfg));
2692 }
2693 enumerator->destroy(enumerator);
2694
2695 /* ... and configuration attributes */
2696 while (array_remove(other->attributes, ARRAY_HEAD, &entry))
2697 {
2698 array_insert(this->attributes, ARRAY_TAIL, &entry);
2699 }
2700
2701 /* inherit all conditions */
2702 this->conditions = other->conditions;
2703 if (this->conditions & COND_NAT_HERE)
2704 {
2705 send_keepalive(this, FALSE);
2706 }
2707
2708 #ifdef ME
2709 if (other->is_mediation_server)
2710 {
2711 act_as_mediation_server(this);
2712 }
2713 else if (other->server_reflexive_host)
2714 {
2715 this->server_reflexive_host = other->server_reflexive_host->clone(
2716 other->server_reflexive_host);
2717 }
2718 #endif /* ME */
2719
2720 /* adopt all children */
2721 while (array_remove(other->child_sas, ARRAY_HEAD, &child_sa))
2722 {
2723 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
2724 add_child_sa(this, child_sa);
2725 }
2726
2727 /* move pending tasks to the new IKE_SA */
2728 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2729
2730 /* reauthentication timeout survives a rekeying */
2731 if (other->stats[STAT_REAUTH])
2732 {
2733 time_t reauth, delete, now = time_monotonic(NULL);
2734
2735 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2736 reauth = this->stats[STAT_REAUTH] - now;
2737 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2738 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2739 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2740 "lifetime reduced to %ds", reauth, delete);
2741 lib->scheduler->schedule_job(lib->scheduler,
2742 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2743 lib->scheduler->schedule_job(lib->scheduler,
2744 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2745 }
2746 }
2747
2748 METHOD(ike_sa_t, destroy, void,
2749 private_ike_sa_t *this)
2750 {
2751 attribute_entry_t entry;
2752 child_sa_t *child_sa;
2753 host_t *vip;
2754
2755 charon->bus->set_sa(charon->bus, &this->public);
2756
2757 set_state(this, IKE_DESTROYING);
2758 if (this->task_manager)
2759 {
2760 this->task_manager->flush(this->task_manager);
2761 }
2762
2763 /* remove attributes first, as we pass the IKE_SA to the handler */
2764 charon->bus->handle_vips(charon->bus, &this->public, FALSE);
2765 while (array_remove(this->attributes, ARRAY_TAIL, &entry))
2766 {
2767 if (entry.handler)
2768 {
2769 charon->attributes->release(charon->attributes, entry.handler,
2770 &this->public, entry.type, entry.data);
2771 }
2772 free(entry.data.ptr);
2773 }
2774 /* uninstall CHILD_SAs before virtual IPs, otherwise we might kill
2775 * routes that the CHILD_SA tries to uninstall. */
2776 while (array_remove(this->child_sas, ARRAY_TAIL, &child_sa))
2777 {
2778 charon->child_sa_manager->remove(charon->child_sa_manager, child_sa);
2779 child_sa->destroy(child_sa);
2780 }
2781 while (array_remove(this->my_vips, ARRAY_TAIL, &vip))
2782 {
2783 charon->kernel->del_ip(charon->kernel, vip, -1, TRUE);
2784 vip->destroy(vip);
2785 }
2786 if (array_count(this->other_vips))
2787 {
2788 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
2789 }
2790 while (array_remove(this->other_vips, ARRAY_TAIL, &vip))
2791 {
2792 if (this->peer_cfg)
2793 {
2794 linked_list_t *pools;
2795
2796 pools = linked_list_create_from_enumerator(
2797 this->peer_cfg->create_pool_enumerator(this->peer_cfg));
2798 charon->attributes->release_address(charon->attributes,
2799 pools, vip, &this->public);
2800 pools->destroy(pools);
2801 }
2802 vip->destroy(vip);
2803 }
2804
2805 /* unset SA after here to avoid usage by the listeners */
2806 charon->bus->set_sa(charon->bus, NULL);
2807
2808 array_destroy(this->child_sas);
2809 DESTROY_IF(this->task_manager);
2810 DESTROY_IF(this->keymat);
2811 array_destroy(this->attributes);
2812 array_destroy(this->my_vips);
2813 array_destroy(this->other_vips);
2814 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
2815 #ifdef ME
2816 if (this->is_mediation_server)
2817 {
2818 charon->mediation_manager->remove(charon->mediation_manager,
2819 this->ike_sa_id);
2820 }
2821 DESTROY_IF(this->server_reflexive_host);
2822 chunk_free(&this->connect_id);
2823 #endif /* ME */
2824 free(this->nat_detection_dest.ptr);
2825
2826 DESTROY_IF(this->my_host);
2827 DESTROY_IF(this->other_host);
2828 DESTROY_IF(this->my_id);
2829 DESTROY_IF(this->other_id);
2830 DESTROY_IF(this->local_host);
2831 DESTROY_IF(this->remote_host);
2832 DESTROY_IF(this->redirected_from);
2833 array_destroy(this->redirected_at);
2834
2835 DESTROY_IF(this->ike_cfg);
2836 DESTROY_IF(this->peer_cfg);
2837 DESTROY_IF(this->proposal);
2838 this->my_auth->destroy(this->my_auth);
2839 this->other_auth->destroy(this->other_auth);
2840 array_destroy_offset(this->my_auths, offsetof(auth_cfg_t, destroy));
2841 array_destroy_offset(this->other_auths, offsetof(auth_cfg_t, destroy));
2842
2843 this->ike_sa_id->destroy(this->ike_sa_id);
2844 free(this);
2845 }
2846
2847 /*
2848 * Described in header.
2849 */
2850 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
2851 ike_version_t version)
2852 {
2853 private_ike_sa_t *this;
2854 static refcount_t unique_id = 0;
2855
2856 if (version == IKE_ANY)
2857 { /* prefer IKEv2 if protocol not specified */
2858 #ifdef USE_IKEV2
2859 version = IKEV2;
2860 #else
2861 version = IKEV1;
2862 #endif
2863 }
2864
2865 INIT(this,
2866 .public = {
2867 .get_version = _get_version,
2868 .get_state = _get_state,
2869 .set_state = _set_state,
2870 .get_name = _get_name,
2871 .get_statistic = _get_statistic,
2872 .set_statistic = _set_statistic,
2873 .process_message = _process_message,
2874 .initiate = _initiate,
2875 .retry_initiate = _retry_initiate,
2876 .get_ike_cfg = _get_ike_cfg,
2877 .set_ike_cfg = _set_ike_cfg,
2878 .get_peer_cfg = _get_peer_cfg,
2879 .set_peer_cfg = _set_peer_cfg,
2880 .get_auth_cfg = _get_auth_cfg,
2881 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2882 .verify_peer_certificate = _verify_peer_certificate,
2883 .add_auth_cfg = _add_auth_cfg,
2884 .get_proposal = _get_proposal,
2885 .set_proposal = _set_proposal,
2886 .get_id = _get_id,
2887 .get_my_host = _get_my_host,
2888 .set_my_host = _set_my_host,
2889 .get_other_host = _get_other_host,
2890 .set_other_host = _set_other_host,
2891 .set_message_id = _set_message_id,
2892 .float_ports = _float_ports,
2893 .update_hosts = _update_hosts,
2894 .get_my_id = _get_my_id,
2895 .set_my_id = _set_my_id,
2896 .get_other_id = _get_other_id,
2897 .set_other_id = _set_other_id,
2898 .get_other_eap_id = _get_other_eap_id,
2899 .enable_extension = _enable_extension,
2900 .supports_extension = _supports_extension,
2901 .set_condition = _set_condition,
2902 .has_condition = _has_condition,
2903 .set_pending_updates = _set_pending_updates,
2904 .get_pending_updates = _get_pending_updates,
2905 .create_peer_address_enumerator = _create_peer_address_enumerator,
2906 .add_peer_address = _add_peer_address,
2907 .clear_peer_addresses = _clear_peer_addresses,
2908 .has_mapping_changed = _has_mapping_changed,
2909 .retransmit = _retransmit,
2910 .delete = _delete_,
2911 .destroy = _destroy,
2912 .send_dpd = _send_dpd,
2913 .send_keepalive = _send_keepalive,
2914 .redirect = _redirect,
2915 .handle_redirect = _handle_redirect,
2916 .get_redirected_from = _get_redirected_from,
2917 .get_keymat = _get_keymat,
2918 .add_child_sa = _add_child_sa,
2919 .get_child_sa = _get_child_sa,
2920 .get_child_count = _get_child_count,
2921 .create_child_sa_enumerator = _create_child_sa_enumerator,
2922 .remove_child_sa = _remove_child_sa,
2923 .rekey_child_sa = _rekey_child_sa,
2924 .delete_child_sa = _delete_child_sa,
2925 .destroy_child_sa = _destroy_child_sa,
2926 .rekey = _rekey,
2927 .reauth = _reauth,
2928 .reestablish = _reestablish,
2929 .set_auth_lifetime = _set_auth_lifetime,
2930 .roam = _roam,
2931 .inherit_pre = _inherit_pre,
2932 .inherit_post = _inherit_post,
2933 .generate_message = _generate_message,
2934 .generate_message_fragmented = _generate_message_fragmented,
2935 .reset = _reset,
2936 .get_unique_id = _get_unique_id,
2937 .add_virtual_ip = _add_virtual_ip,
2938 .clear_virtual_ips = _clear_virtual_ips,
2939 .create_virtual_ip_enumerator = _create_virtual_ip_enumerator,
2940 .add_configuration_attribute = _add_configuration_attribute,
2941 .create_attribute_enumerator = _create_attribute_enumerator,
2942 .set_kmaddress = _set_kmaddress,
2943 .create_task_enumerator = _create_task_enumerator,
2944 .flush_queue = _flush_queue,
2945 .queue_task = _queue_task,
2946 .queue_task_delayed = _queue_task_delayed,
2947 #ifdef ME
2948 .act_as_mediation_server = _act_as_mediation_server,
2949 .get_server_reflexive_host = _get_server_reflexive_host,
2950 .set_server_reflexive_host = _set_server_reflexive_host,
2951 .get_connect_id = _get_connect_id,
2952 .initiate_mediation = _initiate_mediation,
2953 .initiate_mediated = _initiate_mediated,
2954 .relay = _relay,
2955 .callback = _callback,
2956 .respond = _respond,
2957 #endif /* ME */
2958 },
2959 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2960 .version = version,
2961 .my_host = host_create_any(AF_INET),
2962 .other_host = host_create_any(AF_INET),
2963 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2964 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2965 .keymat = keymat_create(version, initiator),
2966 .state = IKE_CREATED,
2967 .stats[STAT_INBOUND] = time_monotonic(NULL),
2968 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2969 .my_auth = auth_cfg_create(),
2970 .other_auth = auth_cfg_create(),
2971 .my_auths = array_create(0, 0),
2972 .other_auths = array_create(0, 0),
2973 .attributes = array_create(sizeof(attribute_entry_t), 0),
2974 .unique_id = ref_get(&unique_id),
2975 .keepalive_interval = lib->settings->get_time(lib->settings,
2976 "%s.keep_alive", KEEPALIVE_INTERVAL, lib->ns),
2977 .retry_initiate_interval = lib->settings->get_time(lib->settings,
2978 "%s.retry_initiate_interval", 0, lib->ns),
2979 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2980 "%s.flush_auth_cfg", FALSE, lib->ns),
2981 .fragment_size = lib->settings->get_int(lib->settings,
2982 "%s.fragment_size", 0, lib->ns),
2983 .follow_redirects = lib->settings->get_bool(lib->settings,
2984 "%s.follow_redirects", TRUE, lib->ns),
2985 );
2986
2987 if (version == IKEV2)
2988 { /* always supported with IKEv2 */
2989 enable_extension(this, EXT_DPD);
2990 }
2991
2992 this->task_manager = task_manager_create(&this->public);
2993 this->my_host->set_port(this->my_host,
2994 charon->socket->get_port(charon->socket, FALSE));
2995
2996 if (!this->task_manager || !this->keymat)
2997 {
2998 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2999 destroy(this);
3000 return NULL;
3001 }
3002 return &this->public;
3003 }