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