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