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