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