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