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