2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
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>.
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
31 #include <utils/linked_list.h>
32 #include <utils/lexparser.h>
33 #include <crypto/diffie_hellman.h>
34 #include <crypto/prf_plus.h>
35 #include <crypto/crypters/crypter.h>
36 #include <crypto/hashers/hasher.h>
37 #include <encoding/payloads/sa_payload.h>
38 #include <encoding/payloads/nonce_payload.h>
39 #include <encoding/payloads/ke_payload.h>
40 #include <encoding/payloads/delete_payload.h>
41 #include <encoding/payloads/transform_substructure.h>
42 #include <encoding/payloads/transform_attribute.h>
43 #include <encoding/payloads/ts_payload.h>
44 #include <sa/task_manager.h>
45 #include <sa/tasks/ike_init.h>
46 #include <sa/tasks/ike_natd.h>
47 #include <sa/tasks/ike_mobike.h>
48 #include <sa/tasks/ike_auth.h>
49 #include <sa/tasks/ike_auth_lifetime.h>
50 #include <sa/tasks/ike_config.h>
51 #include <sa/tasks/ike_cert_pre.h>
52 #include <sa/tasks/ike_cert_post.h>
53 #include <sa/tasks/ike_rekey.h>
54 #include <sa/tasks/ike_reauth.h>
55 #include <sa/tasks/ike_delete.h>
56 #include <sa/tasks/ike_dpd.h>
57 #include <sa/tasks/child_create.h>
58 #include <sa/tasks/child_delete.h>
59 #include <sa/tasks/child_rekey.h>
60 #include <processing/jobs/retransmit_job.h>
61 #include <processing/jobs/delete_ike_sa_job.h>
62 #include <processing/jobs/send_dpd_job.h>
63 #include <processing/jobs/send_keepalive_job.h>
64 #include <processing/jobs/rekey_ike_sa_job.h>
67 #include <sa/tasks/ike_me.h>
68 #include <processing/jobs/initiate_mediation_job.h>
72 #define RESOLV_CONF "/etc/resolv.conf"
75 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DESTROYING
,
84 typedef struct private_ike_sa_t private_ike_sa_t
;
87 * Private data of an ike_sa_t object.
89 struct private_ike_sa_t
{
97 * Identifier for the current IKE_SA.
99 ike_sa_id_t
*ike_sa_id
;
102 * unique numerical ID for this IKE_SA.
107 * Current state of the IKE_SA
109 ike_sa_state_t state
;
112 * IKE configuration used to set up this IKE_SA
117 * Peer and authentication information to establish IKE_SA.
119 peer_cfg_t
*peer_cfg
;
122 * associated authentication/authorization info for local peer
124 auth_info_t
*my_auth
;
127 * associated authentication/authorization info for remote peer
129 auth_info_t
*other_auth
;
132 * Juggles tasks to process messages
134 task_manager_t
*task_manager
;
137 * Address of local host
142 * Address of remote host
148 * Are we mediation server
150 bool is_mediation_server
;
153 * Server reflexive host
155 host_t
*server_reflexive_host
;
164 * Identification used for us
166 identification_t
*my_id
;
169 * Identification used for other
171 identification_t
*other_id
;
174 * EAP Identity exchange in EAP-Identity method
176 identification_t
*eap_identity
;;
179 * set of extensions the peer supports
181 ike_extension_t extensions
;
184 * set of condition flags currently enabled for this IKE_SA
186 ike_condition_t conditions
;
189 * Linked List containing the child sa's of the current IKE_SA.
191 linked_list_t
*child_sas
;
194 * String describing the selected IKE proposal
196 char *selected_proposal
;
199 * crypter for inbound traffic
201 crypter_t
*crypter_in
;
204 * crypter for outbound traffic
206 crypter_t
*crypter_out
;
209 * Signer for inbound traffic
214 * Signer for outbound traffic
216 signer_t
*signer_out
;
219 * Multi purpose prf, set key, use it, forget it
224 * Prf function for derivating keymat child SAs
229 * Key to build outging authentication data (SKp)
234 * Key to verify incoming authentication data (SKp)
239 * Virtual IP on local host, if any
241 host_t
*my_virtual_ip
;
244 * Virtual IP on remote host, if any
246 host_t
*other_virtual_ip
;
249 * List of DNS servers installed by us
251 linked_list_t
*dns_servers
;
254 * list of peers additional addresses, transmitted via MOBIKE
256 linked_list_t
*additional_addresses
;
259 * previously value of received DESTINATION_IP hash
261 chunk_t nat_detection_dest
;
264 * number pending UPDATE_SA_ADDRESS (MOBIKE)
266 u_int32_t pending_updates
;
269 * NAT keep alive interval
271 u_int32_t keepalive_interval
;
274 * Timestamps for this IKE_SA
277 /** last IKE message received */
279 /** last IKE message sent */
281 /** when IKE_SA became established */
282 u_int32_t established
;
283 /** when IKE_SA gets rekeyed */
285 /** when IKE_SA gets reauthenticated */
287 /** when IKE_SA gets deleted */
292 * how many times we have retried so far (keyingtries)
297 * are we the initiator of this IKE_SA (rekeying does not affect this flag)
303 * get the time of the latest traffic processed by the kernel
305 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
307 enumerator_t
*enumerator
;
308 child_sa_t
*child_sa
;
313 use_time
= this->time
.inbound
;
317 use_time
= this->time
.outbound
;
319 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
320 while (enumerator
->enumerate(enumerator
, &child_sa
))
322 use_time
= max(use_time
, child_sa
->get_usetime(child_sa
, inbound
));
324 enumerator
->destroy(enumerator
);
330 * Implementation of ike_sa_t.get_unique_id
332 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
334 return this->unique_id
;
338 * Implementation of ike_sa_t.get_name.
340 static char *get_name(private_ike_sa_t
*this)
344 return this->peer_cfg
->get_name(this->peer_cfg
);
350 * Implementation of ike_sa_t.get_statistic.
352 static u_int32_t
get_statistic(private_ike_sa_t
*this, statistic_t kind
)
354 time_t now
= time(NULL
);
358 case STAT_REKEY_TIME
:
359 if (this->time
.rekey
> now
)
361 return this->time
.rekey
- now
;
364 case STAT_REAUTH_TIME
:
365 if (this->time
.reauth
> now
)
367 return this->time
.reauth
- now
;
377 * Implementation of ike_sa_t.get_my_host.
379 static host_t
*get_my_host(private_ike_sa_t
*this)
381 return this->my_host
;
385 * Implementation of ike_sa_t.set_my_host.
387 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
389 DESTROY_IF(this->my_host
);
394 * Implementation of ike_sa_t.get_other_host.
396 static host_t
*get_other_host(private_ike_sa_t
*this)
398 return this->other_host
;
402 * Implementation of ike_sa_t.set_other_host.
404 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
406 DESTROY_IF(this->other_host
);
407 this->other_host
= other
;
411 * Implementation of ike_sa_t.get_peer_cfg
413 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
415 return this->peer_cfg
;
419 * Implementation of ike_sa_t.set_peer_cfg
421 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
423 DESTROY_IF(this->peer_cfg
);
424 peer_cfg
->get_ref(peer_cfg
);
425 this->peer_cfg
= peer_cfg
;
427 if (this->ike_cfg
== NULL
)
429 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
430 this->ike_cfg
->get_ref(this->ike_cfg
);
432 /* apply IDs if they are not already set */
433 if (this->my_id
->contains_wildcards(this->my_id
))
435 DESTROY_IF(this->my_id
);
436 this->my_id
= this->peer_cfg
->get_my_id(this->peer_cfg
);
437 this->my_id
= this->my_id
->clone(this->my_id
);
439 if (this->other_id
->contains_wildcards(this->other_id
))
441 DESTROY_IF(this->other_id
);
442 this->other_id
= this->peer_cfg
->get_other_id(this->peer_cfg
);
443 this->other_id
= this->other_id
->clone(this->other_id
);
448 * Implementation of ike_sa_t.get_my_auth.
450 static auth_info_t
* get_my_auth(private_ike_sa_t
*this)
452 return this->my_auth
;
456 * Implementation of ike_sa_t.get_other_auth.
458 static auth_info_t
* get_other_auth(private_ike_sa_t
*this)
460 return this->other_auth
;
464 * Implementation of ike_sa_t.send_keepalive
466 static void send_keepalive(private_ike_sa_t
*this)
468 send_keepalive_job_t
*job
;
469 time_t last_out
, now
, diff
;
471 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
472 { /* disable keep alives if we are not NATed anymore */
476 last_out
= get_use_time(this, FALSE
);
479 diff
= now
- last_out
;
481 if (diff
>= this->keepalive_interval
)
486 packet
= packet_create();
487 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
488 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
489 data
.ptr
= malloc(1);
492 packet
->set_data(packet
, data
);
493 DBG1(DBG_IKE
, "sending keep alive");
494 charon
->sender
->send(charon
->sender
, packet
);
497 job
= send_keepalive_job_create(this->ike_sa_id
);
498 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
499 (this->keepalive_interval
- diff
) * 1000);
503 * Implementation of ike_sa_t.get_ike_cfg
505 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
507 return this->ike_cfg
;
511 * Implementation of ike_sa_t.set_ike_cfg
513 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
515 ike_cfg
->get_ref(ike_cfg
);
516 this->ike_cfg
= ike_cfg
;
520 * Implementation of ike_sa_t.is_ike_initiator
522 static bool is_ike_initiator(private_ike_sa_t
*this)
524 return this->ike_initiator
;
528 * Implementation of ike_sa_t.enable_extension.
530 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
532 this->extensions
|= extension
;
536 * Implementation of ike_sa_t.has_extension.
538 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
540 return (this->extensions
& extension
) != FALSE
;
544 * Implementation of ike_sa_t.has_condition.
546 static bool has_condition(private_ike_sa_t
*this, ike_condition_t condition
)
548 return (this->conditions
& condition
) != FALSE
;
552 * Implementation of ike_sa_t.enable_condition.
554 static void set_condition(private_ike_sa_t
*this, ike_condition_t condition
,
557 if (has_condition(this, condition
) != enable
)
561 this->conditions
|= condition
;
565 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
566 this->conditions
|= COND_NAT_ANY
;
567 send_keepalive(this);
570 DBG1(DBG_IKE
, "remote host is behind NAT");
571 this->conditions
|= COND_NAT_ANY
;
574 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
575 this->conditions
|= COND_NAT_ANY
;
583 this->conditions
&= ~condition
;
589 set_condition(this, COND_NAT_ANY
,
590 has_condition(this, COND_NAT_HERE
) ||
591 has_condition(this, COND_NAT_THERE
) ||
592 has_condition(this, COND_NAT_FAKE
));
602 * Implementation of ike_sa_t.send_dpd
604 static status_t
send_dpd(private_ike_sa_t
*this)
609 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
617 if (this->task_manager
->busy(this->task_manager
))
619 /* an exchange is in the air, no need to start a DPD check */
624 /* check if there was any inbound traffic */
626 last_in
= get_use_time(this, TRUE
);
628 diff
= now
- last_in
;
631 /* to long ago, initiate dead peer detection */
633 ike_mobike_t
*mobike
;
635 if (supports_extension(this, EXT_MOBIKE
) &&
636 has_condition(this, COND_NAT_HERE
))
638 /* use mobike enabled DPD to detect NAT mapping changes */
639 mobike
= ike_mobike_create(&this->public, TRUE
);
641 task
= &mobike
->task
;
645 task
= (task_t
*)ike_dpd_create(TRUE
);
648 DBG1(DBG_IKE
, "sending DPD request");
650 this->task_manager
->queue_task(this->task_manager
, task
);
651 this->task_manager
->initiate(this->task_manager
);
654 /* recheck in "interval" seconds */
655 job
= send_dpd_job_create(this->ike_sa_id
);
656 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
657 (delay
- diff
) * 1000);
662 * Implementation of ike_sa_t.get_state.
664 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
670 * Implementation of ike_sa_t.set_state.
672 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
674 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
675 get_name(this), this->unique_id
,
676 ike_sa_state_names
, this->state
,
677 ike_sa_state_names
, state
);
681 case IKE_ESTABLISHED
:
683 if (this->state
== IKE_CONNECTING
)
688 /* calculate rekey, reauth and lifetime */
689 this->time
.established
= time(NULL
);
691 /* schedule rekeying if we have a time which is smaller than
692 * an already scheduled rekeying */
693 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
);
694 if (t
&& (this->time
.rekey
== 0 ||
695 (this->time
.rekey
> t
+ this->time
.established
)))
697 this->time
.rekey
= t
+ this->time
.established
;
698 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
699 charon
->scheduler
->schedule_job(charon
->scheduler
,
701 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
703 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
);
704 if (t
&& (this->time
.reauth
== 0 ||
705 (this->time
.reauth
> t
+ this->time
.established
)))
707 this->time
.reauth
= t
+ this->time
.established
;
708 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
709 charon
->scheduler
->schedule_job(charon
->scheduler
,
711 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
713 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
714 if (this->time
.rekey
|| this->time
.reauth
)
716 if (this->time
.reauth
== 0)
718 this->time
.delete = this->time
.rekey
;
720 else if (this->time
.rekey
== 0)
722 this->time
.delete = this->time
.reauth
;
726 this->time
.delete = min(this->time
.rekey
, this->time
.reauth
);
728 this->time
.delete += t
;
729 t
= this->time
.delete - this->time
.established
;
730 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
731 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
733 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
736 /* start DPD checks */
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
);
752 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
757 * Implementation of ike_sa_t.reset
759 static void reset(private_ike_sa_t
*this)
761 /* the responder ID is reset, as peer may choose another one */
762 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
764 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
767 set_state(this, IKE_CREATED
);
769 this->task_manager
->reset(this->task_manager
);
773 * Implementation of ike_sa_t.set_virtual_ip
775 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
779 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
780 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
781 this->my_host
) == SUCCESS
)
783 if (this->my_virtual_ip
)
785 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
786 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
787 this->my_virtual_ip
);
789 DESTROY_IF(this->my_virtual_ip
);
790 this->my_virtual_ip
= ip
->clone(ip
);
794 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
795 this->my_virtual_ip
= NULL
;
800 DESTROY_IF(this->other_virtual_ip
);
801 this->other_virtual_ip
= ip
->clone(ip
);
806 * Implementation of ike_sa_t.get_virtual_ip
808 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
812 return this->my_virtual_ip
;
816 return this->other_virtual_ip
;
821 * Implementation of ike_sa_t.add_additional_address.
823 static void add_additional_address(private_ike_sa_t
*this, host_t
*host
)
825 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
829 * Implementation of ike_sa_t.create_additional_address_iterator.
831 static iterator_t
* create_additional_address_iterator(private_ike_sa_t
*this)
833 return this->additional_addresses
->create_iterator(
834 this->additional_addresses
, TRUE
);
838 * Implementation of ike_sa_t.has_mapping_changed
840 static bool has_mapping_changed(private_ike_sa_t
*this, chunk_t hash
)
842 if (this->nat_detection_dest
.ptr
== NULL
)
844 this->nat_detection_dest
= chunk_clone(hash
);
847 if (chunk_equals(hash
, this->nat_detection_dest
))
851 free(this->nat_detection_dest
.ptr
);
852 this->nat_detection_dest
= chunk_clone(hash
);
857 * Implementation of ike_sa_t.set_pending_updates.
859 static void set_pending_updates(private_ike_sa_t
*this, u_int32_t updates
)
861 this->pending_updates
= updates
;
865 * Implementation of ike_sa_t.get_pending_updates.
867 static u_int32_t
get_pending_updates(private_ike_sa_t
*this)
869 return this->pending_updates
;
873 * Update hosts, as addresses may change (NAT)
875 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
885 other
= this->other_host
;
888 /* apply hosts on first received message */
889 if (this->my_host
->is_anyaddr(this->my_host
) ||
890 this->other_host
->is_anyaddr(this->other_host
))
892 set_my_host(this, me
->clone(me
));
893 set_other_host(this, other
->clone(other
));
898 /* update our address in any case */
899 if (!me
->equals(me
, this->my_host
))
901 set_my_host(this, me
->clone(me
));
905 if (!other
->equals(other
, this->other_host
))
907 /* update others adress if we are NOT NATed,
908 * and allow port changes if we are NATed */
909 if (!has_condition(this, COND_NAT_HERE
) ||
910 other
->ip_equals(other
, this->other_host
))
912 set_other_host(this, other
->clone(other
));
918 /* update all associated CHILD_SAs, if required */
921 iterator_t
*iterator
;
922 child_sa_t
*child_sa
;
924 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
925 while (iterator
->iterate(iterator
, (void**)&child_sa
))
927 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
928 this->my_virtual_ip
, has_condition(this, COND_NAT_ANY
));
930 iterator
->destroy(iterator
);
935 * Implementation of ike_sa_t.generate
937 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
940 this->time
.outbound
= time(NULL
);
941 message
->set_ike_sa_id(message
, this->ike_sa_id
);
942 return message
->generate(message
, this->crypter_out
, this->signer_out
, packet
);
946 * send a notify back to the sender
948 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
954 response
= message_create();
955 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
956 response
->set_request(response
, FALSE
);
957 response
->set_message_id(response
, request
->get_message_id(request
));
958 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
959 if (this->my_host
->is_anyaddr(this->my_host
))
961 this->my_host
->destroy(this->my_host
);
962 this->my_host
= request
->get_destination(request
);
963 this->my_host
= this->my_host
->clone(this->my_host
);
965 if (this->other_host
->is_anyaddr(this->other_host
))
967 this->other_host
->destroy(this->other_host
);
968 this->other_host
= request
->get_source(request
);
969 this->other_host
= this->other_host
->clone(this->other_host
);
971 response
->set_source(response
, this->my_host
->clone(this->my_host
));
972 response
->set_destination(response
, this->other_host
->clone(this->other_host
));
973 if (generate_message(this, response
, &packet
) == SUCCESS
)
975 charon
->sender
->send(charon
->sender
, packet
);
977 response
->destroy(response
);
982 * Implementation of ike_sa_t.act_as_mediation_server.
984 static void act_as_mediation_server(private_ike_sa_t
*this)
986 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
987 this->other_id
, this->ike_sa_id
);
988 this->is_mediation_server
= TRUE
;
992 * Implementation of ike_sa_t.get_server_reflexive_host.
994 static host_t
*get_server_reflexive_host(private_ike_sa_t
*this)
996 return this->server_reflexive_host
;
1000 * Implementation of ike_sa_t.set_server_reflexive_host.
1002 static void set_server_reflexive_host(private_ike_sa_t
*this, host_t
*host
)
1004 DESTROY_IF(this->server_reflexive_host
);
1005 this->server_reflexive_host
= host
;
1009 * Implementation of ike_sa_t.get_connect_id.
1011 static chunk_t
get_connect_id(private_ike_sa_t
*this)
1013 return this->connect_id
;
1017 * Implementation of ike_sa_t.respond
1019 static status_t
respond(private_ike_sa_t
*this, identification_t
*peer_id
,
1022 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1023 task
->respond(task
, peer_id
, connect_id
);
1024 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1025 return this->task_manager
->initiate(this->task_manager
);
1029 * Implementation of ike_sa_t.callback
1031 static status_t
callback(private_ike_sa_t
*this, identification_t
*peer_id
)
1033 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1034 task
->callback(task
, peer_id
);
1035 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1036 return this->task_manager
->initiate(this->task_manager
);
1040 * Implementation of ike_sa_t.relay
1042 static status_t
relay(private_ike_sa_t
*this, identification_t
*requester
,
1043 chunk_t connect_id
, chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
1045 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1046 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1047 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1048 return this->task_manager
->initiate(this->task_manager
);
1052 * Implementation of ike_sa_t.initiate_mediation
1054 static status_t
initiate_mediation(private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
1056 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1057 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1058 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1059 return this->task_manager
->initiate(this->task_manager
);
1063 * Implementation of ike_sa_t.initiate_mediated
1065 static status_t
initiate_mediated(private_ike_sa_t
*this, host_t
*me
, host_t
*other
,
1068 set_my_host(this, me
->clone(me
));
1069 set_other_host(this, other
->clone(other
));
1070 chunk_free(&this->connect_id
);
1071 this->connect_id
= chunk_clone(connect_id
);
1073 return this->task_manager
->initiate(this->task_manager
);
1078 * Resolve DNS host in configuration
1080 static void resolve_hosts(private_ike_sa_t
*this)
1084 host
= host_create_from_dns(this->ike_cfg
->get_other_addr(this->ike_cfg
),
1088 set_other_host(this, host
);
1091 host
= host_create_from_dns(this->ike_cfg
->get_my_addr(this->ike_cfg
),
1092 this->my_host
->get_family(this->my_host
),
1095 if (host
&& host
->is_anyaddr(host
) &&
1096 !this->other_host
->is_anyaddr(this->other_host
))
1098 host
->destroy(host
);
1099 host
= charon
->kernel_interface
->get_source_addr(
1100 charon
->kernel_interface
, this->other_host
, NULL
);
1103 host
->set_port(host
, IKEV2_UDP_PORT
);
1108 set_my_host(this, host
);
1113 * Initiates a CHILD_SA using the appropriate reqid
1115 static status_t
initiate_with_reqid(private_ike_sa_t
*this, child_cfg_t
*child_cfg
, u_int32_t reqid
)
1119 if (this->state
== IKE_CREATED
)
1121 resolve_hosts(this);
1123 if (this->other_host
->is_anyaddr(this->other_host
)
1125 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1129 child_cfg
->destroy(child_cfg
);
1130 DBG1(DBG_IKE
, "unable to initiate to %%any");
1134 this->ike_initiator
= TRUE
;
1136 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1137 this->task_manager
->queue_task(this->task_manager
, task
);
1138 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1139 this->task_manager
->queue_task(this->task_manager
, task
);
1140 task
= (task_t
*)ike_cert_pre_create(&this->public, TRUE
);
1141 this->task_manager
->queue_task(this->task_manager
, task
);
1142 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1143 this->task_manager
->queue_task(this->task_manager
, task
);
1144 task
= (task_t
*)ike_cert_post_create(&this->public, TRUE
);
1145 this->task_manager
->queue_task(this->task_manager
, task
);
1146 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1147 this->task_manager
->queue_task(this->task_manager
, task
);
1148 task
= (task_t
*)ike_auth_lifetime_create(&this->public, TRUE
);
1149 this->task_manager
->queue_task(this->task_manager
, task
);
1150 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1152 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1153 this->task_manager
->queue_task(this->task_manager
, task
);
1156 task
= (task_t
*)ike_me_create(&this->public, TRUE
);
1157 this->task_manager
->queue_task(this->task_manager
, task
);
1162 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1163 { /* mediation connection is already established, retrigger state change
1164 * to notify bus listeners */
1165 DBG1(DBG_IKE
, "mediation connection is already up");
1166 set_state(this, IKE_ESTABLISHED
);
1167 DESTROY_IF(child_cfg
);
1172 /* normal IKE_SA with CHILD_SA */
1173 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
1174 child_cfg
->destroy(child_cfg
);
1177 child_create_t
*child_create
= (child_create_t
*)task
;
1178 child_create
->use_reqid(child_create
, reqid
);
1180 this->task_manager
->queue_task(this->task_manager
, task
);
1183 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1185 /* mediated connection, initiate mediation process */
1186 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1187 charon
->processor
->queue_job(charon
->processor
, job
);
1193 return this->task_manager
->initiate(this->task_manager
);
1197 * Implementation of ike_sa_t.initiate.
1199 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1201 return initiate_with_reqid(this, child_cfg
, 0);
1205 * Implementation of ike_sa_t.acquire.
1207 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
1209 child_cfg_t
*child_cfg
;
1210 iterator_t
*iterator
;
1211 child_sa_t
*current
, *child_sa
= NULL
;
1213 if (this->state
== IKE_DELETING
)
1215 DBG1(DBG_IKE
, "acquiring CHILD_SA {reqid %d} failed: "
1216 "IKE_SA is deleting", reqid
);
1221 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1222 while (iterator
->iterate(iterator
, (void**)¤t
))
1224 if (current
->get_reqid(current
) == reqid
)
1230 iterator
->destroy(iterator
);
1233 DBG1(DBG_IKE
, "acquiring CHILD_SA {reqid %d} failed: "
1234 "CHILD_SA not found", reqid
);
1238 child_cfg
= child_sa
->get_config(child_sa
);
1239 child_cfg
->get_ref(child_cfg
);
1241 return initiate_with_reqid(this, child_cfg
, reqid
);
1245 * Implementation of ike_sa_t.route.
1247 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1249 child_sa_t
*child_sa
;
1250 iterator_t
*iterator
;
1251 linked_list_t
*my_ts
, *other_ts
;
1255 /* check if not already routed*/
1256 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1257 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1259 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1260 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
1262 iterator
->destroy(iterator
);
1263 DBG1(DBG_IKE
, "routing CHILD_SA failed: already routed");
1267 iterator
->destroy(iterator
);
1269 switch (this->state
)
1273 DBG1(DBG_IKE
, "routing CHILD_SA failed: IKE_SA is %N",
1274 ike_sa_state_names
, this->state
);
1277 case IKE_CONNECTING
:
1278 case IKE_ESTABLISHED
:
1283 resolve_hosts(this);
1285 /* install kernel policies */
1286 child_sa
= child_sa_create(this->my_host
, this->other_host
,
1287 child_cfg
, 0, FALSE
);
1289 if (this->my_virtual_ip
)
1291 me
= this->my_virtual_ip
;
1293 other
= this->other_host
;
1294 if (this->other_virtual_ip
)
1296 other
= this->other_virtual_ip
;
1299 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, me
);
1300 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, other
);
1301 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
,
1302 child_cfg
->get_mode(child_cfg
), PROTO_NONE
);
1303 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1304 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1305 if (status
== SUCCESS
)
1307 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1308 DBG1(DBG_IKE
, "CHILD_SA routed");
1312 DBG1(DBG_IKE
, "routing CHILD_SA failed");
1318 * Implementation of ike_sa_t.unroute.
1320 static status_t
unroute(private_ike_sa_t
*this, u_int32_t reqid
)
1322 iterator_t
*iterator
;
1323 child_sa_t
*child_sa
;
1326 /* find CHILD_SA in ROUTED state */
1327 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1328 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1330 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1331 child_sa
->get_reqid(child_sa
) == reqid
)
1333 iterator
->remove(iterator
);
1334 DBG1(DBG_IKE
, "CHILD_SA unrouted");
1335 child_sa
->destroy(child_sa
);
1340 iterator
->destroy(iterator
);
1344 DBG1(DBG_IKE
, "unrouting CHILD_SA failed: reqid %d not found", reqid
);
1347 /* if we are not established, and we have no more routed childs, remove whole SA */
1348 if (this->state
== IKE_CREATED
&&
1349 this->child_sas
->get_count(this->child_sas
) == 0)
1356 * Implementation of ike_sa_t.process_message.
1358 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
1363 is_request
= message
->get_request(message
);
1365 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
1366 if (status
!= SUCCESS
)
1374 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
1377 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
1381 DBG1(DBG_IKE
, "message parsing failed");
1384 send_notify_response(this, message
, INVALID_SYNTAX
);
1388 DBG1(DBG_IKE
, "message verification failed");
1391 send_notify_response(this, message
, INVALID_SYNTAX
);
1395 DBG1(DBG_IKE
, "integrity check failed");
1399 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1402 send_notify_response(this, message
, INVALID_SYNTAX
);
1408 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1409 exchange_type_names
, message
->get_exchange_type(message
),
1410 message
->get_request(message
) ?
"request" : "response",
1411 message
->get_message_id(message
));
1417 private_ike_sa_t
*new;
1418 iterator_t
*iterator
;
1420 bool has_routed
= FALSE
;
1422 me
= message
->get_destination(message
);
1423 other
= message
->get_source(message
);
1425 /* if this IKE_SA is virgin, we check for a config */
1426 if (this->ike_cfg
== NULL
)
1429 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1431 if (this->ike_cfg
== NULL
)
1433 /* no config found for these hosts, destroy */
1434 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1435 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1436 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
1439 /* add a timeout if peer does not establish it completely */
1440 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
1441 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
1442 HALF_OPEN_IKE_SA_TIMEOUT
);
1444 this->time
.inbound
= time(NULL
);
1445 /* check if message is trustworthy, and update host information */
1446 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
1447 message
->get_exchange_type(message
) != IKE_SA_INIT
)
1449 if (!supports_extension(this, EXT_MOBIKE
))
1450 { /* with MOBIKE, we do no implicit updates */
1451 update_hosts(this, me
, other
);
1454 status
= this->task_manager
->process_message(this->task_manager
, message
);
1455 if (status
!= DESTROY_ME
)
1459 /* if IKE_SA gets closed for any reasons, reroute routed children */
1460 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1461 while (iterator
->iterate(iterator
, (void**)&child
))
1463 if (child
->get_state(child
) == CHILD_ROUTED
)
1469 iterator
->destroy(iterator
);
1474 /* move routed children to a new IKE_SA, apply connection info */
1475 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1476 charon
->ike_sa_manager
, TRUE
);
1477 set_peer_cfg(new, this->peer_cfg
);
1478 new->other_host
->destroy(new->other_host
);
1479 new->other_host
= this->other_host
->clone(this->other_host
);
1480 if (!has_condition(this, COND_NAT_THERE
))
1482 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1484 if (this->my_virtual_ip
)
1486 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1488 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1489 while (iterator
->iterate(iterator
, (void**)&child
))
1491 if (child
->get_state(child
) == CHILD_ROUTED
)
1493 route(new, child
->get_config(child
));
1496 iterator
->destroy(iterator
);
1497 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1503 * Implementation of ike_sa_t.get_prf.
1505 static prf_t
*get_prf(private_ike_sa_t
*this)
1511 * Implementation of ike_sa_t.get_prf.
1513 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1515 return this->child_prf
;
1519 * Implementation of ike_sa_t.get_skp_bild
1521 static chunk_t
get_skp_build(private_ike_sa_t
*this)
1523 return this->skp_build
;
1527 * Implementation of ike_sa_t.get_skp_verify
1529 static chunk_t
get_skp_verify(private_ike_sa_t
*this)
1531 return this->skp_verify
;
1535 * Implementation of ike_sa_t.get_id.
1537 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1539 return this->ike_sa_id
;
1543 * Implementation of ike_sa_t.get_my_id.
1545 static identification_t
* get_my_id(private_ike_sa_t
*this)
1551 * Implementation of ike_sa_t.set_my_id.
1553 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1555 DESTROY_IF(this->my_id
);
1560 * Implementation of ike_sa_t.get_other_id.
1562 static identification_t
* get_other_id(private_ike_sa_t
*this)
1564 return this->other_id
;
1568 * Implementation of ike_sa_t.set_other_id.
1570 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1572 DESTROY_IF(this->other_id
);
1573 this->other_id
= other
;
1577 * Implementation of ike_sa_t.get_eap_identity.
1579 static identification_t
* get_eap_identity(private_ike_sa_t
*this)
1581 return this->eap_identity
;
1585 * Implementation of ike_sa_t.set_eap_identity.
1587 static void set_eap_identity(private_ike_sa_t
*this, identification_t
*id
)
1589 DESTROY_IF(this->eap_identity
);
1590 this->eap_identity
= id
;
1594 * Implementation of ike_sa_t.derive_keys.
1596 static status_t
derive_keys(private_ike_sa_t
*this,
1597 proposal_t
*proposal
, chunk_t secret
,
1598 chunk_t nonce_i
, chunk_t nonce_r
,
1599 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1601 prf_plus_t
*prf_plus
;
1602 chunk_t skeyseed
, key
, full_nonce
, fixed_nonce
, prf_plus_seed
;
1603 u_int16_t alg
, key_size
;
1604 crypter_t
*crypter_i
, *crypter_r
;
1605 signer_t
*signer_i
, *signer_r
;
1606 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1607 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1608 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1610 /* Create SAs general purpose PRF first, we may use it here */
1611 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &alg
, NULL
))
1613 DBG1(DBG_IKE
, "no %N selected",
1614 transform_type_names
, PSEUDO_RANDOM_FUNCTION
);
1617 this->prf
= lib
->crypto
->create_prf(lib
->crypto
, alg
);
1618 if (this->prf
== NULL
)
1620 DBG1(DBG_IKE
, "%N %N not supported!",
1621 transform_type_names
, PSEUDO_RANDOM_FUNCTION
,
1622 pseudo_random_function_names
, alg
);
1625 DBG4(DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1626 /* full nonce is used as seed for PRF+ ... */
1627 full_nonce
= chunk_cat("cc", nonce_i
, nonce_r
);
1628 /* but the PRF may need a fixed key which only uses the first bytes of
1632 case PRF_AES128_XCBC
:
1633 /* while rfc4434 defines variable keys for AES-XCBC, rfc3664 does
1634 * not and therefore fixed key semantics apply to XCBC for key
1636 nonce_i
.len
= min(nonce_i
.len
, this->prf
->get_key_size(this->prf
)/2);
1637 nonce_r
.len
= min(nonce_r
.len
, this->prf
->get_key_size(this->prf
)/2);
1640 /* all other algorithms use variable key length, full nonce */
1643 fixed_nonce
= chunk_cat("cc", nonce_i
, nonce_r
);
1644 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1645 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1646 prf_plus_seed
= chunk_cat("ccc", full_nonce
, spi_i
, spi_r
);
1648 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1650 * if we are rekeying, SKEYSEED is built on another way
1652 if (child_prf
== NULL
) /* not rekeying */
1654 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1655 this->prf
->set_key(this->prf
, fixed_nonce
);
1656 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1657 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1658 this->prf
->set_key(this->prf
, skeyseed
);
1659 chunk_clear(&skeyseed
);
1660 chunk_clear(&secret
);
1661 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1665 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1666 * use OLD SAs PRF functions for both prf_plus and prf */
1667 secret
= chunk_cat("mc", secret
, full_nonce
);
1668 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1669 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1670 old_prf
->set_key(old_prf
, skeyseed
);
1671 chunk_clear(&skeyseed
);
1672 chunk_clear(&secret
);
1673 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1675 chunk_free(&full_nonce
);
1676 chunk_free(&fixed_nonce
);
1677 chunk_clear(&prf_plus_seed
);
1679 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1681 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1682 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &alg
, NULL
);
1683 this->child_prf
= lib
->crypto
->create_prf(lib
->crypto
, alg
);
1684 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1685 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1686 DBG4(DBG_IKE
, "Sk_d secret %B", &key
);
1687 this->child_prf
->set_key(this->child_prf
, key
);
1690 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1691 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &alg
, NULL
))
1693 DBG1(DBG_IKE
, "no %N selected",
1694 transform_type_names
, INTEGRITY_ALGORITHM
);
1697 signer_i
= lib
->crypto
->create_signer(lib
->crypto
, alg
);
1698 signer_r
= lib
->crypto
->create_signer(lib
->crypto
, alg
);
1699 if (signer_i
== NULL
|| signer_r
== NULL
)
1701 DBG1(DBG_IKE
, "%N %N not supported!",
1702 transform_type_names
, INTEGRITY_ALGORITHM
,
1703 integrity_algorithm_names
,alg
);
1704 prf_plus
->destroy(prf_plus
);
1707 key_size
= signer_i
->get_key_size(signer_i
);
1709 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1710 DBG4(DBG_IKE
, "Sk_ai secret %B", &key
);
1711 signer_i
->set_key(signer_i
, key
);
1714 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1715 DBG4(DBG_IKE
, "Sk_ar secret %B", &key
);
1716 signer_r
->set_key(signer_r
, key
);
1721 this->signer_in
= signer_r
;
1722 this->signer_out
= signer_i
;
1726 this->signer_in
= signer_i
;
1727 this->signer_out
= signer_r
;
1730 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1731 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &alg
, &key_size
))
1733 DBG1(DBG_IKE
, "no %N selected",
1734 transform_type_names
, ENCRYPTION_ALGORITHM
);
1735 prf_plus
->destroy(prf_plus
);
1738 crypter_i
= lib
->crypto
->create_crypter(lib
->crypto
, alg
, key_size
/ 8);
1739 crypter_r
= lib
->crypto
->create_crypter(lib
->crypto
, alg
, key_size
/ 8);
1740 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1742 DBG1(DBG_IKE
, "%N %N (key size %d) not supported!",
1743 transform_type_names
, ENCRYPTION_ALGORITHM
,
1744 encryption_algorithm_names
, alg
, key_size
);
1745 prf_plus
->destroy(prf_plus
);
1748 key_size
= crypter_i
->get_key_size(crypter_i
);
1750 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1751 DBG4(DBG_IKE
, "Sk_ei secret %B", &key
);
1752 crypter_i
->set_key(crypter_i
, key
);
1755 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1756 DBG4(DBG_IKE
, "Sk_er secret %B", &key
);
1757 crypter_r
->set_key(crypter_r
, key
);
1762 this->crypter_in
= crypter_r
;
1763 this->crypter_out
= crypter_i
;
1767 this->crypter_in
= crypter_i
;
1768 this->crypter_out
= crypter_r
;
1771 /* SK_pi/SK_pr used for authentication => stored for later */
1772 key_size
= this->prf
->get_key_size(this->prf
);
1773 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1774 DBG4(DBG_IKE
, "Sk_pi secret %B", &key
);
1777 this->skp_build
= key
;
1781 this->skp_verify
= key
;
1783 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1784 DBG4(DBG_IKE
, "Sk_pr secret %B", &key
);
1787 this->skp_verify
= key
;
1791 this->skp_build
= key
;
1794 /* all done, prf_plus not needed anymore */
1795 prf_plus
->destroy(prf_plus
);
1801 * Implementation of ike_sa_t.get_proposal.
1803 static char* get_proposal(private_ike_sa_t
*this)
1805 return this->selected_proposal
;
1809 * Implementation of ike_sa_t.set_proposal.
1811 static void set_proposal(private_ike_sa_t
*this, char *proposal
)
1813 free(this->selected_proposal
);
1814 this->selected_proposal
= strdup(proposal
);
1818 * Implementation of ike_sa_t.add_child_sa.
1820 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1822 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1826 * Implementation of ike_sa_t.get_child_sa.
1828 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1829 u_int32_t spi
, bool inbound
)
1831 iterator_t
*iterator
;
1832 child_sa_t
*current
, *found
= NULL
;
1834 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1835 while (iterator
->iterate(iterator
, (void**)¤t
))
1837 if (current
->get_spi(current
, inbound
) == spi
&&
1838 current
->get_protocol(current
) == protocol
)
1843 iterator
->destroy(iterator
);
1848 * Implementation of ike_sa_t.create_child_sa_iterator.
1850 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1852 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1856 * Implementation of ike_sa_t.rekey_child_sa.
1858 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1860 child_sa_t
*child_sa
;
1861 child_rekey_t
*child_rekey
;
1863 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1866 child_rekey
= child_rekey_create(&this->public, child_sa
);
1867 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1868 return this->task_manager
->initiate(this->task_manager
);
1874 * Implementation of ike_sa_t.delete_child_sa.
1876 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1878 child_sa_t
*child_sa
;
1879 child_delete_t
*child_delete
;
1881 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1884 child_delete
= child_delete_create(&this->public, child_sa
);
1885 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1886 return this->task_manager
->initiate(this->task_manager
);
1892 * Implementation of ike_sa_t.destroy_child_sa.
1894 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1897 iterator_t
*iterator
;
1898 child_sa_t
*child_sa
;
1899 status_t status
= NOT_FOUND
;
1901 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1902 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1904 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1905 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1907 child_sa
->destroy(child_sa
);
1908 iterator
->remove(iterator
);
1913 iterator
->destroy(iterator
);
1918 * Implementation of public_ike_sa_t.delete.
1920 static status_t
delete_(private_ike_sa_t
*this)
1922 ike_delete_t
*ike_delete
;
1924 switch (this->state
)
1926 case IKE_ESTABLISHED
:
1928 ike_delete
= ike_delete_create(&this->public, TRUE
);
1929 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1930 return this->task_manager
->initiate(this->task_manager
);
1932 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1935 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1936 "without notification", ike_sa_state_names
, this->state
);
1943 * Implementation of ike_sa_t.rekey.
1945 static status_t
rekey(private_ike_sa_t
*this)
1947 ike_rekey_t
*ike_rekey
;
1949 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1951 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1952 return this->task_manager
->initiate(this->task_manager
);
1956 * Implementation of ike_sa_t.reauth
1958 static status_t
reauth(private_ike_sa_t
*this)
1962 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1963 * If the peer does not support RFC4478, there is no way to keep the
1965 if (!this->ike_initiator
)
1967 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1968 if (this->other_virtual_ip
!= NULL
||
1969 has_condition(this, COND_EAP_AUTHENTICATED
)
1971 /* if we are mediation server we too cannot reauth the IKE_SA */
1972 || this->is_mediation_server
1976 time_t now
= time(NULL
);
1978 DBG1(DBG_IKE
, "IKE_SA will timeout in %#V", &now
, &this->time
.delete);
1983 DBG1(DBG_IKE
, "reauthenticating actively");
1986 task
= (task_t
*)ike_reauth_create(&this->public);
1987 this->task_manager
->queue_task(this->task_manager
, task
);
1989 return this->task_manager
->initiate(this->task_manager
);
1993 * Implementation of ike_sa_t.reestablish
1995 static status_t
reestablish(private_ike_sa_t
*this)
2000 iterator_t
*iterator
;
2001 child_sa_t
*child_sa
;
2002 child_cfg_t
*child_cfg
;
2003 bool required
= FALSE
;
2004 status_t status
= FAILED
;
2006 /* check if we have children to keep up at all*/
2007 iterator
= create_child_sa_iterator(this);
2008 while (iterator
->iterate(iterator
, (void**)&child_sa
))
2010 child_cfg
= child_sa
->get_config(child_sa
);
2011 if (this->state
== IKE_DELETING
)
2013 action
= child_cfg
->get_close_action(child_cfg
);
2017 action
= child_cfg
->get_dpd_action(child_cfg
);
2021 case ACTION_RESTART
:
2028 iterator
->destroy(iterator
);
2030 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
2031 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
2041 /* check if we are able to reestablish this IKE_SA */
2042 if (!this->ike_initiator
&&
2043 (this->other_virtual_ip
!= NULL
||
2044 has_condition(this, COND_EAP_AUTHENTICATED
)
2046 || this->is_mediation_server
2050 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due asymetric setup");
2054 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
, TRUE
);
2055 new->set_peer_cfg(new, this->peer_cfg
);
2056 host
= this->other_host
;
2057 new->set_other_host(new, host
->clone(host
));
2058 host
= this->my_host
;
2059 new->set_my_host(new, host
->clone(host
));
2060 /* if we already have a virtual IP, we reuse it */
2061 host
= this->my_virtual_ip
;
2064 new->set_virtual_ip(new, TRUE
, host
);
2068 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
2070 status
= new->initiate(new, NULL
);
2075 iterator
= create_child_sa_iterator(this);
2076 while (iterator
->iterate(iterator
, (void**)&child_sa
))
2078 child_cfg
= child_sa
->get_config(child_sa
);
2079 if (this->state
== IKE_DELETING
)
2081 action
= child_cfg
->get_close_action(child_cfg
);
2085 action
= child_cfg
->get_dpd_action(child_cfg
);
2089 case ACTION_RESTART
:
2090 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
2091 child_cfg
->get_name(child_cfg
));
2092 child_cfg
->get_ref(child_cfg
);
2093 status
= new->initiate(new, child_cfg
);
2096 status
= new->route(new, child_cfg
);
2101 if (status
== DESTROY_ME
)
2106 iterator
->destroy(iterator
);
2109 if (status
== DESTROY_ME
)
2111 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
2116 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
2122 * Implementation of ike_sa_t.retransmit.
2124 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
2126 this->time
.outbound
= time(NULL
);
2127 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
2129 /* send a proper signal to brief interested bus listeners */
2130 switch (this->state
)
2132 case IKE_CONNECTING
:
2134 /* retry IKE_SA_INIT if we have multiple keyingtries */
2135 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
2137 if (tries
== 0 || tries
> this->keyingtry
)
2139 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
2140 this->keyingtry
+ 1, tries
);
2142 return this->task_manager
->initiate(this->task_manager
);
2144 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
2148 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
2151 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
2163 * Implementation of ike_sa_t.set_auth_lifetime.
2165 static void set_auth_lifetime(private_ike_sa_t
*this, u_int32_t lifetime
)
2167 u_int32_t reduction
= this->peer_cfg
->get_over_time(this->peer_cfg
);
2168 u_int32_t reauth_time
= time(NULL
) + lifetime
- reduction
;
2170 if (lifetime
< reduction
)
2172 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, starting reauthentication",
2174 charon
->processor
->queue_job(charon
->processor
,
2175 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
2177 else if (this->time
.reauth
== 0 || this->time
.reauth
> reauth_time
)
2179 this->time
.reauth
= reauth_time
;
2180 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
2181 " in %ds", lifetime
, lifetime
- reduction
);
2182 charon
->scheduler
->schedule_job(charon
->scheduler
,
2183 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2184 (lifetime
- reduction
) * 1000);
2188 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, reauthentication already "
2189 "scheduled in %ds", lifetime
, this->time
.reauth
- time(NULL
));
2194 * Implementation of ike_sa_t.roam.
2196 static status_t
roam(private_ike_sa_t
*this, bool address
)
2199 ike_mobike_t
*mobike
;
2201 switch (this->state
)
2209 /* responder just updates the peer about changed address config */
2210 if (!this->ike_sa_id
->is_initiator(this->ike_sa_id
))
2212 if (supports_extension(this, EXT_MOBIKE
) && address
)
2214 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
2215 mobike
= ike_mobike_create(&this->public, TRUE
);
2216 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
2217 return this->task_manager
->initiate(this->task_manager
);
2222 /* keep existing path if possible */
2223 src
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
2224 this->other_host
, this->my_host
);
2227 if (src
->ip_equals(src
, this->my_host
))
2229 DBG2(DBG_IKE
, "keeping connection path %H - %H",
2230 src
, this->other_host
);
2237 /* update addresses with mobike, if supported ... */
2238 if (supports_extension(this, EXT_MOBIKE
))
2240 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
2241 mobike
= ike_mobike_create(&this->public, TRUE
);
2242 mobike
->roam(mobike
, address
);
2243 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
2244 return this->task_manager
->initiate(this->task_manager
);
2246 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
2247 /* ... reauth if not */
2248 return reauth(this);
2252 * Implementation of ike_sa_t.inherit.
2254 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
2256 child_sa_t
*child_sa
;
2259 /* apply hosts and ids */
2260 this->my_host
->destroy(this->my_host
);
2261 this->other_host
->destroy(this->other_host
);
2262 this->my_id
->destroy(this->my_id
);
2263 this->other_id
->destroy(this->other_id
);
2264 this->my_host
= other
->my_host
->clone(other
->my_host
);
2265 this->other_host
= other
->other_host
->clone(other
->other_host
);
2266 this->my_id
= other
->my_id
->clone(other
->my_id
);
2267 this->other_id
= other
->other_id
->clone(other
->other_id
);
2268 this->ike_initiator
= other
->ike_initiator
;
2270 /* apply virtual assigned IPs... */
2271 if (other
->my_virtual_ip
)
2273 this->my_virtual_ip
= other
->my_virtual_ip
;
2274 other
->my_virtual_ip
= NULL
;
2276 if (other
->other_virtual_ip
)
2278 this->other_virtual_ip
= other
->other_virtual_ip
;
2279 other
->other_virtual_ip
= NULL
;
2282 /* ... and DNS servers */
2283 while (other
->dns_servers
->remove_last(other
->dns_servers
,
2284 (void**)&ip
) == SUCCESS
)
2286 this->dns_servers
->insert_first(this->dns_servers
, ip
);
2289 /* inherit NAT-T conditions */
2290 this->conditions
= other
->conditions
;
2291 if (this->conditions
& COND_NAT_HERE
)
2293 send_keepalive(this);
2297 if (other
->is_mediation_server
)
2299 act_as_mediation_server(this);
2301 else if (other
->server_reflexive_host
)
2303 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
2304 other
->server_reflexive_host
);
2308 /* adopt all children */
2309 while (other
->child_sas
->remove_last(other
->child_sas
,
2310 (void**)&child_sa
) == SUCCESS
)
2312 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2315 /* move pending tasks to the new IKE_SA */
2316 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2318 /* reauthentication timeout survives a rekeying */
2319 if (other
->time
.reauth
)
2321 time_t reauth
, delete, now
= time(NULL
);
2323 this->time
.reauth
= other
->time
.reauth
;
2324 reauth
= this->time
.reauth
- now
;
2325 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
2326 this->time
.delete = this->time
.reauth
+ delete;
2327 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
2328 "lifetime reduced to %ds", reauth
, delete);
2329 charon
->scheduler
->schedule_job(charon
->scheduler
,
2330 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2332 charon
->scheduler
->schedule_job(charon
->scheduler
,
2333 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2336 /* we have to initate here, there may be new tasks to handle */
2337 return this->task_manager
->initiate(this->task_manager
);
2341 * Implementation of ike_sa_t.remove_dns_server
2343 static void remove_dns_servers(private_ike_sa_t
*this)
2347 chunk_t contents
, line
, orig_line
, token
;
2348 char string
[INET6_ADDRSTRLEN
];
2350 iterator_t
*iterator
;
2352 if (this->dns_servers
->get_count(this->dns_servers
) == 0)
2354 /* don't touch anything if we have no nameservers installed */
2358 file
= fopen(RESOLV_CONF
, "r");
2359 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2361 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2362 RESOLV_CONF
, strerror(errno
));
2366 contents
= chunk_alloca((size_t)stats
.st_size
);
2368 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2370 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2376 file
= fopen(RESOLV_CONF
, "w");
2379 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2380 RESOLV_CONF
, strerror(errno
));
2384 iterator
= this->dns_servers
->create_iterator(this->dns_servers
, TRUE
);
2385 while (fetchline(&contents
, &line
))
2389 if (extract_token(&token
, ' ', &line
) &&
2390 strncasecmp(token
.ptr
, "nameserver", token
.len
) == 0)
2392 if (!extract_token(&token
, ' ', &line
))
2396 iterator
->reset(iterator
);
2397 while (iterator
->iterate(iterator
, (void**)&ip
))
2399 snprintf(string
, sizeof(string
), "%H", ip
);
2400 if (strlen(string
) == token
.len
&&
2401 strncmp(token
.ptr
, string
, token
.len
) == 0)
2403 iterator
->remove(iterator
);
2413 /* write line untouched back to file */
2414 fwrite(orig_line
.ptr
, orig_line
.len
, 1, file
);
2415 fprintf(file
, "\n");
2418 iterator
->destroy(iterator
);
2423 * Implementation of ike_sa_t.add_dns_server
2425 static void add_dns_server(private_ike_sa_t
*this, host_t
*dns
)
2431 DBG1(DBG_IKE
, "installing DNS server %H", dns
);
2433 file
= fopen(RESOLV_CONF
, "a+");
2434 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2436 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2437 RESOLV_CONF
, strerror(errno
));
2441 contents
= chunk_alloca(stats
.st_size
);
2443 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2445 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2451 file
= fopen(RESOLV_CONF
, "w");
2454 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2455 RESOLV_CONF
, strerror(errno
));
2459 if (fprintf(file
, "nameserver %H # added by strongSwan, assigned by %D\n",
2460 dns
, this->other_id
) < 0)
2462 DBG1(DBG_IKE
, "unable to write DNS configuration: %s", strerror(errno
));
2466 this->dns_servers
->insert_last(this->dns_servers
, dns
->clone(dns
));
2468 fwrite(contents
.ptr
, contents
.len
, 1, file
);
2474 * Implementation of ike_sa_t.destroy.
2476 static void destroy(private_ike_sa_t
*this)
2478 set_state(this, IKE_DESTROYING
);
2480 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2482 /* unset SA after here to avoid usage by the listeners */
2483 charon
->bus
->set_sa(charon
->bus
, NULL
);
2485 this->task_manager
->destroy(this->task_manager
);
2487 DESTROY_IF(this->crypter_in
);
2488 DESTROY_IF(this->crypter_out
);
2489 DESTROY_IF(this->signer_in
);
2490 DESTROY_IF(this->signer_out
);
2491 DESTROY_IF(this->prf
);
2492 DESTROY_IF(this->child_prf
);
2493 chunk_free(&this->skp_verify
);
2494 chunk_free(&this->skp_build
);
2495 free(this->selected_proposal
);
2497 if (this->my_virtual_ip
)
2499 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
2500 this->my_virtual_ip
);
2501 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
2503 if (this->other_virtual_ip
)
2505 if (this->peer_cfg
&& this->peer_cfg
->get_pool(this->peer_cfg
))
2507 charon
->attributes
->release_address(charon
->attributes
,
2508 this->peer_cfg
->get_pool(this->peer_cfg
),
2509 this->other_virtual_ip
);
2511 this->other_virtual_ip
->destroy(this->other_virtual_ip
);
2514 remove_dns_servers(this);
2515 this->dns_servers
->destroy_offset(this->dns_servers
,
2516 offsetof(host_t
, destroy
));
2517 this->additional_addresses
->destroy_offset(this->additional_addresses
,
2518 offsetof(host_t
, destroy
));
2520 if (this->is_mediation_server
)
2522 charon
->mediation_manager
->remove(charon
->mediation_manager
, this->ike_sa_id
);
2524 DESTROY_IF(this->server_reflexive_host
);
2525 chunk_free(&this->connect_id
);
2527 free(this->nat_detection_dest
.ptr
);
2529 DESTROY_IF(this->my_host
);
2530 DESTROY_IF(this->other_host
);
2531 DESTROY_IF(this->my_id
);
2532 DESTROY_IF(this->other_id
);
2533 DESTROY_IF(this->eap_identity
);
2535 DESTROY_IF(this->ike_cfg
);
2536 DESTROY_IF(this->peer_cfg
);
2537 DESTROY_IF(this->my_auth
);
2538 DESTROY_IF(this->other_auth
);
2540 this->ike_sa_id
->destroy(this->ike_sa_id
);
2545 * Described in header.
2547 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2549 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2550 static u_int32_t unique_id
= 0;
2552 /* Public functions */
2553 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
2554 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2555 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
2556 this->public.get_statistic
= (u_int32_t(*)(ike_sa_t
*, statistic_t kind
))get_statistic
;
2557 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
2558 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
2559 this->public.route
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) route
;
2560 this->public.unroute
= (status_t (*)(ike_sa_t
*,u_int32_t
)) unroute
;
2561 this->public.acquire
= (status_t (*)(ike_sa_t
*,u_int32_t
)) acquire
;
2562 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
2563 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
2564 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
2565 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
2566 this->public.get_my_auth
= (auth_info_t
*(*)(ike_sa_t
*))get_my_auth
;
2567 this->public.get_other_auth
= (auth_info_t
*(*)(ike_sa_t
*))get_other_auth
;
2568 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
2569 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
2570 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
2571 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
2572 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
2573 this->public.update_hosts
= (void(*)(ike_sa_t
*, host_t
*me
, host_t
*other
))update_hosts
;
2574 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
2575 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2576 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
2577 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2578 this->public.get_eap_identity
= (identification_t
* (*)(ike_sa_t
*)) get_eap_identity
;
2579 this->public.set_eap_identity
= (void (*)(ike_sa_t
*,identification_t
*)) set_eap_identity
;
2580 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
2581 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
2582 this->public.set_condition
= (void (*)(ike_sa_t
*, ike_condition_t
,bool)) set_condition
;
2583 this->public.has_condition
= (bool (*)(ike_sa_t
*,ike_condition_t
)) has_condition
;
2584 this->public.set_pending_updates
= (void(*)(ike_sa_t
*, u_int32_t updates
))set_pending_updates
;
2585 this->public.get_pending_updates
= (u_int32_t(*)(ike_sa_t
*))get_pending_updates
;
2586 this->public.is_ike_initiator
= (bool (*)(ike_sa_t
*))is_ike_initiator
;
2587 this->public.create_additional_address_iterator
= (iterator_t
*(*)(ike_sa_t
*))create_additional_address_iterator
;
2588 this->public.add_additional_address
= (void(*)(ike_sa_t
*, host_t
*host
))add_additional_address
;
2589 this->public.has_mapping_changed
= (bool(*)(ike_sa_t
*, chunk_t hash
))has_mapping_changed
;
2590 this->public.retransmit
= (status_t (*)(ike_sa_t
*, u_int32_t
)) retransmit
;
2591 this->public.delete = (status_t (*)(ike_sa_t
*))delete_
;
2592 this->public.destroy
= (void (*)(ike_sa_t
*))destroy
;
2593 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
2594 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
2595 this->public.get_prf
= (prf_t
* (*)(ike_sa_t
*)) get_prf
;
2596 this->public.get_child_prf
= (prf_t
* (*)(ike_sa_t
*)) get_child_prf
;
2597 this->public.get_skp_verify
= (chunk_t (*)(ike_sa_t
*)) get_skp_verify
;
2598 this->public.get_skp_build
= (chunk_t (*)(ike_sa_t
*)) get_skp_build
;
2599 this->public.derive_keys
= (status_t (*)(ike_sa_t
*,proposal_t
*,chunk_t
,chunk_t
,chunk_t
,bool,prf_t
*,prf_t
*)) derive_keys
;
2600 this->public.get_proposal
= (char* (*)(ike_sa_t
*)) get_proposal
;
2601 this->public.set_proposal
= (void (*)(ike_sa_t
*,char*)) set_proposal
;
2602 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2603 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2604 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2605 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2606 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2607 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2608 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
2609 this->public.reauth
= (status_t (*)(ike_sa_t
*))reauth
;
2610 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
2611 this->public.set_auth_lifetime
= (void(*)(ike_sa_t
*, u_int32_t lifetime
))set_auth_lifetime
;
2612 this->public.roam
= (status_t(*)(ike_sa_t
*,bool))roam
;
2613 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
2614 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
2615 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
2616 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
2617 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
2618 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
2619 this->public.add_dns_server
= (void (*)(ike_sa_t
*,host_t
*))add_dns_server
;
2621 this->public.act_as_mediation_server
= (void (*)(ike_sa_t
*)) act_as_mediation_server
;
2622 this->public.get_server_reflexive_host
= (host_t
* (*)(ike_sa_t
*)) get_server_reflexive_host
;
2623 this->public.set_server_reflexive_host
= (void (*)(ike_sa_t
*,host_t
*)) set_server_reflexive_host
;
2624 this->public.get_connect_id
= (chunk_t (*)(ike_sa_t
*)) get_connect_id
;
2625 this->public.initiate_mediation
= (status_t (*)(ike_sa_t
*,peer_cfg_t
*)) initiate_mediation
;
2626 this->public.initiate_mediated
= (status_t (*)(ike_sa_t
*,host_t
*,host_t
*,chunk_t
)) initiate_mediated
;
2627 this->public.relay
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
,chunk_t
,linked_list_t
*,bool)) relay
;
2628 this->public.callback
= (status_t (*)(ike_sa_t
*,identification_t
*)) callback
;
2629 this->public.respond
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
)) respond
;
2632 /* initialize private fields */
2633 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2634 this->child_sas
= linked_list_create();
2635 this->my_host
= host_create_from_string("0.0.0.0", IKEV2_UDP_PORT
);
2636 this->other_host
= host_create_from_string("0.0.0.0", IKEV2_UDP_PORT
);
2637 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2638 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2639 this->eap_identity
= NULL
;
2640 this->extensions
= 0;
2641 this->conditions
= 0;
2642 this->selected_proposal
= NULL
;
2643 this->crypter_in
= NULL
;
2644 this->crypter_out
= NULL
;
2645 this->signer_in
= NULL
;
2646 this->signer_out
= NULL
;
2648 this->skp_verify
= chunk_empty
;
2649 this->skp_build
= chunk_empty
;
2650 this->child_prf
= NULL
;
2651 this->state
= IKE_CREATED
;
2652 this->keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2653 "charon.keep_alive", KEEPALIVE_INTERVAL
);
2654 this->time
.inbound
= this->time
.outbound
= time(NULL
);
2655 this->time
.established
= 0;
2656 this->time
.rekey
= 0;
2657 this->time
.reauth
= 0;
2658 this->time
.delete = 0;
2659 this->ike_cfg
= NULL
;
2660 this->peer_cfg
= NULL
;
2661 this->my_auth
= auth_info_create();
2662 this->other_auth
= auth_info_create();
2663 this->task_manager
= task_manager_create(&this->public);
2664 this->unique_id
= ++unique_id
;
2665 this->my_virtual_ip
= NULL
;
2666 this->other_virtual_ip
= NULL
;
2667 this->dns_servers
= linked_list_create();
2668 this->additional_addresses
= linked_list_create();
2669 this->nat_detection_dest
= chunk_empty
;
2670 this->pending_updates
= 0;
2671 this->keyingtry
= 0;
2672 this->ike_initiator
= FALSE
;
2674 this->is_mediation_server
= FALSE
;
2675 this->server_reflexive_host
= NULL
;
2676 this->connect_id
= chunk_empty
;
2679 return &this->public;