ike: Fix reestablishing SAs if no child-creating tasks are queued
[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 }
691 break;
692 }
693 default:
694 break;
695 }
696 charon->bus->ike_state_change(charon->bus, &this->public, state);
697 this->state = state;
698
699 if (trigger_dpd)
700 {
701 if (supports_extension(this, EXT_DPD))
702 {
703 send_dpd(this);
704 }
705 else
706 {
707 DBG1(DBG_IKE, "DPD not supported by peer, disabled");
708 }
709 }
710 }
711
712 METHOD(ike_sa_t, reset, void,
713 private_ike_sa_t *this)
714 {
715 /* the responder ID is reset, as peer may choose another one */
716 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
717 {
718 this->ike_sa_id->set_responder_spi(this->ike_sa_id, 0);
719 }
720
721 set_state(this, IKE_CREATED);
722
723 flush_auth_cfgs(this);
724
725 this->keymat->destroy(this->keymat);
726 this->keymat = keymat_create(this->version,
727 this->ike_sa_id->is_initiator(this->ike_sa_id));
728
729 this->task_manager->reset(this->task_manager, 0, 0);
730 }
731
732 METHOD(ike_sa_t, get_keymat, keymat_t*,
733 private_ike_sa_t *this)
734 {
735 return this->keymat;
736 }
737
738 METHOD(ike_sa_t, add_virtual_ip, void,
739 private_ike_sa_t *this, bool local, host_t *ip)
740 {
741 if (local)
742 {
743 char *iface;
744
745 if (hydra->kernel_interface->get_interface(hydra->kernel_interface,
746 this->my_host, &iface))
747 {
748 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
749 if (hydra->kernel_interface->add_ip(hydra->kernel_interface,
750 ip, -1, iface) == SUCCESS)
751 {
752 array_insert_create(&this->my_vips, ARRAY_TAIL, ip->clone(ip));
753 }
754 else
755 {
756 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
757 }
758 free(iface);
759 }
760 else
761 {
762 DBG1(DBG_IKE, "looking up interface for virtual IP %H failed", ip);
763 }
764 }
765 else
766 {
767 array_insert_create(&this->other_vips, ARRAY_TAIL, ip->clone(ip));
768 }
769 }
770
771
772 METHOD(ike_sa_t, clear_virtual_ips, void,
773 private_ike_sa_t *this, bool local)
774 {
775 array_t *vips;
776 host_t *vip;
777
778 vips = local ? this->my_vips : this->other_vips;
779 if (!local && array_count(vips))
780 {
781 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
782 }
783 while (array_remove(vips, ARRAY_HEAD, &vip))
784 {
785 if (local)
786 {
787 hydra->kernel_interface->del_ip(hydra->kernel_interface,
788 vip, -1, TRUE);
789 }
790 vip->destroy(vip);
791 }
792 }
793
794 METHOD(ike_sa_t, create_virtual_ip_enumerator, enumerator_t*,
795 private_ike_sa_t *this, bool local)
796 {
797 if (local)
798 {
799 return array_create_enumerator(this->my_vips);
800 }
801 return array_create_enumerator(this->other_vips);
802 }
803
804 METHOD(ike_sa_t, add_peer_address, void,
805 private_ike_sa_t *this, host_t *host)
806 {
807 array_insert_create(&this->peer_addresses, ARRAY_TAIL, host);
808 }
809
810 METHOD(ike_sa_t, create_peer_address_enumerator, enumerator_t*,
811 private_ike_sa_t *this)
812 {
813 if (this->peer_addresses)
814 {
815 return array_create_enumerator(this->peer_addresses);
816 }
817 /* in case we don't have MOBIKE */
818 return enumerator_create_single(this->other_host, NULL);
819 }
820
821 METHOD(ike_sa_t, clear_peer_addresses, void,
822 private_ike_sa_t *this)
823 {
824 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
825 this->peer_addresses = NULL;
826 }
827
828 METHOD(ike_sa_t, has_mapping_changed, bool,
829 private_ike_sa_t *this, chunk_t hash)
830 {
831 if (this->nat_detection_dest.ptr == NULL)
832 {
833 this->nat_detection_dest = chunk_clone(hash);
834 return FALSE;
835 }
836 if (chunk_equals(hash, this->nat_detection_dest))
837 {
838 return FALSE;
839 }
840 free(this->nat_detection_dest.ptr);
841 this->nat_detection_dest = chunk_clone(hash);
842 return TRUE;
843 }
844
845 METHOD(ike_sa_t, set_pending_updates, void,
846 private_ike_sa_t *this, u_int32_t updates)
847 {
848 this->pending_updates = updates;
849 }
850
851 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
852 private_ike_sa_t *this)
853 {
854 return this->pending_updates;
855 }
856
857 METHOD(ike_sa_t, float_ports, void,
858 private_ike_sa_t *this)
859 {
860 /* do not switch if we have a custom port from MOBIKE/NAT */
861 if (this->my_host->get_port(this->my_host) ==
862 charon->socket->get_port(charon->socket, FALSE))
863 {
864 this->my_host->set_port(this->my_host,
865 charon->socket->get_port(charon->socket, TRUE));
866 }
867 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
868 {
869 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
870 }
871 }
872
873 METHOD(ike_sa_t, update_hosts, void,
874 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
875 {
876 bool update = FALSE;
877
878 if (me == NULL)
879 {
880 me = this->my_host;
881 }
882 if (other == NULL)
883 {
884 other = this->other_host;
885 }
886
887 /* apply hosts on first received message */
888 if (this->my_host->is_anyaddr(this->my_host) ||
889 this->other_host->is_anyaddr(this->other_host))
890 {
891 set_my_host(this, me->clone(me));
892 set_other_host(this, other->clone(other));
893 update = TRUE;
894 }
895 else
896 {
897 /* update our address in any case */
898 if (force && !me->equals(me, this->my_host))
899 {
900 set_my_host(this, me->clone(me));
901 update = TRUE;
902 }
903
904 if (!other->equals(other, this->other_host))
905 {
906 /* update others address if we are NOT NATed */
907 if ((has_condition(this, COND_NAT_THERE) &&
908 !has_condition(this, COND_NAT_HERE)) || force )
909 {
910 set_other_host(this, other->clone(other));
911 update = TRUE;
912 }
913 }
914 }
915
916 /* update all associated CHILD_SAs, if required */
917 if (update)
918 {
919 enumerator_t *enumerator;
920 child_sa_t *child_sa;
921 linked_list_t *vips;
922
923 vips = linked_list_create_from_enumerator(
924 array_create_enumerator(this->my_vips));
925
926 enumerator = array_create_enumerator(this->child_sas);
927 while (enumerator->enumerate(enumerator, &child_sa))
928 {
929 if (child_sa->update(child_sa, this->my_host, this->other_host,
930 vips, has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
931 {
932 this->public.rekey_child_sa(&this->public,
933 child_sa->get_protocol(child_sa),
934 child_sa->get_spi(child_sa, TRUE));
935 }
936 }
937 enumerator->destroy(enumerator);
938
939 vips->destroy(vips);
940 }
941 }
942
943 /**
944 * Set configured DSCP value on packet
945 */
946 static void set_dscp(private_ike_sa_t *this, packet_t *packet)
947 {
948 ike_cfg_t *ike_cfg;
949
950 /* prefer IKE config on peer_cfg, as its selection is more accurate
951 * then the initial IKE config */
952 if (this->peer_cfg)
953 {
954 ike_cfg = this->peer_cfg->get_ike_cfg(this->peer_cfg);
955 }
956 else
957 {
958 ike_cfg = this->ike_cfg;
959 }
960 if (ike_cfg)
961 {
962 packet->set_dscp(packet, ike_cfg->get_dscp(ike_cfg));
963 }
964 }
965
966 METHOD(ike_sa_t, generate_message, status_t,
967 private_ike_sa_t *this, message_t *message, packet_t **packet)
968 {
969 status_t status;
970
971 if (message->is_encoded(message))
972 { /* already encoded in task, but set DSCP value */
973 *packet = message->get_packet(message);
974 set_dscp(this, *packet);
975 return SUCCESS;
976 }
977 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
978 message->set_ike_sa_id(message, this->ike_sa_id);
979 charon->bus->message(charon->bus, message, FALSE, TRUE);
980 status = message->generate(message, this->keymat, packet);
981 if (status == SUCCESS)
982 {
983 set_dscp(this, *packet);
984 charon->bus->message(charon->bus, message, FALSE, FALSE);
985 }
986 return status;
987 }
988
989 METHOD(ike_sa_t, set_kmaddress, void,
990 private_ike_sa_t *this, host_t *local, host_t *remote)
991 {
992 DESTROY_IF(this->local_host);
993 DESTROY_IF(this->remote_host);
994 this->local_host = local->clone(local);
995 this->remote_host = remote->clone(remote);
996 }
997
998 #ifdef ME
999 METHOD(ike_sa_t, act_as_mediation_server, void,
1000 private_ike_sa_t *this)
1001 {
1002 charon->mediation_manager->update_sa_id(charon->mediation_manager,
1003 this->other_id, this->ike_sa_id);
1004 this->is_mediation_server = TRUE;
1005 }
1006
1007 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
1008 private_ike_sa_t *this)
1009 {
1010 return this->server_reflexive_host;
1011 }
1012
1013 METHOD(ike_sa_t, set_server_reflexive_host, void,
1014 private_ike_sa_t *this, host_t *host)
1015 {
1016 DESTROY_IF(this->server_reflexive_host);
1017 this->server_reflexive_host = host;
1018 }
1019
1020 METHOD(ike_sa_t, get_connect_id, chunk_t,
1021 private_ike_sa_t *this)
1022 {
1023 return this->connect_id;
1024 }
1025
1026 METHOD(ike_sa_t, respond, status_t,
1027 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
1028 {
1029 ike_me_t *task = ike_me_create(&this->public, TRUE);
1030 task->respond(task, peer_id, connect_id);
1031 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1032 return this->task_manager->initiate(this->task_manager);
1033 }
1034
1035 METHOD(ike_sa_t, callback, status_t,
1036 private_ike_sa_t *this, identification_t *peer_id)
1037 {
1038 ike_me_t *task = ike_me_create(&this->public, TRUE);
1039 task->callback(task, peer_id);
1040 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1041 return this->task_manager->initiate(this->task_manager);
1042 }
1043
1044 METHOD(ike_sa_t, relay, status_t,
1045 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
1046 chunk_t connect_key, linked_list_t *endpoints, bool response)
1047 {
1048 ike_me_t *task = ike_me_create(&this->public, TRUE);
1049 task->relay(task, requester, connect_id, connect_key, endpoints, response);
1050 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1051 return this->task_manager->initiate(this->task_manager);
1052 }
1053
1054 METHOD(ike_sa_t, initiate_mediation, status_t,
1055 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1056 {
1057 ike_me_t *task = ike_me_create(&this->public, TRUE);
1058 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1059 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1060 return this->task_manager->initiate(this->task_manager);
1061 }
1062
1063 METHOD(ike_sa_t, initiate_mediated, status_t,
1064 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1065 {
1066 set_my_host(this, me->clone(me));
1067 set_other_host(this, other->clone(other));
1068 chunk_free(&this->connect_id);
1069 this->connect_id = chunk_clone(connect_id);
1070 return this->task_manager->initiate(this->task_manager);
1071 }
1072 #endif /* ME */
1073
1074 /**
1075 * Resolve DNS host in configuration
1076 */
1077 static void resolve_hosts(private_ike_sa_t *this)
1078 {
1079 host_t *host;
1080 int family = 0;
1081
1082 switch (charon->socket->supported_families(charon->socket))
1083 {
1084 case SOCKET_FAMILY_IPV4:
1085 family = AF_INET;
1086 break;
1087 case SOCKET_FAMILY_IPV6:
1088 family = AF_INET6;
1089 break;
1090 case SOCKET_FAMILY_BOTH:
1091 case SOCKET_FAMILY_NONE:
1092 break;
1093 }
1094
1095 if (this->remote_host)
1096 {
1097 host = this->remote_host->clone(this->remote_host);
1098 host->set_port(host, IKEV2_UDP_PORT);
1099 }
1100 else
1101 {
1102 char *other_addr;
1103 u_int16_t other_port;
1104
1105 other_addr = this->ike_cfg->get_other_addr(this->ike_cfg, NULL);
1106 other_port = this->ike_cfg->get_other_port(this->ike_cfg);
1107 host = host_create_from_dns(other_addr, family, other_port);
1108 }
1109 if (host)
1110 {
1111 set_other_host(this, host);
1112 }
1113
1114 if (this->local_host)
1115 {
1116 host = this->local_host->clone(this->local_host);
1117 host->set_port(host, charon->socket->get_port(charon->socket, FALSE));
1118 }
1119 else
1120 {
1121 char *my_addr;
1122 u_int16_t my_port;
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 my_addr = this->ike_cfg->get_my_addr(this->ike_cfg, NULL);
1130 my_port = this->ike_cfg->get_my_port(this->ike_cfg);
1131 host = host_create_from_dns(my_addr, family, my_port);
1132
1133 if (host && host->is_anyaddr(host) &&
1134 !this->other_host->is_anyaddr(this->other_host))
1135 {
1136 host->destroy(host);
1137 host = hydra->kernel_interface->get_source_addr(
1138 hydra->kernel_interface, this->other_host, NULL);
1139 if (host)
1140 {
1141 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1142 }
1143 else
1144 { /* fallback to address family specific %any(6), if configured */
1145 host = host_create_from_dns(my_addr, family, my_port);
1146 }
1147 }
1148 }
1149 if (host)
1150 {
1151 set_my_host(this, host);
1152 }
1153 }
1154
1155 METHOD(ike_sa_t, initiate, status_t,
1156 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1157 traffic_selector_t *tsi, traffic_selector_t *tsr)
1158 {
1159 bool defer_initiate = FALSE;
1160
1161 if (this->state == IKE_CREATED)
1162 {
1163 if (this->my_host->is_anyaddr(this->my_host) ||
1164 this->other_host->is_anyaddr(this->other_host))
1165 {
1166 resolve_hosts(this);
1167 }
1168
1169 if (this->other_host->is_anyaddr(this->other_host)
1170 #ifdef ME
1171 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1172 #endif /* ME */
1173 )
1174 {
1175 char *addr = this->ike_cfg->get_other_addr(this->ike_cfg, NULL);
1176 bool is_anyaddr = streq(addr, "%any") || streq(addr, "%any6");
1177
1178 if (is_anyaddr || !this->retry_initiate_interval)
1179 {
1180 if (is_anyaddr)
1181 {
1182 DBG1(DBG_IKE, "unable to initiate to %s", addr);
1183 }
1184 else
1185 {
1186 DBG1(DBG_IKE, "unable to resolve %s, initiate aborted",
1187 addr);
1188 }
1189 DESTROY_IF(child_cfg);
1190 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1191 return DESTROY_ME;
1192 }
1193 DBG1(DBG_IKE, "unable to resolve %s, retrying in %ds",
1194 addr, this->retry_initiate_interval);
1195 defer_initiate = TRUE;
1196 }
1197
1198 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1199 this->task_manager->queue_ike(this->task_manager);
1200 }
1201
1202 #ifdef ME
1203 if (this->peer_cfg->is_mediation(this->peer_cfg))
1204 {
1205 if (this->state == IKE_ESTABLISHED)
1206 {
1207 /* mediation connection is already established, retrigger state
1208 * change to notify bus listeners */
1209 DBG1(DBG_IKE, "mediation connection is already up");
1210 set_state(this, IKE_ESTABLISHED);
1211 }
1212 DESTROY_IF(child_cfg);
1213 }
1214 else
1215 #endif /* ME */
1216 if (child_cfg)
1217 {
1218 /* normal IKE_SA with CHILD_SA */
1219 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1220 tsi, tsr);
1221 #ifdef ME
1222 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1223 {
1224 /* mediated connection, initiate mediation process */
1225 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1226 lib->processor->queue_job(lib->processor, job);
1227 return SUCCESS;
1228 }
1229 #endif /* ME */
1230 }
1231
1232 if (defer_initiate)
1233 {
1234 if (!this->retry_initiate_queued)
1235 {
1236 job_t *job = (job_t*)retry_initiate_job_create(this->ike_sa_id);
1237 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
1238 this->retry_initiate_interval);
1239 this->retry_initiate_queued = TRUE;
1240 }
1241 return SUCCESS;
1242 }
1243 this->retry_initiate_queued = FALSE;
1244 return this->task_manager->initiate(this->task_manager);
1245 }
1246
1247 METHOD(ike_sa_t, retry_initiate, status_t,
1248 private_ike_sa_t *this)
1249 {
1250 if (this->retry_initiate_queued)
1251 {
1252 this->retry_initiate_queued = FALSE;
1253 return initiate(this, NULL, 0, NULL, NULL);
1254 }
1255 return SUCCESS;
1256 }
1257
1258 METHOD(ike_sa_t, process_message, status_t,
1259 private_ike_sa_t *this, message_t *message)
1260 {
1261 status_t status;
1262
1263 if (this->state == IKE_PASSIVE)
1264 { /* do not handle messages in passive state */
1265 return FAILED;
1266 }
1267 if (message->get_major_version(message) != this->version)
1268 {
1269 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1270 exchange_type_names, message->get_exchange_type(message),
1271 message->get_major_version(message),
1272 ike_version_names, this->version);
1273 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1274 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1275 return FAILED;
1276 }
1277 status = this->task_manager->process_message(this->task_manager, message);
1278 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1279 {
1280 /* authentication completed */
1281 this->flush_auth_cfg = FALSE;
1282 flush_auth_cfgs(this);
1283 }
1284 return status;
1285 }
1286
1287 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1288 private_ike_sa_t *this)
1289 {
1290 return this->ike_sa_id;
1291 }
1292
1293 METHOD(ike_sa_t, get_version, ike_version_t,
1294 private_ike_sa_t *this)
1295 {
1296 return this->version;
1297 }
1298
1299 METHOD(ike_sa_t, get_my_id, identification_t*,
1300 private_ike_sa_t *this)
1301 {
1302 return this->my_id;
1303 }
1304
1305 METHOD(ike_sa_t, set_my_id, void,
1306 private_ike_sa_t *this, identification_t *me)
1307 {
1308 DESTROY_IF(this->my_id);
1309 this->my_id = me;
1310 }
1311
1312 METHOD(ike_sa_t, get_other_id, identification_t*,
1313 private_ike_sa_t *this)
1314 {
1315 return this->other_id;
1316 }
1317
1318 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1319 private_ike_sa_t *this)
1320 {
1321 identification_t *id = NULL, *current;
1322 enumerator_t *enumerator;
1323 auth_cfg_t *cfg;
1324
1325 enumerator = array_create_enumerator(this->other_auths);
1326 while (enumerator->enumerate(enumerator, &cfg))
1327 {
1328 /* prefer EAP-Identity of last round */
1329 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1330 if (!current || current->get_type(current) == ID_ANY)
1331 {
1332 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1333 }
1334 if (!current || current->get_type(current) == ID_ANY)
1335 {
1336 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1337 }
1338 if (current && current->get_type(current) != ID_ANY)
1339 {
1340 id = current;
1341 continue;
1342 }
1343 }
1344 enumerator->destroy(enumerator);
1345 if (id)
1346 {
1347 return id;
1348 }
1349 return this->other_id;
1350 }
1351
1352 METHOD(ike_sa_t, set_other_id, void,
1353 private_ike_sa_t *this, identification_t *other)
1354 {
1355 DESTROY_IF(this->other_id);
1356 this->other_id = other;
1357 }
1358
1359 METHOD(ike_sa_t, add_child_sa, void,
1360 private_ike_sa_t *this, child_sa_t *child_sa)
1361 {
1362 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
1363 }
1364
1365 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1366 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1367 {
1368 enumerator_t *enumerator;
1369 child_sa_t *current, *found = NULL;
1370
1371 enumerator = array_create_enumerator(this->child_sas);
1372 while (enumerator->enumerate(enumerator, (void**)&current))
1373 {
1374 if (current->get_spi(current, inbound) == spi &&
1375 current->get_protocol(current) == protocol)
1376 {
1377 found = current;
1378 }
1379 }
1380 enumerator->destroy(enumerator);
1381 return found;
1382 }
1383
1384 METHOD(ike_sa_t, get_child_count, int,
1385 private_ike_sa_t *this)
1386 {
1387 return array_count(this->child_sas);
1388 }
1389
1390 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1391 private_ike_sa_t *this)
1392 {
1393 return array_create_enumerator(this->child_sas);
1394 }
1395
1396 METHOD(ike_sa_t, remove_child_sa, void,
1397 private_ike_sa_t *this, enumerator_t *enumerator)
1398 {
1399 array_remove_at(this->child_sas, enumerator);
1400 }
1401
1402 METHOD(ike_sa_t, rekey_child_sa, status_t,
1403 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1404 {
1405 if (this->state == IKE_PASSIVE)
1406 {
1407 return INVALID_STATE;
1408 }
1409 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1410 return this->task_manager->initiate(this->task_manager);
1411 }
1412
1413 METHOD(ike_sa_t, delete_child_sa, status_t,
1414 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool expired)
1415 {
1416 if (this->state == IKE_PASSIVE)
1417 {
1418 return INVALID_STATE;
1419 }
1420 this->task_manager->queue_child_delete(this->task_manager,
1421 protocol, spi, expired);
1422 return this->task_manager->initiate(this->task_manager);
1423 }
1424
1425 METHOD(ike_sa_t, destroy_child_sa, status_t,
1426 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1427 {
1428 enumerator_t *enumerator;
1429 child_sa_t *child_sa;
1430 status_t status = NOT_FOUND;
1431
1432 enumerator = array_create_enumerator(this->child_sas);
1433 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1434 {
1435 if (child_sa->get_protocol(child_sa) == protocol &&
1436 child_sa->get_spi(child_sa, TRUE) == spi)
1437 {
1438 array_remove_at(this->child_sas, enumerator);
1439 child_sa->destroy(child_sa);
1440 status = SUCCESS;
1441 break;
1442 }
1443 }
1444 enumerator->destroy(enumerator);
1445 return status;
1446 }
1447
1448 METHOD(ike_sa_t, delete_, status_t,
1449 private_ike_sa_t *this)
1450 {
1451 switch (this->state)
1452 {
1453 case IKE_REKEYING:
1454 if (this->version == IKEV1)
1455 { /* SA has been reauthenticated, delete */
1456 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1457 break;
1458 }
1459 /* FALL */
1460 case IKE_ESTABLISHED:
1461 if (time_monotonic(NULL) >= this->stats[STAT_DELETE])
1462 { /* IKE_SA hard lifetime hit */
1463 charon->bus->alert(charon->bus, ALERT_IKE_SA_EXPIRED);
1464 }
1465 this->task_manager->queue_ike_delete(this->task_manager);
1466 return this->task_manager->initiate(this->task_manager);
1467 case IKE_CREATED:
1468 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1469 break;
1470 case IKE_PASSIVE:
1471 break;
1472 default:
1473 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1474 "without notification", ike_sa_state_names, this->state);
1475 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1476 break;
1477 }
1478 return DESTROY_ME;
1479 }
1480
1481 METHOD(ike_sa_t, rekey, status_t,
1482 private_ike_sa_t *this)
1483 {
1484 if (this->state == IKE_PASSIVE)
1485 {
1486 return INVALID_STATE;
1487 }
1488 this->task_manager->queue_ike_rekey(this->task_manager);
1489 return this->task_manager->initiate(this->task_manager);
1490 }
1491
1492 METHOD(ike_sa_t, reauth, status_t,
1493 private_ike_sa_t *this)
1494 {
1495 if (this->state == IKE_PASSIVE)
1496 {
1497 return INVALID_STATE;
1498 }
1499 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1500 * If the peer does not support RFC4478, there is no way to keep the
1501 * IKE_SA up. */
1502 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1503 {
1504 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1505 if (array_count(this->other_vips) != 0 ||
1506 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1507 has_condition(this, COND_EAP_AUTHENTICATED)
1508 #ifdef ME
1509 /* as mediation server we too cannot reauth the IKE_SA */
1510 || this->is_mediation_server
1511 #endif /* ME */
1512 )
1513 {
1514 time_t del, now;
1515
1516 del = this->stats[STAT_DELETE];
1517 now = time_monotonic(NULL);
1518 DBG1(DBG_IKE, "IKE_SA %s[%d] will timeout in %V",
1519 get_name(this), this->unique_id, &now, &del);
1520 return FAILED;
1521 }
1522 else
1523 {
1524 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d] actively",
1525 get_name(this), this->unique_id);
1526 }
1527 }
1528 else
1529 {
1530 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d]",
1531 get_name(this), this->unique_id);
1532 }
1533 set_condition(this, COND_REAUTHENTICATING, TRUE);
1534 this->task_manager->queue_ike_reauth(this->task_manager);
1535 return this->task_manager->initiate(this->task_manager);
1536 }
1537
1538 /**
1539 * Check if tasks to create CHILD_SAs are queued in the given queue
1540 */
1541 static bool is_child_queued(private_ike_sa_t *this, task_queue_t queue)
1542 {
1543 enumerator_t *enumerator;
1544 task_t *task;
1545 bool found = FALSE;
1546
1547 enumerator = this->task_manager->create_task_enumerator(this->task_manager,
1548 queue);
1549 while (enumerator->enumerate(enumerator, &task))
1550 {
1551 if (task->get_type(task) == TASK_CHILD_CREATE ||
1552 task->get_type(task) == TASK_QUICK_MODE)
1553 {
1554 found = TRUE;
1555 break;
1556 }
1557 }
1558 enumerator->destroy(enumerator);
1559 return found;
1560 }
1561
1562 METHOD(ike_sa_t, reestablish, status_t,
1563 private_ike_sa_t *this)
1564 {
1565 ike_sa_t *new;
1566 host_t *host;
1567 action_t action;
1568 enumerator_t *enumerator;
1569 child_sa_t *child_sa;
1570 child_cfg_t *child_cfg;
1571 bool restart = FALSE;
1572 status_t status = FAILED;
1573
1574 if (has_condition(this, COND_REAUTHENTICATING))
1575 { /* only reauthenticate if we have children */
1576 if (array_count(this->child_sas) == 0
1577 #ifdef ME
1578 /* allow reauth of mediation connections without CHILD_SAs */
1579 && !this->peer_cfg->is_mediation(this->peer_cfg)
1580 #endif /* ME */
1581 )
1582 {
1583 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA "
1584 "to recreate");
1585 }
1586 else
1587 {
1588 restart = TRUE;
1589 }
1590 }
1591 else
1592 { /* check if we have children to keep up at all */
1593 enumerator = array_create_enumerator(this->child_sas);
1594 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1595 {
1596 if (this->state == IKE_DELETING)
1597 {
1598 action = child_sa->get_close_action(child_sa);
1599 }
1600 else
1601 {
1602 action = child_sa->get_dpd_action(child_sa);
1603 }
1604 switch (action)
1605 {
1606 case ACTION_RESTART:
1607 restart = TRUE;
1608 break;
1609 case ACTION_ROUTE:
1610 charon->traps->install(charon->traps, this->peer_cfg,
1611 child_sa->get_config(child_sa),
1612 child_sa->get_reqid(child_sa));
1613 break;
1614 default:
1615 break;
1616 }
1617 }
1618 enumerator->destroy(enumerator);
1619 /* check if we have tasks that recreate children */
1620 if (!restart)
1621 {
1622 restart = is_child_queued(this, TASK_QUEUE_ACTIVE) ||
1623 is_child_queued(this, TASK_QUEUE_QUEUED);
1624 }
1625 #ifdef ME
1626 /* mediation connections have no children, keep them up anyway */
1627 if (this->peer_cfg->is_mediation(this->peer_cfg))
1628 {
1629 restart = TRUE;
1630 }
1631 #endif /* ME */
1632 }
1633 if (!restart)
1634 {
1635 return FAILED;
1636 }
1637
1638 /* check if we are able to reestablish this IKE_SA */
1639 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1640 (array_count(this->other_vips) != 0 ||
1641 has_condition(this, COND_EAP_AUTHENTICATED)
1642 #ifdef ME
1643 || this->is_mediation_server
1644 #endif /* ME */
1645 ))
1646 {
1647 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1648 return FAILED;
1649 }
1650
1651 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1652 this->version, TRUE);
1653 if (!new)
1654 {
1655 return FAILED;
1656 }
1657 new->set_peer_cfg(new, this->peer_cfg);
1658 host = this->other_host;
1659 new->set_other_host(new, host->clone(host));
1660 host = this->my_host;
1661 new->set_my_host(new, host->clone(host));
1662 /* if we already have a virtual IP, we reuse it */
1663 enumerator = array_create_enumerator(this->my_vips);
1664 while (enumerator->enumerate(enumerator, &host))
1665 {
1666 new->add_virtual_ip(new, TRUE, host);
1667 }
1668 enumerator->destroy(enumerator);
1669
1670 #ifdef ME
1671 if (this->peer_cfg->is_mediation(this->peer_cfg))
1672 {
1673 status = new->initiate(new, NULL, 0, NULL, NULL);
1674 }
1675 else
1676 #endif /* ME */
1677 {
1678 /* handle existing CHILD_SAs */
1679 enumerator = array_create_enumerator(this->child_sas);
1680 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1681 {
1682 if (has_condition(this, COND_REAUTHENTICATING))
1683 {
1684 switch (child_sa->get_state(child_sa))
1685 {
1686 case CHILD_ROUTED:
1687 { /* move routed child directly */
1688 array_remove_at(this->child_sas, enumerator);
1689 new->add_child_sa(new, child_sa);
1690 action = ACTION_NONE;
1691 break;
1692 }
1693 default:
1694 { /* initiate/queue all other CHILD_SAs */
1695 action = ACTION_RESTART;
1696 break;
1697 }
1698 }
1699 }
1700 else
1701 { /* only restart CHILD_SAs that are configured accordingly */
1702 if (this->state == IKE_DELETING)
1703 {
1704 action = child_sa->get_close_action(child_sa);
1705 }
1706 else
1707 {
1708 action = child_sa->get_dpd_action(child_sa);
1709 }
1710 }
1711 switch (action)
1712 {
1713 case ACTION_RESTART:
1714 child_cfg = child_sa->get_config(child_sa);
1715 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1716 child_cfg->get_name(child_cfg));
1717 child_cfg->get_ref(child_cfg);
1718 status = new->initiate(new, child_cfg,
1719 child_sa->get_reqid(child_sa), NULL, NULL);
1720 break;
1721 default:
1722 continue;
1723 }
1724 if (status == DESTROY_ME)
1725 {
1726 break;
1727 }
1728 }
1729 enumerator->destroy(enumerator);
1730 /* adopt any active or queued CHILD-creating tasks */
1731 if (status != DESTROY_ME)
1732 {
1733 task_manager_t *other_tasks = ((private_ike_sa_t*)new)->task_manager;
1734 other_tasks->adopt_child_tasks(other_tasks, this->task_manager);
1735 if (new->get_state(new) == IKE_CREATED)
1736 {
1737 status = new->initiate(new, NULL, 0, NULL, NULL);
1738 }
1739 }
1740 }
1741
1742 if (status == DESTROY_ME)
1743 {
1744 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1745 status = FAILED;
1746 }
1747 else
1748 {
1749 charon->bus->ike_reestablish(charon->bus, &this->public, new);
1750 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1751 status = SUCCESS;
1752 }
1753 charon->bus->set_sa(charon->bus, &this->public);
1754 return status;
1755 }
1756
1757 METHOD(ike_sa_t, retransmit, status_t,
1758 private_ike_sa_t *this, u_int32_t message_id)
1759 {
1760 if (this->state == IKE_PASSIVE)
1761 {
1762 return INVALID_STATE;
1763 }
1764 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1765 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1766 {
1767 /* send a proper signal to brief interested bus listeners */
1768 switch (this->state)
1769 {
1770 case IKE_CONNECTING:
1771 {
1772 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1773 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1774 charon->bus->alert(charon->bus, ALERT_PEER_INIT_UNREACHABLE,
1775 this->keyingtry);
1776 this->keyingtry++;
1777 if (tries == 0 || tries > this->keyingtry)
1778 {
1779 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1780 this->keyingtry + 1, tries);
1781 reset(this);
1782 resolve_hosts(this);
1783 this->task_manager->queue_ike(this->task_manager);
1784 return this->task_manager->initiate(this->task_manager);
1785 }
1786 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1787 break;
1788 }
1789 case IKE_DELETING:
1790 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1791 if (has_condition(this, COND_REAUTHENTICATING))
1792 {
1793 DBG1(DBG_IKE, "delete during reauthentication failed, "
1794 "trying to reestablish IKE_SA anyway");
1795 reestablish(this);
1796 }
1797 break;
1798 case IKE_REKEYING:
1799 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1800 /* FALL */
1801 default:
1802 reestablish(this);
1803 break;
1804 }
1805 if (this->state != IKE_CONNECTING)
1806 {
1807 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1808 }
1809 return DESTROY_ME;
1810 }
1811 return SUCCESS;
1812 }
1813
1814 METHOD(ike_sa_t, set_auth_lifetime, status_t,
1815 private_ike_sa_t *this, u_int32_t lifetime)
1816 {
1817 u_int32_t diff, hard, soft, now;
1818 bool send_update;
1819
1820 diff = this->peer_cfg->get_over_time(this->peer_cfg);
1821 now = time_monotonic(NULL);
1822 hard = now + lifetime;
1823 soft = hard - diff;
1824
1825 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1826 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1827 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
1828 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
1829 (array_count(this->other_vips) != 0 ||
1830 has_condition(this, COND_EAP_AUTHENTICATED));
1831
1832 if (lifetime < diff)
1833 {
1834 this->stats[STAT_REAUTH] = now;
1835
1836 if (!send_update)
1837 {
1838 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1839 "starting reauthentication", lifetime);
1840 lib->processor->queue_job(lib->processor,
1841 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1842 }
1843 }
1844 else if (this->stats[STAT_REAUTH] == 0 ||
1845 this->stats[STAT_REAUTH] > soft)
1846 {
1847 this->stats[STAT_REAUTH] = soft;
1848 if (!send_update)
1849 {
1850 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
1851 "reauthentication in %ds", lifetime, lifetime - diff);
1852 lib->scheduler->schedule_job(lib->scheduler,
1853 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1854 lifetime - diff);
1855 }
1856 }
1857 else
1858 {
1859 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1860 "reauthentication already scheduled in %ds", lifetime,
1861 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1862 send_update = FALSE;
1863 }
1864 /* give at least some seconds to reauthenticate */
1865 this->stats[STAT_DELETE] = max(hard, now + 10);
1866
1867 #ifdef USE_IKEV2
1868 if (send_update)
1869 {
1870 ike_auth_lifetime_t *task;
1871
1872 task = ike_auth_lifetime_create(&this->public, TRUE);
1873 this->task_manager->queue_task(this->task_manager, &task->task);
1874 return this->task_manager->initiate(this->task_manager);
1875 }
1876 #endif
1877 return SUCCESS;
1878 }
1879
1880 /**
1881 * Check if the current combination of source and destination address is still
1882 * valid.
1883 */
1884 static bool is_current_path_valid(private_ike_sa_t *this)
1885 {
1886 bool valid = FALSE;
1887 host_t *src;
1888 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1889 this->other_host, this->my_host);
1890 if (src)
1891 {
1892 if (src->ip_equals(src, this->my_host))
1893 {
1894 valid = TRUE;
1895 }
1896 src->destroy(src);
1897 }
1898 return valid;
1899 }
1900
1901 /**
1902 * Check if we have any path avialable for this IKE SA.
1903 */
1904 static bool is_any_path_valid(private_ike_sa_t *this)
1905 {
1906 bool valid = FALSE;
1907 enumerator_t *enumerator;
1908 host_t *src = NULL, *addr;
1909
1910 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1911 enumerator = create_peer_address_enumerator(this);
1912 while (enumerator->enumerate(enumerator, &addr))
1913 {
1914 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1915 src = hydra->kernel_interface->get_source_addr(
1916 hydra->kernel_interface, addr, NULL);
1917 if (src)
1918 {
1919 break;
1920 }
1921 }
1922 enumerator->destroy(enumerator);
1923 if (src)
1924 {
1925 valid = TRUE;
1926 src->destroy(src);
1927 }
1928 return valid;
1929 }
1930
1931 METHOD(ike_sa_t, roam, status_t,
1932 private_ike_sa_t *this, bool address)
1933 {
1934 switch (this->state)
1935 {
1936 case IKE_CREATED:
1937 case IKE_DELETING:
1938 case IKE_DESTROYING:
1939 case IKE_PASSIVE:
1940 return SUCCESS;
1941 default:
1942 break;
1943 }
1944
1945 /* keep existing path if possible */
1946 if (is_current_path_valid(this))
1947 {
1948 DBG2(DBG_IKE, "keeping connection path %H - %H",
1949 this->my_host, this->other_host);
1950 set_condition(this, COND_STALE, FALSE);
1951
1952 if (supports_extension(this, EXT_MOBIKE) && address)
1953 { /* if any addresses changed, send an updated list */
1954 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1955 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
1956 return this->task_manager->initiate(this->task_manager);
1957 }
1958 return SUCCESS;
1959 }
1960
1961 if (!is_any_path_valid(this))
1962 {
1963 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1964 this->other_host);
1965 set_condition(this, COND_STALE, TRUE);
1966 return SUCCESS;
1967 }
1968 set_condition(this, COND_STALE, FALSE);
1969
1970 /* update addresses with mobike, if supported ... */
1971 if (supports_extension(this, EXT_MOBIKE))
1972 {
1973 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1974 { /* responder updates the peer about changed address config */
1975 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1976 "implicitly requesting an address change");
1977 address = TRUE;
1978 }
1979 else
1980 {
1981 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1982 }
1983 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
1984 return this->task_manager->initiate(this->task_manager);
1985 }
1986
1987 /* ... reauth if not */
1988 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1989 { /* responder does not reauthenticate */
1990 set_condition(this, COND_STALE, TRUE);
1991 return SUCCESS;
1992 }
1993 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1994 /* since our previous path is not valid anymore, try and find a new one */
1995 resolve_hosts(this);
1996 return reauth(this);
1997 }
1998
1999 METHOD(ike_sa_t, add_configuration_attribute, void,
2000 private_ike_sa_t *this, attribute_handler_t *handler,
2001 configuration_attribute_type_t type, chunk_t data)
2002 {
2003 attribute_entry_t entry = {
2004 .handler = handler,
2005 .type = type,
2006 .data = chunk_clone(data),
2007 };
2008 array_insert(this->attributes, ARRAY_TAIL, &entry);
2009 }
2010
2011 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
2012 private_ike_sa_t *this, task_queue_t queue)
2013 {
2014 return this->task_manager->create_task_enumerator(this->task_manager, queue);
2015 }
2016
2017 METHOD(ike_sa_t, flush_queue, void,
2018 private_ike_sa_t *this, task_queue_t queue)
2019 {
2020 this->task_manager->flush_queue(this->task_manager, queue);
2021 }
2022
2023 METHOD(ike_sa_t, queue_task, void,
2024 private_ike_sa_t *this, task_t *task)
2025 {
2026 this->task_manager->queue_task(this->task_manager, task);
2027 }
2028
2029 METHOD(ike_sa_t, inherit, void,
2030 private_ike_sa_t *this, ike_sa_t *other_public)
2031 {
2032 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
2033 child_sa_t *child_sa;
2034 enumerator_t *enumerator;
2035 attribute_entry_t entry;
2036 auth_cfg_t *cfg;
2037 host_t *vip;
2038
2039 /* apply hosts and ids */
2040 this->my_host->destroy(this->my_host);
2041 this->other_host->destroy(this->other_host);
2042 this->my_id->destroy(this->my_id);
2043 this->other_id->destroy(this->other_id);
2044 this->my_host = other->my_host->clone(other->my_host);
2045 this->other_host = other->other_host->clone(other->other_host);
2046 this->my_id = other->my_id->clone(other->my_id);
2047 this->other_id = other->other_id->clone(other->other_id);
2048
2049 /* apply assigned virtual IPs... */
2050 while (array_remove(other->my_vips, ARRAY_HEAD, &vip))
2051 {
2052 array_insert_create(&this->my_vips, ARRAY_TAIL, vip);
2053 }
2054 while (array_remove(other->other_vips, ARRAY_HEAD, &vip))
2055 {
2056 array_insert_create(&this->other_vips, ARRAY_TAIL, vip);
2057 }
2058
2059 /* authentication information */
2060 enumerator = array_create_enumerator(other->my_auths);
2061 while (enumerator->enumerate(enumerator, &cfg))
2062 {
2063 array_insert(this->my_auths, ARRAY_TAIL, cfg->clone(cfg));
2064 }
2065 enumerator->destroy(enumerator);
2066 enumerator = array_create_enumerator(other->other_auths);
2067 while (enumerator->enumerate(enumerator, &cfg))
2068 {
2069 array_insert(this->other_auths, ARRAY_TAIL, cfg->clone(cfg));
2070 }
2071 enumerator->destroy(enumerator);
2072
2073 /* ... and configuration attributes */
2074 while (array_remove(other->attributes, ARRAY_HEAD, &entry))
2075 {
2076 array_insert(this->attributes, ARRAY_TAIL, &entry);
2077 }
2078
2079 /* inherit all conditions */
2080 this->conditions = other->conditions;
2081 if (this->conditions & COND_NAT_HERE)
2082 {
2083 send_keepalive(this);
2084 }
2085
2086 #ifdef ME
2087 if (other->is_mediation_server)
2088 {
2089 act_as_mediation_server(this);
2090 }
2091 else if (other->server_reflexive_host)
2092 {
2093 this->server_reflexive_host = other->server_reflexive_host->clone(
2094 other->server_reflexive_host);
2095 }
2096 #endif /* ME */
2097
2098 /* adopt all children */
2099 while (array_remove(other->child_sas, ARRAY_HEAD, &child_sa))
2100 {
2101 array_insert_create(&this->child_sas, ARRAY_TAIL, child_sa);
2102 }
2103
2104 /* move pending tasks to the new IKE_SA */
2105 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2106
2107 /* reauthentication timeout survives a rekeying */
2108 if (other->stats[STAT_REAUTH])
2109 {
2110 time_t reauth, delete, now = time_monotonic(NULL);
2111
2112 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2113 reauth = this->stats[STAT_REAUTH] - now;
2114 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2115 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2116 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2117 "lifetime reduced to %ds", reauth, delete);
2118 lib->scheduler->schedule_job(lib->scheduler,
2119 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2120 lib->scheduler->schedule_job(lib->scheduler,
2121 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2122 }
2123 }
2124
2125 METHOD(ike_sa_t, destroy, void,
2126 private_ike_sa_t *this)
2127 {
2128 attribute_entry_t entry;
2129 child_sa_t *child_sa;
2130 host_t *vip;
2131
2132 charon->bus->set_sa(charon->bus, &this->public);
2133
2134 set_state(this, IKE_DESTROYING);
2135 DESTROY_IF(this->task_manager);
2136
2137 /* remove attributes first, as we pass the IKE_SA to the handler */
2138 while (array_remove(this->attributes, ARRAY_TAIL, &entry))
2139 {
2140 hydra->attributes->release(hydra->attributes, entry.handler,
2141 this->other_id, entry.type, entry.data);
2142 free(entry.data.ptr);
2143 }
2144 /* uninstall CHILD_SAs before virtual IPs, otherwise we might kill
2145 * routes that the CHILD_SA tries to uninstall. */
2146 while (array_remove(this->child_sas, ARRAY_TAIL, &child_sa))
2147 {
2148 child_sa->destroy(child_sa);
2149 }
2150 while (array_remove(this->my_vips, ARRAY_TAIL, &vip))
2151 {
2152 hydra->kernel_interface->del_ip(hydra->kernel_interface, vip, -1, TRUE);
2153 vip->destroy(vip);
2154 }
2155 if (array_count(this->other_vips))
2156 {
2157 charon->bus->assign_vips(charon->bus, &this->public, FALSE);
2158 }
2159 while (array_remove(this->other_vips, ARRAY_TAIL, &vip))
2160 {
2161 if (this->peer_cfg)
2162 {
2163 linked_list_t *pools;
2164 identification_t *id;
2165
2166 id = get_other_eap_id(this);
2167 pools = linked_list_create_from_enumerator(
2168 this->peer_cfg->create_pool_enumerator(this->peer_cfg));
2169 hydra->attributes->release_address(hydra->attributes, pools, vip, id);
2170 pools->destroy(pools);
2171 }
2172 vip->destroy(vip);
2173 }
2174
2175 /* unset SA after here to avoid usage by the listeners */
2176 charon->bus->set_sa(charon->bus, NULL);
2177
2178 array_destroy(this->child_sas);
2179 DESTROY_IF(this->keymat);
2180 array_destroy(this->attributes);
2181 array_destroy(this->my_vips);
2182 array_destroy(this->other_vips);
2183 array_destroy_offset(this->peer_addresses, offsetof(host_t, destroy));
2184 #ifdef ME
2185 if (this->is_mediation_server)
2186 {
2187 charon->mediation_manager->remove(charon->mediation_manager,
2188 this->ike_sa_id);
2189 }
2190 DESTROY_IF(this->server_reflexive_host);
2191 chunk_free(&this->connect_id);
2192 #endif /* ME */
2193 free(this->nat_detection_dest.ptr);
2194
2195 DESTROY_IF(this->my_host);
2196 DESTROY_IF(this->other_host);
2197 DESTROY_IF(this->my_id);
2198 DESTROY_IF(this->other_id);
2199 DESTROY_IF(this->local_host);
2200 DESTROY_IF(this->remote_host);
2201
2202 DESTROY_IF(this->ike_cfg);
2203 DESTROY_IF(this->peer_cfg);
2204 DESTROY_IF(this->proposal);
2205 this->my_auth->destroy(this->my_auth);
2206 this->other_auth->destroy(this->other_auth);
2207 array_destroy_offset(this->my_auths, offsetof(auth_cfg_t, destroy));
2208 array_destroy_offset(this->other_auths, offsetof(auth_cfg_t, destroy));
2209
2210 this->ike_sa_id->destroy(this->ike_sa_id);
2211 free(this);
2212 }
2213
2214 /*
2215 * Described in header.
2216 */
2217 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
2218 ike_version_t version)
2219 {
2220 private_ike_sa_t *this;
2221 static refcount_t unique_id = 0;
2222
2223 if (version == IKE_ANY)
2224 { /* prefer IKEv2 if protocol not specified */
2225 #ifdef USE_IKEV2
2226 version = IKEV2;
2227 #else
2228 version = IKEV1;
2229 #endif
2230 }
2231
2232 INIT(this,
2233 .public = {
2234 .get_version = _get_version,
2235 .get_state = _get_state,
2236 .set_state = _set_state,
2237 .get_name = _get_name,
2238 .get_statistic = _get_statistic,
2239 .set_statistic = _set_statistic,
2240 .process_message = _process_message,
2241 .initiate = _initiate,
2242 .retry_initiate = _retry_initiate,
2243 .get_ike_cfg = _get_ike_cfg,
2244 .set_ike_cfg = _set_ike_cfg,
2245 .get_peer_cfg = _get_peer_cfg,
2246 .set_peer_cfg = _set_peer_cfg,
2247 .get_auth_cfg = _get_auth_cfg,
2248 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2249 .add_auth_cfg = _add_auth_cfg,
2250 .get_proposal = _get_proposal,
2251 .set_proposal = _set_proposal,
2252 .get_id = _get_id,
2253 .get_my_host = _get_my_host,
2254 .set_my_host = _set_my_host,
2255 .get_other_host = _get_other_host,
2256 .set_other_host = _set_other_host,
2257 .set_message_id = _set_message_id,
2258 .float_ports = _float_ports,
2259 .update_hosts = _update_hosts,
2260 .get_my_id = _get_my_id,
2261 .set_my_id = _set_my_id,
2262 .get_other_id = _get_other_id,
2263 .set_other_id = _set_other_id,
2264 .get_other_eap_id = _get_other_eap_id,
2265 .enable_extension = _enable_extension,
2266 .supports_extension = _supports_extension,
2267 .set_condition = _set_condition,
2268 .has_condition = _has_condition,
2269 .set_pending_updates = _set_pending_updates,
2270 .get_pending_updates = _get_pending_updates,
2271 .create_peer_address_enumerator = _create_peer_address_enumerator,
2272 .add_peer_address = _add_peer_address,
2273 .clear_peer_addresses = _clear_peer_addresses,
2274 .has_mapping_changed = _has_mapping_changed,
2275 .retransmit = _retransmit,
2276 .delete = _delete_,
2277 .destroy = _destroy,
2278 .send_dpd = _send_dpd,
2279 .send_keepalive = _send_keepalive,
2280 .get_keymat = _get_keymat,
2281 .add_child_sa = _add_child_sa,
2282 .get_child_sa = _get_child_sa,
2283 .get_child_count = _get_child_count,
2284 .create_child_sa_enumerator = _create_child_sa_enumerator,
2285 .remove_child_sa = _remove_child_sa,
2286 .rekey_child_sa = _rekey_child_sa,
2287 .delete_child_sa = _delete_child_sa,
2288 .destroy_child_sa = _destroy_child_sa,
2289 .rekey = _rekey,
2290 .reauth = _reauth,
2291 .reestablish = _reestablish,
2292 .set_auth_lifetime = _set_auth_lifetime,
2293 .roam = _roam,
2294 .inherit = _inherit,
2295 .generate_message = _generate_message,
2296 .reset = _reset,
2297 .get_unique_id = _get_unique_id,
2298 .add_virtual_ip = _add_virtual_ip,
2299 .clear_virtual_ips = _clear_virtual_ips,
2300 .create_virtual_ip_enumerator = _create_virtual_ip_enumerator,
2301 .add_configuration_attribute = _add_configuration_attribute,
2302 .set_kmaddress = _set_kmaddress,
2303 .create_task_enumerator = _create_task_enumerator,
2304 .flush_queue = _flush_queue,
2305 .queue_task = _queue_task,
2306 #ifdef ME
2307 .act_as_mediation_server = _act_as_mediation_server,
2308 .get_server_reflexive_host = _get_server_reflexive_host,
2309 .set_server_reflexive_host = _set_server_reflexive_host,
2310 .get_connect_id = _get_connect_id,
2311 .initiate_mediation = _initiate_mediation,
2312 .initiate_mediated = _initiate_mediated,
2313 .relay = _relay,
2314 .callback = _callback,
2315 .respond = _respond,
2316 #endif /* ME */
2317 },
2318 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2319 .version = version,
2320 .my_host = host_create_any(AF_INET),
2321 .other_host = host_create_any(AF_INET),
2322 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2323 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2324 .keymat = keymat_create(version, initiator),
2325 .state = IKE_CREATED,
2326 .stats[STAT_INBOUND] = time_monotonic(NULL),
2327 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2328 .my_auth = auth_cfg_create(),
2329 .other_auth = auth_cfg_create(),
2330 .my_auths = array_create(0, 0),
2331 .other_auths = array_create(0, 0),
2332 .attributes = array_create(sizeof(attribute_entry_t), 0),
2333 .unique_id = ref_get(&unique_id),
2334 .keepalive_interval = lib->settings->get_time(lib->settings,
2335 "%s.keep_alive", KEEPALIVE_INTERVAL, charon->name),
2336 .retry_initiate_interval = lib->settings->get_time(lib->settings,
2337 "%s.retry_initiate_interval", 0, charon->name),
2338 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2339 "%s.flush_auth_cfg", FALSE, charon->name),
2340 );
2341
2342 if (version == IKEV2)
2343 { /* always supported with IKEv2 */
2344 enable_extension(this, EXT_DPD);
2345 }
2346
2347 this->task_manager = task_manager_create(&this->public);
2348 this->my_host->set_port(this->my_host,
2349 charon->socket->get_port(charon->socket, FALSE));
2350
2351 if (!this->task_manager || !this->keymat)
2352 {
2353 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2354 destroy(this);
2355 return NULL;
2356 }
2357 return &this->public;
2358 }