Support multiple virtual IPs on peer_cfg and ike_sa classes
[strongswan.git] / src / libcharon / sa / ike_sa.c
1 /*
2 * Copyright (C) 2006-2012 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 <utils/linked_list.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 (as auth_cfg_t)
99 */
100 auth_cfg_t *my_auth;
101
102 /**
103 * list of completed local authentication rounds
104 */
105 linked_list_t *my_auths;
106
107 /**
108 * list of completed remote authentication rounds
109 */
110 linked_list_t *other_auths;
111
112 /**
113 * currently used authentication constraints, remote (as auth_cfg_t)
114 */
115 auth_cfg_t *other_auth;
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 * Linked List containing the child sa's of the current IKE_SA.
176 */
177 linked_list_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 linked_list_t *my_vips;
188
189 /**
190 * Virtual IPs on remote host
191 */
192 linked_list_t *other_vips;
193
194 /**
195 * List of configuration attributes (attribute_entry_t)
196 */
197 linked_list_t *attributes;
198
199 /**
200 * list of peer's addresses, additional ones transmitted via MOBIKE
201 */
202 linked_list_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 * TRUE if we are currently reauthenticating this IKE_SA
257 */
258 bool is_reauthenticating;
259 };
260
261 /**
262 * Entry to maintain install configuration attributes during IKE_SA lifetime
263 */
264 struct attribute_entry_t {
265 /** handler used to install this attribute */
266 attribute_handler_t *handler;
267 /** attribute type */
268 configuration_attribute_type_t type;
269 /** attribute data */
270 chunk_t data;
271 };
272
273 /**
274 * get the time of the latest traffic processed by the kernel
275 */
276 static time_t get_use_time(private_ike_sa_t* this, bool inbound)
277 {
278 enumerator_t *enumerator;
279 child_sa_t *child_sa;
280 time_t use_time, current;
281
282 if (inbound)
283 {
284 use_time = this->stats[STAT_INBOUND];
285 }
286 else
287 {
288 use_time = this->stats[STAT_OUTBOUND];
289 }
290 enumerator = this->child_sas->create_enumerator(this->child_sas);
291 while (enumerator->enumerate(enumerator, &child_sa))
292 {
293 child_sa->get_usestats(child_sa, inbound, &current, NULL);
294 use_time = max(use_time, current);
295 }
296 enumerator->destroy(enumerator);
297
298 return use_time;
299 }
300
301 METHOD(ike_sa_t, get_unique_id, u_int32_t,
302 private_ike_sa_t *this)
303 {
304 return this->unique_id;
305 }
306
307 METHOD(ike_sa_t, get_name, char*,
308 private_ike_sa_t *this)
309 {
310 if (this->peer_cfg)
311 {
312 return this->peer_cfg->get_name(this->peer_cfg);
313 }
314 return "(unnamed)";
315 }
316
317 METHOD(ike_sa_t, get_statistic, u_int32_t,
318 private_ike_sa_t *this, statistic_t kind)
319 {
320 if (kind < STAT_MAX)
321 {
322 return this->stats[kind];
323 }
324 return 0;
325 }
326
327 METHOD(ike_sa_t, set_statistic, void,
328 private_ike_sa_t *this, statistic_t kind, u_int32_t value)
329 {
330 if (kind < STAT_MAX)
331 {
332 this->stats[kind] = value;
333 }
334 }
335
336 METHOD(ike_sa_t, get_my_host, host_t*,
337 private_ike_sa_t *this)
338 {
339 return this->my_host;
340 }
341
342 METHOD(ike_sa_t, set_my_host, void,
343 private_ike_sa_t *this, host_t *me)
344 {
345 DESTROY_IF(this->my_host);
346 this->my_host = me;
347 }
348
349 METHOD(ike_sa_t, get_other_host, host_t*,
350 private_ike_sa_t *this)
351 {
352 return this->other_host;
353 }
354
355 METHOD(ike_sa_t, set_other_host, void,
356 private_ike_sa_t *this, host_t *other)
357 {
358 DESTROY_IF(this->other_host);
359 this->other_host = other;
360 }
361
362 METHOD(ike_sa_t, get_peer_cfg, peer_cfg_t*,
363 private_ike_sa_t *this)
364 {
365 return this->peer_cfg;
366 }
367
368 METHOD(ike_sa_t, set_peer_cfg, void,
369 private_ike_sa_t *this, peer_cfg_t *peer_cfg)
370 {
371 peer_cfg->get_ref(peer_cfg);
372 DESTROY_IF(this->peer_cfg);
373 this->peer_cfg = peer_cfg;
374
375 if (this->ike_cfg == NULL)
376 {
377 this->ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
378 this->ike_cfg->get_ref(this->ike_cfg);
379 }
380 }
381
382 METHOD(ike_sa_t, get_auth_cfg, auth_cfg_t*,
383 private_ike_sa_t *this, bool local)
384 {
385 if (local)
386 {
387 return this->my_auth;
388 }
389 return this->other_auth;
390 }
391
392 METHOD(ike_sa_t, add_auth_cfg, void,
393 private_ike_sa_t *this, bool local, auth_cfg_t *cfg)
394 {
395 if (local)
396 {
397 this->my_auths->insert_last(this->my_auths, cfg);
398 }
399 else
400 {
401 this->other_auths->insert_last(this->other_auths, cfg);
402 }
403 }
404
405 METHOD(ike_sa_t, create_auth_cfg_enumerator, enumerator_t*,
406 private_ike_sa_t *this, bool local)
407 {
408 if (local)
409 {
410 return this->my_auths->create_enumerator(this->my_auths);
411 }
412 return this->other_auths->create_enumerator(this->other_auths);
413 }
414
415 /**
416 * Flush the stored authentication round information
417 */
418 static void flush_auth_cfgs(private_ike_sa_t *this)
419 {
420 auth_cfg_t *cfg;
421
422 this->my_auth->purge(this->my_auth, FALSE);
423 this->other_auth->purge(this->other_auth, FALSE);
424
425 while (this->my_auths->remove_last(this->my_auths,
426 (void**)&cfg) == SUCCESS)
427 {
428 cfg->destroy(cfg);
429 }
430 while (this->other_auths->remove_last(this->other_auths,
431 (void**)&cfg) == SUCCESS)
432 {
433 cfg->destroy(cfg);
434 }
435 }
436
437 METHOD(ike_sa_t, get_proposal, proposal_t*,
438 private_ike_sa_t *this)
439 {
440 return this->proposal;
441 }
442
443 METHOD(ike_sa_t, set_proposal, void,
444 private_ike_sa_t *this, proposal_t *proposal)
445 {
446 DESTROY_IF(this->proposal);
447 this->proposal = proposal->clone(proposal);
448 }
449
450 METHOD(ike_sa_t, set_message_id, void,
451 private_ike_sa_t *this, bool initiate, u_int32_t mid)
452 {
453 if (initiate)
454 {
455 this->task_manager->reset(this->task_manager, mid, UINT_MAX);
456 }
457 else
458 {
459 this->task_manager->reset(this->task_manager, UINT_MAX, mid);
460 }
461 }
462
463 METHOD(ike_sa_t, send_keepalive, void,
464 private_ike_sa_t *this)
465 {
466 send_keepalive_job_t *job;
467 time_t last_out, now, diff;
468
469 if (!(this->conditions & COND_NAT_HERE) || this->keepalive_interval == 0)
470 { /* disable keep alives if we are not NATed anymore */
471 return;
472 }
473
474 last_out = get_use_time(this, FALSE);
475 now = time_monotonic(NULL);
476
477 diff = now - last_out;
478
479 if (diff >= this->keepalive_interval)
480 {
481 packet_t *packet;
482 chunk_t data;
483
484 packet = packet_create();
485 packet->set_source(packet, this->my_host->clone(this->my_host));
486 packet->set_destination(packet, this->other_host->clone(this->other_host));
487 data.ptr = malloc(1);
488 data.ptr[0] = 0xFF;
489 data.len = 1;
490 packet->set_data(packet, data);
491 DBG1(DBG_IKE, "sending keep alive to %#H", this->other_host);
492 charon->sender->send_no_marker(charon->sender, packet);
493 diff = 0;
494 }
495 job = send_keepalive_job_create(this->ike_sa_id);
496 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
497 this->keepalive_interval - diff);
498 }
499
500 METHOD(ike_sa_t, get_ike_cfg, ike_cfg_t*,
501 private_ike_sa_t *this)
502 {
503 return this->ike_cfg;
504 }
505
506 METHOD(ike_sa_t, set_ike_cfg, void,
507 private_ike_sa_t *this, ike_cfg_t *ike_cfg)
508 {
509 ike_cfg->get_ref(ike_cfg);
510 this->ike_cfg = ike_cfg;
511 }
512
513 METHOD(ike_sa_t, enable_extension, void,
514 private_ike_sa_t *this, ike_extension_t extension)
515 {
516 this->extensions |= extension;
517 }
518
519 METHOD(ike_sa_t, supports_extension, bool,
520 private_ike_sa_t *this, ike_extension_t extension)
521 {
522 return (this->extensions & extension) != FALSE;
523 }
524
525 METHOD(ike_sa_t, has_condition, bool,
526 private_ike_sa_t *this, ike_condition_t condition)
527 {
528 return (this->conditions & condition) != FALSE;
529 }
530
531 METHOD(ike_sa_t, set_condition, void,
532 private_ike_sa_t *this, ike_condition_t condition, bool enable)
533 {
534 if (has_condition(this, condition) != enable)
535 {
536 if (enable)
537 {
538 this->conditions |= condition;
539 switch (condition)
540 {
541 case COND_NAT_HERE:
542 DBG1(DBG_IKE, "local host is behind NAT, sending keep alives");
543 this->conditions |= COND_NAT_ANY;
544 send_keepalive(this);
545 break;
546 case COND_NAT_THERE:
547 DBG1(DBG_IKE, "remote host is behind NAT");
548 this->conditions |= COND_NAT_ANY;
549 break;
550 case COND_NAT_FAKE:
551 DBG1(DBG_IKE, "faking NAT situation to enforce UDP encapsulation");
552 this->conditions |= COND_NAT_ANY;
553 break;
554 default:
555 break;
556 }
557 }
558 else
559 {
560 this->conditions &= ~condition;
561 switch (condition)
562 {
563 case COND_NAT_HERE:
564 case COND_NAT_FAKE:
565 case COND_NAT_THERE:
566 set_condition(this, COND_NAT_ANY,
567 has_condition(this, COND_NAT_HERE) ||
568 has_condition(this, COND_NAT_THERE) ||
569 has_condition(this, COND_NAT_FAKE));
570 break;
571 default:
572 break;
573 }
574 }
575 }
576 }
577
578 METHOD(ike_sa_t, send_dpd, status_t,
579 private_ike_sa_t *this)
580 {
581 job_t *job;
582 time_t diff, delay;
583
584 if (this->state == IKE_PASSIVE)
585 {
586 return INVALID_STATE;
587 }
588 delay = this->peer_cfg->get_dpd(this->peer_cfg);
589 if (this->task_manager->busy(this->task_manager))
590 {
591 /* an exchange is in the air, no need to start a DPD check */
592 diff = 0;
593 }
594 else
595 {
596 /* check if there was any inbound traffic */
597 time_t last_in, now;
598 last_in = get_use_time(this, TRUE);
599 now = time_monotonic(NULL);
600 diff = now - last_in;
601 if (!delay || diff >= delay)
602 {
603 /* to long ago, initiate dead peer detection */
604 DBG1(DBG_IKE, "sending DPD request");
605 this->task_manager->queue_dpd(this->task_manager);
606 diff = 0;
607 }
608 }
609 /* recheck in "interval" seconds */
610 if (delay)
611 {
612 job = (job_t*)send_dpd_job_create(this->ike_sa_id);
613 lib->scheduler->schedule_job(lib->scheduler, job, delay - diff);
614 }
615 return this->task_manager->initiate(this->task_manager);
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 DBG1(DBG_IKE, "installing new virtual IP %H", ip);
744 if (hydra->kernel_interface->add_ip(hydra->kernel_interface, ip,
745 this->my_host) == SUCCESS)
746 {
747 this->my_vips->insert_last(this->my_vips, ip->clone(ip));
748 }
749 else
750 {
751 DBG1(DBG_IKE, "installing virtual IP %H failed", ip);
752 }
753 }
754 else
755 {
756 this->other_vips->insert_last(this->other_vips, ip->clone(ip));
757 }
758 }
759
760 METHOD(ike_sa_t, create_virtual_ip_enumerator, enumerator_t*,
761 private_ike_sa_t *this, bool local)
762 {
763 if (local)
764 {
765 return this->my_vips->create_enumerator(this->my_vips);
766 }
767 return this->other_vips->create_enumerator(this->other_vips);
768 }
769
770 METHOD(ike_sa_t, add_peer_address, void,
771 private_ike_sa_t *this, host_t *host)
772 {
773 this->peer_addresses->insert_last(this->peer_addresses, host);
774 }
775
776 METHOD(ike_sa_t, create_peer_address_enumerator, enumerator_t*,
777 private_ike_sa_t *this)
778 {
779 if (this->peer_addresses->get_count(this->peer_addresses))
780 {
781 return this->peer_addresses->create_enumerator(this->peer_addresses);
782 }
783 /* in case we don't have MOBIKE */
784 return enumerator_create_single(this->other_host, NULL);
785 }
786
787 METHOD(ike_sa_t, clear_peer_addresses, void,
788 private_ike_sa_t *this)
789 {
790 enumerator_t *enumerator;
791 host_t *host;
792
793 enumerator = this->peer_addresses->create_enumerator(this->peer_addresses);
794 while (enumerator->enumerate(enumerator, (void**)&host))
795 {
796 this->peer_addresses->remove_at(this->peer_addresses,
797 enumerator);
798 host->destroy(host);
799 }
800 enumerator->destroy(enumerator);
801 }
802
803 METHOD(ike_sa_t, has_mapping_changed, bool,
804 private_ike_sa_t *this, chunk_t hash)
805 {
806 if (this->nat_detection_dest.ptr == NULL)
807 {
808 this->nat_detection_dest = chunk_clone(hash);
809 return FALSE;
810 }
811 if (chunk_equals(hash, this->nat_detection_dest))
812 {
813 return FALSE;
814 }
815 free(this->nat_detection_dest.ptr);
816 this->nat_detection_dest = chunk_clone(hash);
817 return TRUE;
818 }
819
820 METHOD(ike_sa_t, set_pending_updates, void,
821 private_ike_sa_t *this, u_int32_t updates)
822 {
823 this->pending_updates = updates;
824 }
825
826 METHOD(ike_sa_t, get_pending_updates, u_int32_t,
827 private_ike_sa_t *this)
828 {
829 return this->pending_updates;
830 }
831
832 METHOD(ike_sa_t, float_ports, void,
833 private_ike_sa_t *this)
834 {
835 /* do not switch if we have a custom port from MOBIKE/NAT */
836 if (this->my_host->get_port(this->my_host) ==
837 charon->socket->get_port(charon->socket, FALSE))
838 {
839 this->my_host->set_port(this->my_host,
840 charon->socket->get_port(charon->socket, TRUE));
841 }
842 if (this->other_host->get_port(this->other_host) == IKEV2_UDP_PORT)
843 {
844 this->other_host->set_port(this->other_host, IKEV2_NATT_PORT);
845 }
846 }
847
848 METHOD(ike_sa_t, update_hosts, void,
849 private_ike_sa_t *this, host_t *me, host_t *other, bool force)
850 {
851 bool update = FALSE;
852
853 if (me == NULL)
854 {
855 me = this->my_host;
856 }
857 if (other == NULL)
858 {
859 other = this->other_host;
860 }
861
862 /* apply hosts on first received message */
863 if (this->my_host->is_anyaddr(this->my_host) ||
864 this->other_host->is_anyaddr(this->other_host))
865 {
866 set_my_host(this, me->clone(me));
867 set_other_host(this, other->clone(other));
868 update = TRUE;
869 }
870 else
871 {
872 /* update our address in any case */
873 if (!me->equals(me, this->my_host))
874 {
875 set_my_host(this, me->clone(me));
876 update = TRUE;
877 }
878
879 if (!other->equals(other, this->other_host))
880 {
881 /* update others address if we are NOT NATed */
882 if (force || !has_condition(this, COND_NAT_HERE))
883 {
884 set_other_host(this, other->clone(other));
885 update = TRUE;
886 }
887 }
888 }
889
890 /* update all associated CHILD_SAs, if required */
891 if (update)
892 {
893 enumerator_t *enumerator;
894 child_sa_t *child_sa;
895
896 enumerator = this->child_sas->create_enumerator(this->child_sas);
897 while (enumerator->enumerate(enumerator, (void**)&child_sa))
898 {
899 if (child_sa->update(child_sa, this->my_host,
900 this->other_host, this->my_vips,
901 has_condition(this, COND_NAT_ANY)) == NOT_SUPPORTED)
902 {
903 this->public.rekey_child_sa(&this->public,
904 child_sa->get_protocol(child_sa),
905 child_sa->get_spi(child_sa, TRUE));
906 }
907 }
908 enumerator->destroy(enumerator);
909 }
910 }
911
912 METHOD(ike_sa_t, generate_message, status_t,
913 private_ike_sa_t *this, message_t *message, packet_t **packet)
914 {
915 status_t status;
916
917 if (message->is_encoded(message))
918 { /* already done */
919 *packet = message->get_packet(message);
920 return SUCCESS;
921 }
922 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
923 message->set_ike_sa_id(message, this->ike_sa_id);
924 charon->bus->message(charon->bus, message, FALSE, TRUE);
925 status = message->generate(message, this->keymat, packet);
926 if (status == SUCCESS)
927 {
928 charon->bus->message(charon->bus, message, FALSE, FALSE);
929 }
930 return status;
931 }
932
933 METHOD(ike_sa_t, set_kmaddress, void,
934 private_ike_sa_t *this, host_t *local, host_t *remote)
935 {
936 DESTROY_IF(this->local_host);
937 DESTROY_IF(this->remote_host);
938 this->local_host = local->clone(local);
939 this->remote_host = remote->clone(remote);
940 }
941
942 #ifdef ME
943 METHOD(ike_sa_t, act_as_mediation_server, void,
944 private_ike_sa_t *this)
945 {
946 charon->mediation_manager->update_sa_id(charon->mediation_manager,
947 this->other_id, this->ike_sa_id);
948 this->is_mediation_server = TRUE;
949 }
950
951 METHOD(ike_sa_t, get_server_reflexive_host, host_t*,
952 private_ike_sa_t *this)
953 {
954 return this->server_reflexive_host;
955 }
956
957 METHOD(ike_sa_t, set_server_reflexive_host, void,
958 private_ike_sa_t *this, host_t *host)
959 {
960 DESTROY_IF(this->server_reflexive_host);
961 this->server_reflexive_host = host;
962 }
963
964 METHOD(ike_sa_t, get_connect_id, chunk_t,
965 private_ike_sa_t *this)
966 {
967 return this->connect_id;
968 }
969
970 METHOD(ike_sa_t, respond, status_t,
971 private_ike_sa_t *this, identification_t *peer_id, chunk_t connect_id)
972 {
973 ike_me_t *task = ike_me_create(&this->public, TRUE);
974 task->respond(task, peer_id, connect_id);
975 this->task_manager->queue_task(this->task_manager, (task_t*)task);
976 return this->task_manager->initiate(this->task_manager);
977 }
978
979 METHOD(ike_sa_t, callback, status_t,
980 private_ike_sa_t *this, identification_t *peer_id)
981 {
982 ike_me_t *task = ike_me_create(&this->public, TRUE);
983 task->callback(task, peer_id);
984 this->task_manager->queue_task(this->task_manager, (task_t*)task);
985 return this->task_manager->initiate(this->task_manager);
986 }
987
988 METHOD(ike_sa_t, relay, status_t,
989 private_ike_sa_t *this, identification_t *requester, chunk_t connect_id,
990 chunk_t connect_key, linked_list_t *endpoints, bool response)
991 {
992 ike_me_t *task = ike_me_create(&this->public, TRUE);
993 task->relay(task, requester, connect_id, connect_key, endpoints, response);
994 this->task_manager->queue_task(this->task_manager, (task_t*)task);
995 return this->task_manager->initiate(this->task_manager);
996 }
997
998 METHOD(ike_sa_t, initiate_mediation, status_t,
999 private_ike_sa_t *this, peer_cfg_t *mediated_cfg)
1000 {
1001 ike_me_t *task = ike_me_create(&this->public, TRUE);
1002 task->connect(task, mediated_cfg->get_peer_id(mediated_cfg));
1003 this->task_manager->queue_task(this->task_manager, (task_t*)task);
1004 return this->task_manager->initiate(this->task_manager);
1005 }
1006
1007 METHOD(ike_sa_t, initiate_mediated, status_t,
1008 private_ike_sa_t *this, host_t *me, host_t *other, chunk_t connect_id)
1009 {
1010 set_my_host(this, me->clone(me));
1011 set_other_host(this, other->clone(other));
1012 chunk_free(&this->connect_id);
1013 this->connect_id = chunk_clone(connect_id);
1014 return this->task_manager->initiate(this->task_manager);
1015 }
1016 #endif /* ME */
1017
1018 /**
1019 * Resolve DNS host in configuration
1020 */
1021 static void resolve_hosts(private_ike_sa_t *this)
1022 {
1023 host_t *host;
1024
1025 if (this->remote_host)
1026 {
1027 host = this->remote_host->clone(this->remote_host);
1028 host->set_port(host, IKEV2_UDP_PORT);
1029 }
1030 else
1031 {
1032 char *other_addr;
1033 u_int16_t other_port;
1034
1035 other_addr = this->ike_cfg->get_other_addr(this->ike_cfg, NULL);
1036 other_port = this->ike_cfg->get_other_port(this->ike_cfg);
1037 host = host_create_from_dns(other_addr, 0, other_port);
1038 }
1039 if (host)
1040 {
1041 set_other_host(this, host);
1042 }
1043
1044 if (this->local_host)
1045 {
1046 host = this->local_host->clone(this->local_host);
1047 host->set_port(host, charon->socket->get_port(charon->socket, FALSE));
1048 }
1049 else
1050 {
1051 char *my_addr;
1052 u_int16_t my_port;
1053 int family = 0;
1054
1055 /* use same address family as for other */
1056 if (!this->other_host->is_anyaddr(this->other_host))
1057 {
1058 family = this->other_host->get_family(this->other_host);
1059 }
1060 my_addr = this->ike_cfg->get_my_addr(this->ike_cfg, NULL);
1061 my_port = this->ike_cfg->get_my_port(this->ike_cfg);
1062 host = host_create_from_dns(my_addr, family, my_port);
1063
1064 if (host && host->is_anyaddr(host) &&
1065 !this->other_host->is_anyaddr(this->other_host))
1066 {
1067 host->destroy(host);
1068 host = hydra->kernel_interface->get_source_addr(
1069 hydra->kernel_interface, this->other_host, NULL);
1070 if (host)
1071 {
1072 host->set_port(host, this->ike_cfg->get_my_port(this->ike_cfg));
1073 }
1074 else
1075 { /* fallback to address family specific %any(6), if configured */
1076 host = host_create_from_dns(my_addr, 0, my_port);
1077 }
1078 }
1079 }
1080 if (host)
1081 {
1082 set_my_host(this, host);
1083 }
1084 }
1085
1086 METHOD(ike_sa_t, initiate, status_t,
1087 private_ike_sa_t *this, child_cfg_t *child_cfg, u_int32_t reqid,
1088 traffic_selector_t *tsi, traffic_selector_t *tsr)
1089 {
1090 bool defer_initiate = FALSE;
1091
1092 if (this->state == IKE_CREATED)
1093 {
1094 if (this->my_host->is_anyaddr(this->my_host) ||
1095 this->other_host->is_anyaddr(this->other_host))
1096 {
1097 resolve_hosts(this);
1098 }
1099
1100 if (this->other_host->is_anyaddr(this->other_host)
1101 #ifdef ME
1102 && !this->peer_cfg->get_mediated_by(this->peer_cfg)
1103 #endif /* ME */
1104 )
1105 {
1106 char *addr = this->ike_cfg->get_other_addr(this->ike_cfg, NULL);
1107 bool is_anyaddr = streq(addr, "%any") || streq(addr, "%any6");
1108
1109 if (is_anyaddr || !this->retry_initiate_interval)
1110 {
1111 if (is_anyaddr)
1112 {
1113 DBG1(DBG_IKE, "unable to initiate to %s", addr);
1114 }
1115 else
1116 {
1117 DBG1(DBG_IKE, "unable to resolve %s, initiate aborted",
1118 addr);
1119 }
1120 DESTROY_IF(child_cfg);
1121 charon->bus->alert(charon->bus, ALERT_PEER_ADDR_FAILED);
1122 return DESTROY_ME;
1123 }
1124 DBG1(DBG_IKE, "unable to resolve %s, retrying in %ds",
1125 addr, this->retry_initiate_interval);
1126 defer_initiate = TRUE;
1127 }
1128
1129 set_condition(this, COND_ORIGINAL_INITIATOR, TRUE);
1130 this->task_manager->queue_ike(this->task_manager);
1131 }
1132
1133 #ifdef ME
1134 if (this->peer_cfg->is_mediation(this->peer_cfg))
1135 {
1136 if (this->state == IKE_ESTABLISHED)
1137 {
1138 /* mediation connection is already established, retrigger state
1139 * change to notify bus listeners */
1140 DBG1(DBG_IKE, "mediation connection is already up");
1141 set_state(this, IKE_ESTABLISHED);
1142 }
1143 DESTROY_IF(child_cfg);
1144 }
1145 else
1146 #endif /* ME */
1147 if (child_cfg)
1148 {
1149 /* normal IKE_SA with CHILD_SA */
1150 this->task_manager->queue_child(this->task_manager, child_cfg, reqid,
1151 tsi, tsr);
1152 #ifdef ME
1153 if (this->peer_cfg->get_mediated_by(this->peer_cfg))
1154 {
1155 /* mediated connection, initiate mediation process */
1156 job_t *job = (job_t*)initiate_mediation_job_create(this->ike_sa_id);
1157 lib->processor->queue_job(lib->processor, job);
1158 return SUCCESS;
1159 }
1160 #endif /* ME */
1161 }
1162
1163 if (defer_initiate)
1164 {
1165 if (!this->retry_initiate_queued)
1166 {
1167 job_t *job = (job_t*)retry_initiate_job_create(this->ike_sa_id);
1168 lib->scheduler->schedule_job(lib->scheduler, (job_t*)job,
1169 this->retry_initiate_interval);
1170 this->retry_initiate_queued = TRUE;
1171 }
1172 return SUCCESS;
1173 }
1174 this->retry_initiate_queued = FALSE;
1175 return this->task_manager->initiate(this->task_manager);
1176 }
1177
1178 METHOD(ike_sa_t, retry_initiate, status_t,
1179 private_ike_sa_t *this)
1180 {
1181 if (this->retry_initiate_queued)
1182 {
1183 this->retry_initiate_queued = FALSE;
1184 return initiate(this, NULL, 0, NULL, NULL);
1185 }
1186 return SUCCESS;
1187 }
1188
1189 METHOD(ike_sa_t, process_message, status_t,
1190 private_ike_sa_t *this, message_t *message)
1191 {
1192 status_t status;
1193
1194 if (this->state == IKE_PASSIVE)
1195 { /* do not handle messages in passive state */
1196 return FAILED;
1197 }
1198 switch (message->get_exchange_type(message))
1199 {
1200 case ID_PROT:
1201 case AGGRESSIVE:
1202 case IKE_SA_INIT:
1203 case IKE_AUTH:
1204 if (this->state != IKE_CREATED &&
1205 this->state != IKE_CONNECTING)
1206 {
1207 DBG1(DBG_IKE, "ignoring %N in established IKE_SA state",
1208 exchange_type_names, message->get_exchange_type(message));
1209 return FAILED;
1210 }
1211 break;
1212 default:
1213 break;
1214 }
1215 if (message->get_major_version(message) != this->version)
1216 {
1217 DBG1(DBG_IKE, "ignoring %N IKEv%u exchange on %N SA",
1218 exchange_type_names, message->get_exchange_type(message),
1219 message->get_major_version(message),
1220 ike_version_names, this->version);
1221 /* TODO-IKEv1: fall back to IKEv1 if we receive an IKEv1
1222 * INVALID_MAJOR_VERSION on an IKEv2 SA. */
1223 return FAILED;
1224 }
1225 status = this->task_manager->process_message(this->task_manager, message);
1226 if (this->flush_auth_cfg && this->state == IKE_ESTABLISHED)
1227 {
1228 /* authentication completed */
1229 this->flush_auth_cfg = FALSE;
1230 flush_auth_cfgs(this);
1231 }
1232 return status;
1233 }
1234
1235 METHOD(ike_sa_t, get_id, ike_sa_id_t*,
1236 private_ike_sa_t *this)
1237 {
1238 return this->ike_sa_id;
1239 }
1240
1241 METHOD(ike_sa_t, get_version, ike_version_t,
1242 private_ike_sa_t *this)
1243 {
1244 return this->version;
1245 }
1246
1247 METHOD(ike_sa_t, get_my_id, identification_t*,
1248 private_ike_sa_t *this)
1249 {
1250 return this->my_id;
1251 }
1252
1253 METHOD(ike_sa_t, set_my_id, void,
1254 private_ike_sa_t *this, identification_t *me)
1255 {
1256 DESTROY_IF(this->my_id);
1257 this->my_id = me;
1258 }
1259
1260 METHOD(ike_sa_t, get_other_id, identification_t*,
1261 private_ike_sa_t *this)
1262 {
1263 return this->other_id;
1264 }
1265
1266 METHOD(ike_sa_t, get_other_eap_id, identification_t*,
1267 private_ike_sa_t *this)
1268 {
1269 identification_t *id = NULL, *current;
1270 enumerator_t *enumerator;
1271 auth_cfg_t *cfg;
1272
1273 enumerator = this->other_auths->create_enumerator(this->other_auths);
1274 while (enumerator->enumerate(enumerator, &cfg))
1275 {
1276 /* prefer EAP-Identity of last round */
1277 current = cfg->get(cfg, AUTH_RULE_EAP_IDENTITY);
1278 if (!current || current->get_type(current) == ID_ANY)
1279 {
1280 current = cfg->get(cfg, AUTH_RULE_XAUTH_IDENTITY);
1281 }
1282 if (!current || current->get_type(current) == ID_ANY)
1283 {
1284 current = cfg->get(cfg, AUTH_RULE_IDENTITY);
1285 }
1286 if (current && current->get_type(current) != ID_ANY)
1287 {
1288 id = current;
1289 continue;
1290 }
1291 }
1292 enumerator->destroy(enumerator);
1293 if (id)
1294 {
1295 return id;
1296 }
1297 return this->other_id;
1298 }
1299
1300 METHOD(ike_sa_t, set_other_id, void,
1301 private_ike_sa_t *this, identification_t *other)
1302 {
1303 DESTROY_IF(this->other_id);
1304 this->other_id = other;
1305 }
1306
1307 METHOD(ike_sa_t, add_child_sa, void,
1308 private_ike_sa_t *this, child_sa_t *child_sa)
1309 {
1310 this->child_sas->insert_last(this->child_sas, child_sa);
1311 }
1312
1313 METHOD(ike_sa_t, get_child_sa, child_sa_t*,
1314 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool inbound)
1315 {
1316 enumerator_t *enumerator;
1317 child_sa_t *current, *found = NULL;
1318
1319 enumerator = this->child_sas->create_enumerator(this->child_sas);
1320 while (enumerator->enumerate(enumerator, (void**)&current))
1321 {
1322 if (current->get_spi(current, inbound) == spi &&
1323 current->get_protocol(current) == protocol)
1324 {
1325 found = current;
1326 }
1327 }
1328 enumerator->destroy(enumerator);
1329 return found;
1330 }
1331
1332 METHOD(ike_sa_t, get_child_count, int,
1333 private_ike_sa_t *this)
1334 {
1335 return this->child_sas->get_count(this->child_sas);
1336 }
1337
1338 METHOD(ike_sa_t, create_child_sa_enumerator, enumerator_t*,
1339 private_ike_sa_t *this)
1340 {
1341 return this->child_sas->create_enumerator(this->child_sas);
1342 }
1343
1344 METHOD(ike_sa_t, remove_child_sa, void,
1345 private_ike_sa_t *this, enumerator_t *enumerator)
1346 {
1347 this->child_sas->remove_at(this->child_sas, enumerator);
1348 }
1349
1350 METHOD(ike_sa_t, rekey_child_sa, status_t,
1351 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1352 {
1353 if (this->state == IKE_PASSIVE)
1354 {
1355 return INVALID_STATE;
1356 }
1357 this->task_manager->queue_child_rekey(this->task_manager, protocol, spi);
1358 return this->task_manager->initiate(this->task_manager);
1359 }
1360
1361 METHOD(ike_sa_t, delete_child_sa, status_t,
1362 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi, bool expired)
1363 {
1364 if (this->state == IKE_PASSIVE)
1365 {
1366 return INVALID_STATE;
1367 }
1368 this->task_manager->queue_child_delete(this->task_manager,
1369 protocol, spi, expired);
1370 return this->task_manager->initiate(this->task_manager);
1371 }
1372
1373 METHOD(ike_sa_t, destroy_child_sa, status_t,
1374 private_ike_sa_t *this, protocol_id_t protocol, u_int32_t spi)
1375 {
1376 enumerator_t *enumerator;
1377 child_sa_t *child_sa;
1378 status_t status = NOT_FOUND;
1379
1380 enumerator = this->child_sas->create_enumerator(this->child_sas);
1381 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1382 {
1383 if (child_sa->get_protocol(child_sa) == protocol &&
1384 child_sa->get_spi(child_sa, TRUE) == spi)
1385 {
1386 this->child_sas->remove_at(this->child_sas, enumerator);
1387 child_sa->destroy(child_sa);
1388 status = SUCCESS;
1389 break;
1390 }
1391 }
1392 enumerator->destroy(enumerator);
1393 return status;
1394 }
1395
1396 METHOD(ike_sa_t, delete_, status_t,
1397 private_ike_sa_t *this)
1398 {
1399 switch (this->state)
1400 {
1401 case IKE_REKEYING:
1402 if (this->version == IKEV1)
1403 { /* SA has been reauthenticated, delete */
1404 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1405 break;
1406 }
1407 /* FALL */
1408 case IKE_ESTABLISHED:
1409 this->task_manager->queue_ike_delete(this->task_manager);
1410 return this->task_manager->initiate(this->task_manager);
1411 case IKE_CREATED:
1412 DBG1(DBG_IKE, "deleting unestablished IKE_SA");
1413 break;
1414 case IKE_PASSIVE:
1415 break;
1416 default:
1417 DBG1(DBG_IKE, "destroying IKE_SA in state %N "
1418 "without notification", ike_sa_state_names, this->state);
1419 charon->bus->ike_updown(charon->bus, &this->public, FALSE);
1420 break;
1421 }
1422 return DESTROY_ME;
1423 }
1424
1425 METHOD(ike_sa_t, rekey, status_t,
1426 private_ike_sa_t *this)
1427 {
1428 if (this->state == IKE_PASSIVE)
1429 {
1430 return INVALID_STATE;
1431 }
1432 this->task_manager->queue_ike_rekey(this->task_manager);
1433 return this->task_manager->initiate(this->task_manager);
1434 }
1435
1436 METHOD(ike_sa_t, reauth, status_t,
1437 private_ike_sa_t *this)
1438 {
1439 if (this->state == IKE_PASSIVE)
1440 {
1441 return INVALID_STATE;
1442 }
1443 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1444 * If the peer does not support RFC4478, there is no way to keep the
1445 * IKE_SA up. */
1446 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1447 {
1448 DBG1(DBG_IKE, "initiator did not reauthenticate as requested");
1449 if (this->other_vips->get_count(this->other_vips) != 0 ||
1450 has_condition(this, COND_XAUTH_AUTHENTICATED) ||
1451 has_condition(this, COND_EAP_AUTHENTICATED)
1452 #ifdef ME
1453 /* as mediation server we too cannot reauth the IKE_SA */
1454 || this->is_mediation_server
1455 #endif /* ME */
1456 )
1457 {
1458 time_t del, now;
1459
1460 del = this->stats[STAT_DELETE];
1461 now = time_monotonic(NULL);
1462 DBG1(DBG_IKE, "IKE_SA %s[%d] will timeout in %V",
1463 get_name(this), this->unique_id, &now, &del);
1464 return FAILED;
1465 }
1466 else
1467 {
1468 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d] actively",
1469 get_name(this), this->unique_id);
1470 }
1471 }
1472 else
1473 {
1474 DBG0(DBG_IKE, "reauthenticating IKE_SA %s[%d]",
1475 get_name(this), this->unique_id);
1476 }
1477 this->is_reauthenticating = TRUE;
1478 this->task_manager->queue_ike_reauth(this->task_manager);
1479 return this->task_manager->initiate(this->task_manager);
1480 }
1481
1482 METHOD(ike_sa_t, reestablish, status_t,
1483 private_ike_sa_t *this)
1484 {
1485 ike_sa_t *new;
1486 host_t *host;
1487 action_t action;
1488 enumerator_t *enumerator;
1489 child_sa_t *child_sa;
1490 child_cfg_t *child_cfg;
1491 bool restart = FALSE;
1492 status_t status = FAILED;
1493
1494 if (this->is_reauthenticating)
1495 { /* only reauthenticate if we have children */
1496 if (this->child_sas->get_count(this->child_sas) == 0
1497 #ifdef ME
1498 /* allow reauth of mediation connections without CHILD_SAs */
1499 && !this->peer_cfg->is_mediation(this->peer_cfg)
1500 #endif /* ME */
1501 )
1502 {
1503 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA "
1504 "to recreate");
1505 }
1506 else
1507 {
1508 restart = TRUE;
1509 }
1510 }
1511 else
1512 { /* check if we have children to keep up at all */
1513 enumerator = this->child_sas->create_enumerator(this->child_sas);
1514 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1515 {
1516 if (this->state == IKE_DELETING)
1517 {
1518 action = child_sa->get_close_action(child_sa);
1519 }
1520 else
1521 {
1522 action = child_sa->get_dpd_action(child_sa);
1523 }
1524 switch (action)
1525 {
1526 case ACTION_RESTART:
1527 restart = TRUE;
1528 break;
1529 case ACTION_ROUTE:
1530 charon->traps->install(charon->traps, this->peer_cfg,
1531 child_sa->get_config(child_sa));
1532 break;
1533 default:
1534 break;
1535 }
1536 }
1537 enumerator->destroy(enumerator);
1538 #ifdef ME
1539 /* mediation connections have no children, keep them up anyway */
1540 if (this->peer_cfg->is_mediation(this->peer_cfg))
1541 {
1542 restart = TRUE;
1543 }
1544 #endif /* ME */
1545 }
1546 if (!restart)
1547 {
1548 return FAILED;
1549 }
1550
1551 /* check if we are able to reestablish this IKE_SA */
1552 if (!has_condition(this, COND_ORIGINAL_INITIATOR) &&
1553 (this->other_vips->get_count(this->other_vips) != 0 ||
1554 has_condition(this, COND_EAP_AUTHENTICATED)
1555 #ifdef ME
1556 || this->is_mediation_server
1557 #endif /* ME */
1558 ))
1559 {
1560 DBG1(DBG_IKE, "unable to reestablish IKE_SA due to asymmetric setup");
1561 return FAILED;
1562 }
1563
1564 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1565 this->version, TRUE);
1566 if (!new)
1567 {
1568 return FAILED;
1569 }
1570 new->set_peer_cfg(new, this->peer_cfg);
1571 host = this->other_host;
1572 new->set_other_host(new, host->clone(host));
1573 host = this->my_host;
1574 new->set_my_host(new, host->clone(host));
1575 /* if we already have a virtual IP, we reuse it */
1576 enumerator = this->my_vips->create_enumerator(this->my_vips);
1577 while (enumerator->enumerate(enumerator, &host))
1578 {
1579 new->add_virtual_ip(new, TRUE, host);
1580 }
1581 enumerator->destroy(enumerator);
1582
1583 #ifdef ME
1584 if (this->peer_cfg->is_mediation(this->peer_cfg))
1585 {
1586 status = new->initiate(new, NULL, 0, NULL, NULL);
1587 }
1588 else
1589 #endif /* ME */
1590 {
1591 enumerator = this->child_sas->create_enumerator(this->child_sas);
1592 while (enumerator->enumerate(enumerator, (void**)&child_sa))
1593 {
1594 if (this->is_reauthenticating)
1595 {
1596 switch (child_sa->get_state(child_sa))
1597 {
1598 case CHILD_ROUTED:
1599 { /* move routed child directly */
1600 this->child_sas->remove_at(this->child_sas, enumerator);
1601 new->add_child_sa(new, child_sa);
1602 action = ACTION_NONE;
1603 break;
1604 }
1605 default:
1606 { /* initiate/queue all other CHILD_SAs */
1607 action = ACTION_RESTART;
1608 break;
1609 }
1610 }
1611 }
1612 else
1613 { /* only restart CHILD_SAs that are configured accordingly */
1614 if (this->state == IKE_DELETING)
1615 {
1616 action = child_sa->get_close_action(child_sa);
1617 }
1618 else
1619 {
1620 action = child_sa->get_dpd_action(child_sa);
1621 }
1622 }
1623 switch (action)
1624 {
1625 case ACTION_RESTART:
1626 child_cfg = child_sa->get_config(child_sa);
1627 DBG1(DBG_IKE, "restarting CHILD_SA %s",
1628 child_cfg->get_name(child_cfg));
1629 child_cfg->get_ref(child_cfg);
1630 status = new->initiate(new, child_cfg, 0, NULL, NULL);
1631 break;
1632 default:
1633 continue;
1634 }
1635 if (status == DESTROY_ME)
1636 {
1637 break;
1638 }
1639 }
1640 enumerator->destroy(enumerator);
1641 }
1642
1643 if (status == DESTROY_ME)
1644 {
1645 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1646 status = FAILED;
1647 }
1648 else
1649 {
1650 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1651 status = SUCCESS;
1652 }
1653 charon->bus->set_sa(charon->bus, &this->public);
1654 return status;
1655 }
1656
1657 METHOD(ike_sa_t, retransmit, status_t,
1658 private_ike_sa_t *this, u_int32_t message_id)
1659 {
1660 if (this->state == IKE_PASSIVE)
1661 {
1662 return INVALID_STATE;
1663 }
1664 this->stats[STAT_OUTBOUND] = time_monotonic(NULL);
1665 if (this->task_manager->retransmit(this->task_manager, message_id) != SUCCESS)
1666 {
1667 /* send a proper signal to brief interested bus listeners */
1668 switch (this->state)
1669 {
1670 case IKE_CONNECTING:
1671 {
1672 /* retry IKE_SA_INIT/Main Mode if we have multiple keyingtries */
1673 u_int32_t tries = this->peer_cfg->get_keyingtries(this->peer_cfg);
1674 this->keyingtry++;
1675 if (tries == 0 || tries > this->keyingtry)
1676 {
1677 DBG1(DBG_IKE, "peer not responding, trying again (%d/%d)",
1678 this->keyingtry + 1, tries);
1679 reset(this);
1680 resolve_hosts(this);
1681 this->task_manager->queue_ike(this->task_manager);
1682 return this->task_manager->initiate(this->task_manager);
1683 }
1684 DBG1(DBG_IKE, "establishing IKE_SA failed, peer not responding");
1685 break;
1686 }
1687 case IKE_DELETING:
1688 DBG1(DBG_IKE, "proper IKE_SA delete failed, peer not responding");
1689 if (this->is_reauthenticating)
1690 {
1691 DBG1(DBG_IKE, "delete during reauthentication failed, "
1692 "trying to reestablish IKE_SA anyway");
1693 reestablish(this);
1694 }
1695 break;
1696 case IKE_REKEYING:
1697 DBG1(DBG_IKE, "rekeying IKE_SA failed, peer not responding");
1698 /* FALL */
1699 default:
1700 reestablish(this);
1701 break;
1702 }
1703 return DESTROY_ME;
1704 }
1705 return SUCCESS;
1706 }
1707
1708 METHOD(ike_sa_t, set_auth_lifetime, status_t,
1709 private_ike_sa_t *this, u_int32_t lifetime)
1710 {
1711 u_int32_t diff, hard, soft, now;
1712 ike_auth_lifetime_t *task;
1713 bool send_update;
1714
1715 diff = this->peer_cfg->get_over_time(this->peer_cfg);
1716 now = time_monotonic(NULL);
1717 hard = now + lifetime;
1718 soft = hard - diff;
1719
1720 /* check if we have to send an AUTH_LIFETIME to enforce the new lifetime.
1721 * We send the notify in IKE_AUTH if not yet ESTABLISHED. */
1722 send_update = this->state == IKE_ESTABLISHED && this->version == IKEV2 &&
1723 !has_condition(this, COND_ORIGINAL_INITIATOR) &&
1724 (this->other_vips->get_count(this->other_vips) != 0 ||
1725 has_condition(this, COND_EAP_AUTHENTICATED));
1726
1727 if (lifetime < diff)
1728 {
1729 this->stats[STAT_REAUTH] = now;
1730
1731 if (!send_update)
1732 {
1733 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1734 "starting reauthentication", lifetime);
1735 lib->processor->queue_job(lib->processor,
1736 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE));
1737 }
1738 }
1739 else if (this->stats[STAT_REAUTH] == 0 ||
1740 this->stats[STAT_REAUTH] > soft)
1741 {
1742 this->stats[STAT_REAUTH] = soft;
1743 if (!send_update)
1744 {
1745 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, scheduling "
1746 "reauthentication in %ds", lifetime, lifetime - diff);
1747 lib->scheduler->schedule_job(lib->scheduler,
1748 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE),
1749 lifetime - diff);
1750 }
1751 }
1752 else
1753 {
1754 DBG1(DBG_IKE, "received AUTH_LIFETIME of %ds, "
1755 "reauthentication already scheduled in %ds", lifetime,
1756 this->stats[STAT_REAUTH] - time_monotonic(NULL));
1757 send_update = FALSE;
1758 }
1759 /* give at least some seconds to reauthenticate */
1760 this->stats[STAT_DELETE] = max(hard, now + 10);
1761
1762 if (send_update)
1763 {
1764 task = ike_auth_lifetime_create(&this->public, TRUE);
1765 this->task_manager->queue_task(this->task_manager, &task->task);
1766 return this->task_manager->initiate(this->task_manager);
1767 }
1768 return SUCCESS;
1769 }
1770
1771 /**
1772 * Check if the current combination of source and destination address is still
1773 * valid.
1774 */
1775 static bool is_current_path_valid(private_ike_sa_t *this)
1776 {
1777 bool valid = FALSE;
1778 host_t *src;
1779 src = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
1780 this->other_host, this->my_host);
1781 if (src)
1782 {
1783 if (src->ip_equals(src, this->my_host))
1784 {
1785 valid = TRUE;
1786 }
1787 src->destroy(src);
1788 }
1789 return valid;
1790 }
1791
1792 /**
1793 * Check if we have any path avialable for this IKE SA.
1794 */
1795 static bool is_any_path_valid(private_ike_sa_t *this)
1796 {
1797 bool valid = FALSE;
1798 enumerator_t *enumerator;
1799 host_t *src = NULL, *addr;
1800
1801 DBG1(DBG_IKE, "old path is not available anymore, try to find another");
1802 enumerator = create_peer_address_enumerator(this);
1803 while (enumerator->enumerate(enumerator, &addr))
1804 {
1805 DBG1(DBG_IKE, "looking for a route to %H ...", addr);
1806 src = hydra->kernel_interface->get_source_addr(
1807 hydra->kernel_interface, addr, NULL);
1808 if (src)
1809 {
1810 break;
1811 }
1812 }
1813 enumerator->destroy(enumerator);
1814 if (src)
1815 {
1816 valid = TRUE;
1817 src->destroy(src);
1818 }
1819 return valid;
1820 }
1821
1822 METHOD(ike_sa_t, roam, status_t,
1823 private_ike_sa_t *this, bool address)
1824 {
1825 switch (this->state)
1826 {
1827 case IKE_CREATED:
1828 case IKE_DELETING:
1829 case IKE_DESTROYING:
1830 case IKE_PASSIVE:
1831 return SUCCESS;
1832 default:
1833 break;
1834 }
1835
1836 /* keep existing path if possible */
1837 if (is_current_path_valid(this))
1838 {
1839 DBG2(DBG_IKE, "keeping connection path %H - %H",
1840 this->my_host, this->other_host);
1841 set_condition(this, COND_STALE, FALSE);
1842
1843 if (supports_extension(this, EXT_MOBIKE) && address)
1844 { /* if any addresses changed, send an updated list */
1845 DBG1(DBG_IKE, "sending address list update using MOBIKE");
1846 this->task_manager->queue_mobike(this->task_manager, FALSE, TRUE);
1847 return this->task_manager->initiate(this->task_manager);
1848 }
1849 return SUCCESS;
1850 }
1851
1852 if (!is_any_path_valid(this))
1853 {
1854 DBG1(DBG_IKE, "no route found to reach %H, MOBIKE update deferred",
1855 this->other_host);
1856 set_condition(this, COND_STALE, TRUE);
1857 return SUCCESS;
1858 }
1859 set_condition(this, COND_STALE, FALSE);
1860
1861 /* update addresses with mobike, if supported ... */
1862 if (supports_extension(this, EXT_MOBIKE))
1863 {
1864 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1865 { /* responder updates the peer about changed address config */
1866 DBG1(DBG_IKE, "sending address list update using MOBIKE, "
1867 "implicitly requesting an address change");
1868 address = TRUE;
1869 }
1870 else
1871 {
1872 DBG1(DBG_IKE, "requesting address change using MOBIKE");
1873 }
1874 this->task_manager->queue_mobike(this->task_manager, TRUE, address);
1875 return this->task_manager->initiate(this->task_manager);
1876 }
1877
1878 /* ... reauth if not */
1879 if (!has_condition(this, COND_ORIGINAL_INITIATOR))
1880 { /* responder does not reauthenticate */
1881 set_condition(this, COND_STALE, TRUE);
1882 return SUCCESS;
1883 }
1884 DBG1(DBG_IKE, "reauthenticating IKE_SA due to address change");
1885 /* since our previous path is not valid anymore, try and find a new one */
1886 resolve_hosts(this);
1887 return reauth(this);
1888 }
1889
1890 METHOD(ike_sa_t, add_configuration_attribute, void,
1891 private_ike_sa_t *this, attribute_handler_t *handler,
1892 configuration_attribute_type_t type, chunk_t data)
1893 {
1894 attribute_entry_t *entry = malloc_thing(attribute_entry_t);
1895
1896 entry->handler = handler;
1897 entry->type = type;
1898 entry->data = chunk_clone(data);
1899
1900 this->attributes->insert_last(this->attributes, entry);
1901 }
1902
1903 METHOD(ike_sa_t, create_task_enumerator, enumerator_t*,
1904 private_ike_sa_t *this, task_queue_t queue)
1905 {
1906 return this->task_manager->create_task_enumerator(this->task_manager, queue);
1907 }
1908
1909 METHOD(ike_sa_t, flush_queue, void,
1910 private_ike_sa_t *this, task_queue_t queue)
1911 {
1912 this->task_manager->flush_queue(this->task_manager, queue);
1913 }
1914
1915 METHOD(ike_sa_t, queue_task, void,
1916 private_ike_sa_t *this, task_t *task)
1917 {
1918 this->task_manager->queue_task(this->task_manager, task);
1919 }
1920
1921 METHOD(ike_sa_t, inherit, void,
1922 private_ike_sa_t *this, ike_sa_t *other_public)
1923 {
1924 private_ike_sa_t *other = (private_ike_sa_t*)other_public;
1925 child_sa_t *child_sa;
1926 attribute_entry_t *entry;
1927 enumerator_t *enumerator;
1928 auth_cfg_t *cfg;
1929 host_t *vip;
1930
1931 /* apply hosts and ids */
1932 this->my_host->destroy(this->my_host);
1933 this->other_host->destroy(this->other_host);
1934 this->my_id->destroy(this->my_id);
1935 this->other_id->destroy(this->other_id);
1936 this->my_host = other->my_host->clone(other->my_host);
1937 this->other_host = other->other_host->clone(other->other_host);
1938 this->my_id = other->my_id->clone(other->my_id);
1939 this->other_id = other->other_id->clone(other->other_id);
1940
1941 /* apply assigned virtual IPs... */
1942 while (this->my_vips->remove_last(this->my_vips, (void**)&vip) == SUCCESS)
1943 {
1944 other->my_vips->insert_first(other->my_vips, vip);
1945 }
1946 while (this->other_vips->remove_last(this->other_vips,
1947 (void**)&vip) == SUCCESS)
1948 {
1949 other->other_vips->insert_first(other->other_vips, vip);
1950 }
1951
1952 /* authentication information */
1953 enumerator = other->my_auths->create_enumerator(other->my_auths);
1954 while (enumerator->enumerate(enumerator, &cfg))
1955 {
1956 this->my_auths->insert_last(this->my_auths, cfg->clone(cfg));
1957 }
1958 enumerator->destroy(enumerator);
1959 enumerator = other->other_auths->create_enumerator(other->other_auths);
1960 while (enumerator->enumerate(enumerator, &cfg))
1961 {
1962 this->other_auths->insert_last(this->other_auths, cfg->clone(cfg));
1963 }
1964 enumerator->destroy(enumerator);
1965
1966 /* ... and configuration attributes */
1967 while (other->attributes->remove_last(other->attributes,
1968 (void**)&entry) == SUCCESS)
1969 {
1970 this->attributes->insert_first(this->attributes, entry);
1971 }
1972
1973 /* inherit all conditions */
1974 this->conditions = other->conditions;
1975 if (this->conditions & COND_NAT_HERE)
1976 {
1977 send_keepalive(this);
1978 }
1979
1980 #ifdef ME
1981 if (other->is_mediation_server)
1982 {
1983 act_as_mediation_server(this);
1984 }
1985 else if (other->server_reflexive_host)
1986 {
1987 this->server_reflexive_host = other->server_reflexive_host->clone(
1988 other->server_reflexive_host);
1989 }
1990 #endif /* ME */
1991
1992 /* adopt all children */
1993 while (other->child_sas->remove_last(other->child_sas,
1994 (void**)&child_sa) == SUCCESS)
1995 {
1996 this->child_sas->insert_first(this->child_sas, (void*)child_sa);
1997 }
1998
1999 /* move pending tasks to the new IKE_SA */
2000 this->task_manager->adopt_tasks(this->task_manager, other->task_manager);
2001
2002 /* reauthentication timeout survives a rekeying */
2003 if (other->stats[STAT_REAUTH])
2004 {
2005 time_t reauth, delete, now = time_monotonic(NULL);
2006
2007 this->stats[STAT_REAUTH] = other->stats[STAT_REAUTH];
2008 reauth = this->stats[STAT_REAUTH] - now;
2009 delete = reauth + this->peer_cfg->get_over_time(this->peer_cfg);
2010 this->stats[STAT_DELETE] = this->stats[STAT_REAUTH] + delete;
2011 DBG1(DBG_IKE, "rescheduling reauthentication in %ds after rekeying, "
2012 "lifetime reduced to %ds", reauth, delete);
2013 lib->scheduler->schedule_job(lib->scheduler,
2014 (job_t*)rekey_ike_sa_job_create(this->ike_sa_id, TRUE), reauth);
2015 lib->scheduler->schedule_job(lib->scheduler,
2016 (job_t*)delete_ike_sa_job_create(this->ike_sa_id, TRUE), delete);
2017 }
2018 }
2019
2020 METHOD(ike_sa_t, destroy, void,
2021 private_ike_sa_t *this)
2022 {
2023 attribute_entry_t *entry;
2024 host_t *vip;
2025
2026 charon->bus->set_sa(charon->bus, &this->public);
2027
2028 set_state(this, IKE_DESTROYING);
2029 DESTROY_IF(this->task_manager);
2030
2031 /* remove attributes first, as we pass the IKE_SA to the handler */
2032 while (this->attributes->remove_last(this->attributes,
2033 (void**)&entry) == SUCCESS)
2034 {
2035 hydra->attributes->release(hydra->attributes, entry->handler,
2036 this->other_id, entry->type, entry->data);
2037 free(entry->data.ptr);
2038 free(entry);
2039 }
2040 this->attributes->destroy(this->attributes);
2041
2042 this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy));
2043
2044 /* unset SA after here to avoid usage by the listeners */
2045 charon->bus->set_sa(charon->bus, NULL);
2046
2047 DESTROY_IF(this->keymat);
2048
2049 while (this->my_vips->remove_last(this->my_vips, (void**)&vip) == SUCCESS)
2050 {
2051 hydra->kernel_interface->del_ip(hydra->kernel_interface, vip);
2052 vip->destroy(vip);
2053 }
2054 this->my_vips->destroy(this->my_vips);
2055 while (this->other_vips->remove_last(this->other_vips,
2056 (void**)&vip) == SUCCESS)
2057 {
2058 if (this->peer_cfg && this->peer_cfg->get_pool(this->peer_cfg))
2059 {
2060 hydra->attributes->release_address(hydra->attributes,
2061 this->peer_cfg->get_pool(this->peer_cfg),
2062 vip, get_other_eap_id(this));
2063 }
2064 vip->destroy(vip);
2065 }
2066 this->other_vips->destroy(this->other_vips);
2067 this->peer_addresses->destroy_offset(this->peer_addresses,
2068 offsetof(host_t, destroy));
2069 #ifdef ME
2070 if (this->is_mediation_server)
2071 {
2072 charon->mediation_manager->remove(charon->mediation_manager,
2073 this->ike_sa_id);
2074 }
2075 DESTROY_IF(this->server_reflexive_host);
2076 chunk_free(&this->connect_id);
2077 #endif /* ME */
2078 free(this->nat_detection_dest.ptr);
2079
2080 DESTROY_IF(this->my_host);
2081 DESTROY_IF(this->other_host);
2082 DESTROY_IF(this->my_id);
2083 DESTROY_IF(this->other_id);
2084 DESTROY_IF(this->local_host);
2085 DESTROY_IF(this->remote_host);
2086
2087 DESTROY_IF(this->ike_cfg);
2088 DESTROY_IF(this->peer_cfg);
2089 DESTROY_IF(this->proposal);
2090 this->my_auth->destroy(this->my_auth);
2091 this->other_auth->destroy(this->other_auth);
2092 this->my_auths->destroy_offset(this->my_auths,
2093 offsetof(auth_cfg_t, destroy));
2094 this->other_auths->destroy_offset(this->other_auths,
2095 offsetof(auth_cfg_t, destroy));
2096
2097 this->ike_sa_id->destroy(this->ike_sa_id);
2098 free(this);
2099 }
2100
2101 /*
2102 * Described in header.
2103 */
2104 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id, bool initiator,
2105 ike_version_t version)
2106 {
2107 private_ike_sa_t *this;
2108 static u_int32_t unique_id = 0;
2109
2110 if (version == IKE_ANY)
2111 { /* prefer IKEv2 if protocol not specified */
2112 #ifdef USE_IKEV2
2113 version = IKEV2;
2114 #else
2115 version = IKEV1;
2116 #endif
2117 }
2118
2119 INIT(this,
2120 .public = {
2121 .get_version = _get_version,
2122 .get_state = _get_state,
2123 .set_state = _set_state,
2124 .get_name = _get_name,
2125 .get_statistic = _get_statistic,
2126 .set_statistic = _set_statistic,
2127 .process_message = _process_message,
2128 .initiate = _initiate,
2129 .retry_initiate = _retry_initiate,
2130 .get_ike_cfg = _get_ike_cfg,
2131 .set_ike_cfg = _set_ike_cfg,
2132 .get_peer_cfg = _get_peer_cfg,
2133 .set_peer_cfg = _set_peer_cfg,
2134 .get_auth_cfg = _get_auth_cfg,
2135 .create_auth_cfg_enumerator = _create_auth_cfg_enumerator,
2136 .add_auth_cfg = _add_auth_cfg,
2137 .get_proposal = _get_proposal,
2138 .set_proposal = _set_proposal,
2139 .get_id = _get_id,
2140 .get_my_host = _get_my_host,
2141 .set_my_host = _set_my_host,
2142 .get_other_host = _get_other_host,
2143 .set_other_host = _set_other_host,
2144 .set_message_id = _set_message_id,
2145 .float_ports = _float_ports,
2146 .update_hosts = _update_hosts,
2147 .get_my_id = _get_my_id,
2148 .set_my_id = _set_my_id,
2149 .get_other_id = _get_other_id,
2150 .set_other_id = _set_other_id,
2151 .get_other_eap_id = _get_other_eap_id,
2152 .enable_extension = _enable_extension,
2153 .supports_extension = _supports_extension,
2154 .set_condition = _set_condition,
2155 .has_condition = _has_condition,
2156 .set_pending_updates = _set_pending_updates,
2157 .get_pending_updates = _get_pending_updates,
2158 .create_peer_address_enumerator = _create_peer_address_enumerator,
2159 .add_peer_address = _add_peer_address,
2160 .clear_peer_addresses = _clear_peer_addresses,
2161 .has_mapping_changed = _has_mapping_changed,
2162 .retransmit = _retransmit,
2163 .delete = _delete_,
2164 .destroy = _destroy,
2165 .send_dpd = _send_dpd,
2166 .send_keepalive = _send_keepalive,
2167 .get_keymat = _get_keymat,
2168 .add_child_sa = _add_child_sa,
2169 .get_child_sa = _get_child_sa,
2170 .get_child_count = _get_child_count,
2171 .create_child_sa_enumerator = _create_child_sa_enumerator,
2172 .remove_child_sa = _remove_child_sa,
2173 .rekey_child_sa = _rekey_child_sa,
2174 .delete_child_sa = _delete_child_sa,
2175 .destroy_child_sa = _destroy_child_sa,
2176 .rekey = _rekey,
2177 .reauth = _reauth,
2178 .reestablish = _reestablish,
2179 .set_auth_lifetime = _set_auth_lifetime,
2180 .roam = _roam,
2181 .inherit = _inherit,
2182 .generate_message = _generate_message,
2183 .reset = _reset,
2184 .get_unique_id = _get_unique_id,
2185 .add_virtual_ip = _add_virtual_ip,
2186 .create_virtual_ip_enumerator = _create_virtual_ip_enumerator,
2187 .add_configuration_attribute = _add_configuration_attribute,
2188 .set_kmaddress = _set_kmaddress,
2189 .create_task_enumerator = _create_task_enumerator,
2190 .flush_queue = _flush_queue,
2191 .queue_task = _queue_task,
2192 #ifdef ME
2193 .act_as_mediation_server = _act_as_mediation_server,
2194 .get_server_reflexive_host = _get_server_reflexive_host,
2195 .set_server_reflexive_host = _set_server_reflexive_host,
2196 .get_connect_id = _get_connect_id,
2197 .initiate_mediation = _initiate_mediation,
2198 .initiate_mediated = _initiate_mediated,
2199 .relay = _relay,
2200 .callback = _callback,
2201 .respond = _respond,
2202 #endif /* ME */
2203 },
2204 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
2205 .version = version,
2206 .child_sas = linked_list_create(),
2207 .my_host = host_create_any(AF_INET),
2208 .other_host = host_create_any(AF_INET),
2209 .my_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2210 .other_id = identification_create_from_encoding(ID_ANY, chunk_empty),
2211 .keymat = keymat_create(version, initiator),
2212 .state = IKE_CREATED,
2213 .stats[STAT_INBOUND] = time_monotonic(NULL),
2214 .stats[STAT_OUTBOUND] = time_monotonic(NULL),
2215 .my_auth = auth_cfg_create(),
2216 .other_auth = auth_cfg_create(),
2217 .my_auths = linked_list_create(),
2218 .other_auths = linked_list_create(),
2219 .unique_id = ++unique_id,
2220 .peer_addresses = linked_list_create(),
2221 .my_vips = linked_list_create(),
2222 .other_vips = linked_list_create(),
2223 .attributes = linked_list_create(),
2224 .keepalive_interval = lib->settings->get_time(lib->settings,
2225 "%s.keep_alive", KEEPALIVE_INTERVAL, charon->name),
2226 .retry_initiate_interval = lib->settings->get_time(lib->settings,
2227 "%s.retry_initiate_interval", 0, charon->name),
2228 .flush_auth_cfg = lib->settings->get_bool(lib->settings,
2229 "%s.flush_auth_cfg", FALSE, charon->name),
2230 );
2231
2232 if (version == IKEV2)
2233 { /* always supported with IKEv2 */
2234 enable_extension(this, EXT_DPD);
2235 }
2236
2237 this->task_manager = task_manager_create(&this->public);
2238 this->my_host->set_port(this->my_host,
2239 charon->socket->get_port(charon->socket, FALSE));
2240
2241 if (!this->task_manager || !this->keymat)
2242 {
2243 DBG1(DBG_IKE, "IKE version %d not supported", this->version);
2244 destroy(this);
2245 return NULL;
2246 }
2247 return &this->public;
2248 }