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