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