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