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