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_DELETING
,
83 typedef struct private_ike_sa_t private_ike_sa_t
;
86 * Private data of an ike_sa_t object.
88 struct private_ike_sa_t
{
96 * Identifier for the current IKE_SA.
98 ike_sa_id_t
*ike_sa_id
;
101 * unique numerical ID for this IKE_SA.
106 * Current state of the IKE_SA
108 ike_sa_state_t state
;
111 * IKE configuration used to set up this IKE_SA
116 * Peer and authentication information to establish IKE_SA.
118 peer_cfg_t
*peer_cfg
;
121 * associated authentication/authorization info for local peer
123 auth_info_t
*my_auth
;
126 * associated authentication/authorization info for remote peer
128 auth_info_t
*other_auth
;
131 * Juggles tasks to process messages
133 task_manager_t
*task_manager
;
136 * Address of local host
141 * Address of remote host
147 * Are we mediation server
149 bool is_mediation_server
;
152 * Server reflexive host
154 host_t
*server_reflexive_host
;
163 * Identification used for us
165 identification_t
*my_id
;
168 * Identification used for other
170 identification_t
*other_id
;
173 * set of extensions the peer supports
175 ike_extension_t extensions
;
178 * set of condition flags currently enabled for this IKE_SA
180 ike_condition_t conditions
;
183 * Linked List containing the child sa's of the current IKE_SA.
185 linked_list_t
*child_sas
;
188 * crypter for inbound traffic
190 crypter_t
*crypter_in
;
193 * crypter for outbound traffic
195 crypter_t
*crypter_out
;
198 * Signer for inbound traffic
203 * Signer for outbound traffic
205 signer_t
*signer_out
;
208 * Multi purpose prf, set key, use it, forget it
213 * Prf function for derivating keymat child SAs
218 * Key to build outging authentication data (SKp)
223 * Key to verify incoming authentication data (SKp)
228 * Virtual IP on local host, if any
230 host_t
*my_virtual_ip
;
233 * Virtual IP on remote host, if any
235 host_t
*other_virtual_ip
;
238 * List of DNS servers installed by us
240 linked_list_t
*dns_servers
;
243 * list of peers additional addresses, transmitted via MOBIKE
245 linked_list_t
*additional_addresses
;
248 * number pending UPDATE_SA_ADDRESS (MOBIKE)
250 u_int32_t pending_updates
;
253 * Timestamps for this IKE_SA
256 /** last IKE message received */
258 /** last IKE message sent */
260 /** when IKE_SA became established */
261 u_int32_t established
;
262 /** when IKE_SA gets rekeyed */
264 /** when IKE_SA gets reauthenticated */
266 /** when IKE_SA gets deleted */
271 * how many times we have retried so far (keyingtries)
276 * are we the initiator of this IKE_SA (rekeying does not affect this flag)
282 * get the time of the latest traffic processed by the kernel
284 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
286 iterator_t
*iterator
;
287 child_sa_t
*child_sa
;
288 time_t latest
= 0, use_time
;
290 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
291 while (iterator
->iterate(iterator
, (void**)&child_sa
))
293 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
295 latest
= max(latest
, use_time
);
298 iterator
->destroy(iterator
);
302 return max(this->time
.inbound
, latest
);
306 return max(this->time
.outbound
, latest
);
311 * Implementation of ike_sa_t.get_unique_id
313 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
315 return this->unique_id
;
319 * Implementation of ike_sa_t.get_name.
321 static char *get_name(private_ike_sa_t
*this)
325 return this->peer_cfg
->get_name(this->peer_cfg
);
331 * Implementation of ike_sa_t.get_statistic.
333 static u_int32_t
get_statistic(private_ike_sa_t
*this, statistic_t kind
)
335 time_t now
= time(NULL
);
339 case STAT_REKEY_TIME
:
340 if (this->time
.rekey
> now
)
342 return this->time
.rekey
- now
;
345 case STAT_REAUTH_TIME
:
346 if (this->time
.reauth
> now
)
348 return this->time
.reauth
- now
;
358 * Implementation of ike_sa_t.get_my_host.
360 static host_t
*get_my_host(private_ike_sa_t
*this)
362 return this->my_host
;
366 * Implementation of ike_sa_t.set_my_host.
368 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
370 DESTROY_IF(this->my_host
);
375 * Implementation of ike_sa_t.get_other_host.
377 static host_t
*get_other_host(private_ike_sa_t
*this)
379 return this->other_host
;
383 * Implementation of ike_sa_t.set_other_host.
385 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
387 DESTROY_IF(this->other_host
);
388 this->other_host
= other
;
392 * Implementation of ike_sa_t.get_peer_cfg
394 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
396 return this->peer_cfg
;
400 * Implementation of ike_sa_t.set_peer_cfg
402 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
404 DESTROY_IF(this->peer_cfg
);
405 peer_cfg
->get_ref(peer_cfg
);
406 this->peer_cfg
= peer_cfg
;
408 if (this->ike_cfg
== NULL
)
410 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
411 this->ike_cfg
->get_ref(this->ike_cfg
);
414 /* apply values, so we are ready to initate/acquire */
415 if (this->my_host
->is_anyaddr(this->my_host
))
417 host_t
*me
= this->ike_cfg
->get_my_host(this->ike_cfg
);
418 set_my_host(this, me
->clone(me
));
420 if (this->other_host
->is_anyaddr(this->other_host
))
422 host_t
*other
= this->ike_cfg
->get_other_host(this->ike_cfg
);
423 set_other_host(this, other
->clone(other
));
425 /* apply IDs if they are not already set */
426 if (this->my_id
->contains_wildcards(this->my_id
))
428 DESTROY_IF(this->my_id
);
429 this->my_id
= this->peer_cfg
->get_my_id(this->peer_cfg
);
430 this->my_id
= this->my_id
->clone(this->my_id
);
432 if (this->other_id
->contains_wildcards(this->other_id
))
434 DESTROY_IF(this->other_id
);
435 this->other_id
= this->peer_cfg
->get_other_id(this->peer_cfg
);
436 this->other_id
= this->other_id
->clone(this->other_id
);
441 * Implementation of ike_sa_t.get_my_auth.
443 static auth_info_t
* get_my_auth(private_ike_sa_t
*this)
445 return this->my_auth
;
449 * Implementation of ike_sa_t.get_other_auth.
451 static auth_info_t
* get_other_auth(private_ike_sa_t
*this)
453 return this->other_auth
;
457 * Implementation of ike_sa_t.send_keepalive
459 static void send_keepalive(private_ike_sa_t
*this)
461 send_keepalive_job_t
*job
;
462 time_t last_out
, now
, diff
;
464 if (!(this->conditions
& COND_NAT_HERE
))
465 { /* disable keep alives if we are not NATed anymore */
469 last_out
= get_use_time(this, FALSE
);
472 diff
= now
- last_out
;
474 if (diff
>= KEEPALIVE_INTERVAL
)
479 packet
= packet_create();
480 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
481 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
482 data
.ptr
= malloc(1);
485 packet
->set_data(packet
, data
);
486 DBG1(DBG_IKE
, "sending keep alive");
487 charon
->sender
->send(charon
->sender
, packet
);
490 job
= send_keepalive_job_create(this->ike_sa_id
);
491 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
492 (KEEPALIVE_INTERVAL
- diff
) * 1000);
496 * Implementation of ike_sa_t.get_ike_cfg
498 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
500 return this->ike_cfg
;
504 * Implementation of ike_sa_t.set_ike_cfg
506 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
508 ike_cfg
->get_ref(ike_cfg
);
509 this->ike_cfg
= ike_cfg
;
513 * Implementation of ike_sa_t.is_ike_initiator
515 static bool is_ike_initiator(private_ike_sa_t
*this)
517 return this->ike_initiator
;
521 * Implementation of ike_sa_t.enable_extension.
523 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
525 this->extensions
|= extension
;
529 * Implementation of ike_sa_t.has_extension.
531 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
533 return (this->extensions
& extension
) != FALSE
;
537 * Implementation of ike_sa_t.has_condition.
539 static bool has_condition(private_ike_sa_t
*this, ike_condition_t condition
)
541 return (this->conditions
& condition
) != FALSE
;
545 * Implementation of ike_sa_t.enable_condition.
547 static void set_condition(private_ike_sa_t
*this, ike_condition_t condition
,
550 if (has_condition(this, condition
) != enable
)
554 this->conditions
|= condition
;
558 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
559 this->conditions
|= COND_NAT_ANY
;
560 send_keepalive(this);
563 DBG1(DBG_IKE
, "remote host is behind NAT");
564 this->conditions
|= COND_NAT_ANY
;
567 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
568 this->conditions
|= COND_NAT_ANY
;
576 this->conditions
&= ~condition
;
582 set_condition(this, COND_NAT_ANY
,
583 has_condition(this, COND_NAT_HERE
) ||
584 has_condition(this, COND_NAT_THERE
) ||
585 has_condition(this, COND_NAT_FAKE
));
595 * Implementation of ike_sa_t.send_dpd
597 static status_t
send_dpd(private_ike_sa_t
*this)
602 delay
= this->peer_cfg
->get_dpd_delay(this->peer_cfg
);
610 if (this->task_manager
->busy(this->task_manager
))
612 /* an exchange is in the air, no need to start a DPD check */
617 /* check if there was any inbound traffic */
619 last_in
= get_use_time(this, TRUE
);
621 diff
= now
- last_in
;
624 /* to long ago, initiate dead peer detection */
627 task
= (task_t
*)ike_dpd_create(TRUE
);
629 DBG1(DBG_IKE
, "sending DPD request");
631 this->task_manager
->queue_task(this->task_manager
, task
);
632 this->task_manager
->initiate(this->task_manager
);
635 /* recheck in "interval" seconds */
636 job
= send_dpd_job_create(this->ike_sa_id
);
637 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
638 (delay
- diff
) * 1000);
643 * Implementation of ike_sa_t.get_state.
645 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
651 * Implementation of ike_sa_t.set_state.
653 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
655 DBG1(DBG_IKE
, "IKE_SA '%s' state change: %N => %N",
657 ike_sa_state_names
, this->state
,
658 ike_sa_state_names
, state
);
662 case IKE_ESTABLISHED
:
664 if (this->state
== IKE_CONNECTING
)
669 /* calculate rekey, reauth and lifetime */
670 this->time
.established
= time(NULL
);
672 /* schedule rekeying if we have a time which is smaller than
673 * an already scheduled rekeying */
674 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
);
675 if (t
&& (this->time
.rekey
== 0 ||
676 (this->time
.rekey
> t
+ this->time
.established
)))
678 this->time
.rekey
= t
+ this->time
.established
;
679 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
680 charon
->scheduler
->schedule_job(charon
->scheduler
,
682 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
684 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
);
685 if (t
&& (this->time
.reauth
== 0 ||
686 (this->time
.reauth
> t
+ this->time
.established
)))
688 this->time
.reauth
= t
+ this->time
.established
;
689 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
690 charon
->scheduler
->schedule_job(charon
->scheduler
,
692 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
694 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
695 if (this->time
.rekey
|| this->time
.reauth
)
697 if (this->time
.reauth
== 0)
699 this->time
.delete = this->time
.rekey
;
701 else if (this->time
.rekey
== 0)
703 this->time
.delete = this->time
.reauth
;
707 this->time
.delete = min(this->time
.rekey
, this->time
.reauth
);
709 this->time
.delete += t
;
710 t
= this->time
.delete - this->time
.established
;
711 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
712 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
714 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
717 /* start DPD checks */
724 /* delete may fail if a packet gets lost, so set a timeout */
725 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
726 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
727 HALF_OPEN_IKE_SA_TIMEOUT
);
738 * Implementation of ike_sa_t.reset
740 static void reset(private_ike_sa_t
*this)
742 /* the responder ID is reset, as peer may choose another one */
743 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
745 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
748 set_state(this, IKE_CREATED
);
750 this->task_manager
->reset(this->task_manager
);
754 * Implementation of ike_sa_t.set_virtual_ip
756 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
760 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
761 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
762 this->my_host
) == SUCCESS
)
764 if (this->my_virtual_ip
)
766 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
767 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
768 this->my_virtual_ip
);
770 DESTROY_IF(this->my_virtual_ip
);
771 this->my_virtual_ip
= ip
->clone(ip
);
775 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
776 this->my_virtual_ip
= NULL
;
781 DESTROY_IF(this->other_virtual_ip
);
782 this->other_virtual_ip
= ip
->clone(ip
);
787 * Implementation of ike_sa_t.get_virtual_ip
789 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
793 return this->my_virtual_ip
;
797 return this->other_virtual_ip
;
802 * Implementation of ike_sa_t.add_additional_address.
804 static void add_additional_address(private_ike_sa_t
*this, host_t
*host
)
806 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
810 * Implementation of ike_sa_t.create_additional_address_iterator.
812 static iterator_t
* create_additional_address_iterator(private_ike_sa_t
*this)
814 return this->additional_addresses
->create_iterator(
815 this->additional_addresses
, TRUE
);
819 * Implementation of ike_sa_t.set_pending_updates.
821 static void set_pending_updates(private_ike_sa_t
*this, u_int32_t updates
)
823 this->pending_updates
= updates
;
827 * Implementation of ike_sa_t.get_pending_updates.
829 static u_int32_t
get_pending_updates(private_ike_sa_t
*this)
831 return this->pending_updates
;
835 * Update hosts, as addresses may change (NAT)
837 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
841 if (supports_extension(this, EXT_MOBIKE
))
842 { /* if peer speaks mobike, address updates are explicit only */
852 other
= this->other_host
;
855 /* apply hosts on first received message */
856 if (this->my_host
->is_anyaddr(this->my_host
) ||
857 this->other_host
->is_anyaddr(this->other_host
))
859 set_my_host(this, me
->clone(me
));
860 set_other_host(this, other
->clone(other
));
865 /* update our address in any case */
866 if (!me
->equals(me
, this->my_host
))
868 set_my_host(this, me
->clone(me
));
872 if (!other
->equals(other
, this->other_host
))
874 /* update others adress if we are NOT NATed,
875 * and allow port changes if we are NATed */
876 if (!has_condition(this, COND_NAT_HERE
) ||
877 other
->ip_equals(other
, this->other_host
))
879 set_other_host(this, other
->clone(other
));
885 /* update all associated CHILD_SAs, if required */
888 iterator_t
*iterator
;
889 child_sa_t
*child_sa
;
891 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
892 while (iterator
->iterate(iterator
, (void**)&child_sa
))
894 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
895 has_condition(this, COND_NAT_ANY
));
897 iterator
->destroy(iterator
);
902 * Implementation of ike_sa_t.generate
904 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
907 this->time
.outbound
= time(NULL
);
908 message
->set_ike_sa_id(message
, this->ike_sa_id
);
909 return message
->generate(message
, this->crypter_out
, this->signer_out
, packet
);
913 * send a notify back to the sender
915 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
921 response
= message_create();
922 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
923 response
->set_request(response
, FALSE
);
924 response
->set_message_id(response
, request
->get_message_id(request
));
925 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
926 if (this->my_host
->is_anyaddr(this->my_host
))
928 this->my_host
->destroy(this->my_host
);
929 this->my_host
= request
->get_destination(request
);
930 this->my_host
= this->my_host
->clone(this->my_host
);
932 if (this->other_host
->is_anyaddr(this->other_host
))
934 this->other_host
->destroy(this->other_host
);
935 this->other_host
= request
->get_source(request
);
936 this->other_host
= this->other_host
->clone(this->other_host
);
938 response
->set_source(response
, this->my_host
->clone(this->my_host
));
939 response
->set_destination(response
, this->other_host
->clone(this->other_host
));
940 if (generate_message(this, response
, &packet
) == SUCCESS
)
942 charon
->sender
->send(charon
->sender
, packet
);
944 response
->destroy(response
);
949 * Implementation of ike_sa_t.act_as_mediation_server.
951 static void act_as_mediation_server(private_ike_sa_t
*this)
953 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
954 this->other_id
, this->ike_sa_id
);
955 this->is_mediation_server
= TRUE
;
959 * Implementation of ike_sa_t.get_server_reflexive_host.
961 static host_t
*get_server_reflexive_host(private_ike_sa_t
*this)
963 return this->server_reflexive_host
;
967 * Implementation of ike_sa_t.set_server_reflexive_host.
969 static void set_server_reflexive_host(private_ike_sa_t
*this, host_t
*host
)
971 DESTROY_IF(this->server_reflexive_host
);
972 this->server_reflexive_host
= host
;
976 * Implementation of ike_sa_t.get_connect_id.
978 static chunk_t
get_connect_id(private_ike_sa_t
*this)
980 return this->connect_id
;
984 * Implementation of ike_sa_t.respond
986 static status_t
respond(private_ike_sa_t
*this, identification_t
*peer_id
,
989 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
990 task
->respond(task
, peer_id
, connect_id
);
991 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
992 return this->task_manager
->initiate(this->task_manager
);
996 * Implementation of ike_sa_t.callback
998 static status_t
callback(private_ike_sa_t
*this, identification_t
*peer_id
)
1000 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1001 task
->callback(task
, peer_id
);
1002 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1003 return this->task_manager
->initiate(this->task_manager
);
1007 * Implementation of ike_sa_t.relay
1009 static status_t
relay(private_ike_sa_t
*this, identification_t
*requester
,
1010 chunk_t connect_id
, chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
1012 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1013 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1014 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1015 return this->task_manager
->initiate(this->task_manager
);
1019 * Implementation of ike_sa_t.initiate_mediation
1021 static status_t
initiate_mediation(private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
1023 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1024 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1025 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1026 return this->task_manager
->initiate(this->task_manager
);
1030 * Implementation of ike_sa_t.initiate_mediated
1032 static status_t
initiate_mediated(private_ike_sa_t
*this, host_t
*me
, host_t
*other
,
1033 linked_list_t
*childs
, chunk_t connect_id
)
1035 set_my_host(this, me
->clone(me
));
1036 set_other_host(this, other
->clone(other
));
1037 this->connect_id
= chunk_clone(connect_id
);
1040 child_cfg_t
*child_cfg
;
1041 iterator_t
*iterator
= childs
->create_iterator(childs
, TRUE
);
1042 while (iterator
->iterate(iterator
, (void**)&child_cfg
))
1044 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
1045 this->task_manager
->queue_task(this->task_manager
, task
);
1047 iterator
->destroy(iterator
);
1048 return this->task_manager
->initiate(this->task_manager
);
1053 * Implementation of ike_sa_t.initiate.
1055 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1059 if (this->state
== IKE_CREATED
)
1061 if (this->other_host
->is_anyaddr(this->other_host
)
1063 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1067 child_cfg
->destroy(child_cfg
);
1068 SIG(IKE_UP_START
, "initiating IKE_SA");
1069 SIG(IKE_UP_FAILED
, "unable to initiate to %%any");
1073 this->ike_initiator
= TRUE
;
1075 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1076 this->task_manager
->queue_task(this->task_manager
, task
);
1077 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1078 this->task_manager
->queue_task(this->task_manager
, task
);
1079 task
= (task_t
*)ike_cert_pre_create(&this->public, TRUE
);
1080 this->task_manager
->queue_task(this->task_manager
, task
);
1081 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1082 this->task_manager
->queue_task(this->task_manager
, task
);
1083 task
= (task_t
*)ike_cert_post_create(&this->public, TRUE
);
1084 this->task_manager
->queue_task(this->task_manager
, task
);
1085 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1086 this->task_manager
->queue_task(this->task_manager
, task
);
1087 task
= (task_t
*)ike_auth_lifetime_create(&this->public, TRUE
);
1088 this->task_manager
->queue_task(this->task_manager
, task
);
1089 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1091 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1092 this->task_manager
->queue_task(this->task_manager
, task
);
1095 task
= (task_t
*)ike_me_create(&this->public, TRUE
);
1096 this->task_manager
->queue_task(this->task_manager
, task
);
1101 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1103 /* mediated connection, initiate mediation process */
1104 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
, child_cfg
);
1105 child_cfg
->destroy(child_cfg
);
1106 charon
->processor
->queue_job(charon
->processor
, job
);
1109 else if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1111 if (this->state
== IKE_ESTABLISHED
)
1112 { /* FIXME: we should try to find a better solution to this */
1113 SIG(CHILD_UP_SUCCESS
, "mediation connection is already up and running");
1115 DESTROY_IF(child_cfg
);
1120 /* normal IKE_SA with CHILD_SA */
1121 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
1122 child_cfg
->destroy(child_cfg
);
1123 this->task_manager
->queue_task(this->task_manager
, task
);
1126 return this->task_manager
->initiate(this->task_manager
);
1130 * Implementation of ike_sa_t.acquire.
1132 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
1133 { /* FIXME: IKE-ME */
1134 child_cfg_t
*child_cfg
;
1135 iterator_t
*iterator
;
1136 child_sa_t
*current
, *child_sa
= NULL
;
1138 child_create_t
*child_create
;
1140 if (this->state
== IKE_DELETING
)
1142 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
1143 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1144 "IKE_SA is deleting", reqid
);
1149 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1150 while (iterator
->iterate(iterator
, (void**)¤t
))
1152 if (current
->get_reqid(current
) == reqid
)
1158 iterator
->destroy(iterator
);
1161 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
1162 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1163 "CHILD_SA not found", reqid
);
1168 if (this->state
== IKE_CREATED
)
1170 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1171 this->task_manager
->queue_task(this->task_manager
, task
);
1172 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1173 this->task_manager
->queue_task(this->task_manager
, task
);
1174 task
= (task_t
*)ike_cert_pre_create(&this->public, TRUE
);
1175 this->task_manager
->queue_task(this->task_manager
, task
);
1176 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1177 this->task_manager
->queue_task(this->task_manager
, task
);
1178 task
= (task_t
*)ike_cert_post_create(&this->public, TRUE
);
1179 this->task_manager
->queue_task(this->task_manager
, task
);
1180 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1181 this->task_manager
->queue_task(this->task_manager
, task
);
1182 task
= (task_t
*)ike_auth_lifetime_create(&this->public, TRUE
);
1183 this->task_manager
->queue_task(this->task_manager
, task
);
1184 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1186 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1187 this->task_manager
->queue_task(this->task_manager
, task
);
1191 child_cfg
= child_sa
->get_config(child_sa
);
1192 child_create
= child_create_create(&this->public, child_cfg
);
1193 child_create
->use_reqid(child_create
, reqid
);
1194 this->task_manager
->queue_task(this->task_manager
, (task_t
*)child_create
);
1196 return this->task_manager
->initiate(this->task_manager
);
1200 * Implementation of ike_sa_t.route.
1202 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1204 child_sa_t
*child_sa
;
1205 iterator_t
*iterator
;
1206 linked_list_t
*my_ts
, *other_ts
;
1210 SIG(CHILD_ROUTE_START
, "routing CHILD_SA");
1212 /* check if not already routed*/
1213 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1214 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1216 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1217 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
1219 iterator
->destroy(iterator
);
1220 SIG(CHILD_ROUTE_FAILED
, "CHILD_SA with such a config already routed");
1224 iterator
->destroy(iterator
);
1226 switch (this->state
)
1230 SIG(CHILD_ROUTE_FAILED
,
1231 "unable to route CHILD_SA, as its IKE_SA gets deleted");
1234 case IKE_CONNECTING
:
1235 case IKE_ESTABLISHED
:
1240 /* install kernel policies */
1241 child_sa
= child_sa_create(this->my_host
, this->other_host
, this->my_id
,
1242 this->other_id
, child_cfg
, FALSE
, 0);
1244 if (this->my_virtual_ip
)
1246 me
= this->my_virtual_ip
;
1248 other
= this->other_host
;
1249 if (this->other_virtual_ip
)
1251 other
= this->other_virtual_ip
;
1254 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, me
);
1255 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, other
);
1256 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
,
1257 child_cfg
->get_mode(child_cfg
));
1258 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1259 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1260 if (status
== SUCCESS
)
1262 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1263 SIG(CHILD_ROUTE_SUCCESS
, "CHILD_SA routed");
1267 SIG(CHILD_ROUTE_FAILED
, "routing CHILD_SA failed");
1273 * Implementation of ike_sa_t.unroute.
1275 static status_t
unroute(private_ike_sa_t
*this, u_int32_t reqid
)
1277 iterator_t
*iterator
;
1278 child_sa_t
*child_sa
;
1281 SIG(CHILD_UNROUTE_START
, "unrouting CHILD_SA");
1283 /* find CHILD_SA in ROUTED state */
1284 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1285 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1287 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1288 child_sa
->get_reqid(child_sa
) == reqid
)
1290 iterator
->remove(iterator
);
1291 SIG(CHILD_UNROUTE_SUCCESS
, "CHILD_SA unrouted");
1292 child_sa
->destroy(child_sa
);
1297 iterator
->destroy(iterator
);
1301 SIG(CHILD_UNROUTE_FAILED
, "CHILD_SA to unroute not found");
1304 /* if we are not established, and we have no more routed childs, remove whole SA */
1305 if (this->state
== IKE_CREATED
&&
1306 this->child_sas
->get_count(this->child_sas
) == 0)
1313 * Implementation of ike_sa_t.process_message.
1315 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
1320 is_request
= message
->get_request(message
);
1322 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
1323 if (status
!= SUCCESS
)
1331 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
1334 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
1338 DBG1(DBG_IKE
, "message parsing failed");
1341 send_notify_response(this, message
, INVALID_SYNTAX
);
1345 DBG1(DBG_IKE
, "message verification failed");
1348 send_notify_response(this, message
, INVALID_SYNTAX
);
1352 DBG1(DBG_IKE
, "integrity check failed");
1356 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1359 send_notify_response(this, message
, INVALID_SYNTAX
);
1365 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1366 exchange_type_names
, message
->get_exchange_type(message
),
1367 message
->get_request(message
) ?
"request" : "response",
1368 message
->get_message_id(message
));
1374 private_ike_sa_t
*new;
1375 iterator_t
*iterator
;
1377 bool has_routed
= FALSE
;
1379 me
= message
->get_destination(message
);
1380 other
= message
->get_source(message
);
1382 /* if this IKE_SA is virgin, we check for a config */
1383 if (this->ike_cfg
== NULL
)
1386 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1388 if (this->ike_cfg
== NULL
)
1390 /* no config found for these hosts, destroy */
1391 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1392 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1393 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
1396 /* add a timeout if peer does not establish it completely */
1397 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
1398 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
1399 HALF_OPEN_IKE_SA_TIMEOUT
);
1402 /* check if message is trustworthy, and update host information */
1403 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
1404 message
->get_exchange_type(message
) != IKE_SA_INIT
)
1406 update_hosts(this, me
, other
);
1407 this->time
.inbound
= time(NULL
);
1409 status
= this->task_manager
->process_message(this->task_manager
, message
);
1410 if (status
!= DESTROY_ME
)
1414 /* if IKE_SA gets closed for any reasons, reroute routed children */
1415 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1416 while (iterator
->iterate(iterator
, (void**)&child
))
1418 if (child
->get_state(child
) == CHILD_ROUTED
)
1424 iterator
->destroy(iterator
);
1429 /* move routed children to a new IKE_SA, apply connection info */
1430 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1431 charon
->ike_sa_manager
, TRUE
);
1432 set_peer_cfg(new, this->peer_cfg
);
1433 new->other_host
->destroy(new->other_host
);
1434 new->other_host
= this->other_host
->clone(this->other_host
);
1435 if (!has_condition(this, COND_NAT_THERE
))
1437 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1439 if (this->my_virtual_ip
)
1441 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1443 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1444 while (iterator
->iterate(iterator
, (void**)&child
))
1446 if (child
->get_state(child
) == CHILD_ROUTED
)
1448 route(new, child
->get_config(child
));
1451 iterator
->destroy(iterator
);
1452 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1458 * Implementation of ike_sa_t.retransmit.
1460 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
1461 { /* FIXME: IKE-ME */
1462 this->time
.outbound
= time(NULL
);
1463 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1465 child_cfg_t
*child_cfg
;
1466 child_sa_t
* child_sa
;
1467 linked_list_t
*to_route
, *to_restart
;
1468 iterator_t
*iterator
;
1470 /* send a proper signal to brief interested bus listeners */
1471 switch (this->state
)
1473 case IKE_CONNECTING
:
1475 /* retry IKE_SA_INIT if we have multiple keyingtries */
1476 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1478 if (tries
== 0 || tries
> this->keyingtry
)
1480 SIG(IKE_UP_FAILED
, "peer not responding, trying again "
1481 "(%d/%d) in background ", this->keyingtry
+ 1, tries
);
1483 return this->task_manager
->initiate(this->task_manager
);
1485 SIG(IKE_UP_FAILED
, "establishing IKE_SA failed, peer not responding");
1489 SIG(IKE_REKEY_FAILED
, "rekeying IKE_SA failed, peer not responding");
1492 SIG(IKE_DOWN_FAILED
, "proper IKE_SA delete failed, peer not responding");
1498 /* summarize how we have to handle each child */
1499 to_route
= linked_list_create();
1500 to_restart
= linked_list_create();
1501 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1502 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1504 child_cfg
= child_sa
->get_config(child_sa
);
1506 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1508 /* reroute routed CHILD_SAs */
1509 to_route
->insert_last(to_route
, child_cfg
);
1513 /* use DPD action for established CHILD_SAs */
1514 switch (this->peer_cfg
->get_dpd_action(this->peer_cfg
))
1517 to_route
->insert_last(to_route
, child_cfg
);
1520 to_restart
->insert_last(to_restart
, child_cfg
);
1527 iterator
->destroy(iterator
);
1529 /* create a new IKE_SA if we have to route or to restart */
1530 if (to_route
->get_count(to_route
) || to_restart
->get_count(to_restart
))
1532 private_ike_sa_t
*new;
1535 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1536 charon
->ike_sa_manager
, TRUE
);
1538 set_peer_cfg(new, this->peer_cfg
);
1539 /* use actual used host, not the wildcarded one in config */
1540 new->other_host
->destroy(new->other_host
);
1541 new->other_host
= this->other_host
->clone(this->other_host
);
1542 /* reset port to 500, but only if peer is not NATed */
1543 if (!has_condition(this, COND_NAT_THERE
))
1545 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1547 /* take over virtual ip, as we need it for a proper route */
1548 if (this->my_virtual_ip
)
1550 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1553 /* install routes */
1554 while (to_route
->remove_last(to_route
, (void**)&child_cfg
) == SUCCESS
)
1556 route(new, child_cfg
);
1559 /* restart children */
1560 if (to_restart
->get_count(to_restart
))
1562 task
= (task_t
*)ike_init_create(&new->public, TRUE
, NULL
);
1563 new->task_manager
->queue_task(new->task_manager
, task
);
1564 task
= (task_t
*)ike_natd_create(&new->public, TRUE
);
1565 new->task_manager
->queue_task(new->task_manager
, task
);
1566 task
= (task_t
*)ike_cert_pre_create(&new->public, TRUE
);
1567 new->task_manager
->queue_task(new->task_manager
, task
);
1568 task
= (task_t
*)ike_config_create(&new->public, TRUE
);
1569 new->task_manager
->queue_task(new->task_manager
, task
);
1570 task
= (task_t
*)ike_auth_create(&new->public, TRUE
);
1571 new->task_manager
->queue_task(new->task_manager
, task
);
1572 task
= (task_t
*)ike_cert_post_create(&new->public, TRUE
);
1573 new->task_manager
->queue_task(new->task_manager
, task
);
1575 while (to_restart
->remove_last(to_restart
, (void**)&child_cfg
) == SUCCESS
)
1577 task
= (task_t
*)child_create_create(&new->public, child_cfg
);
1578 new->task_manager
->queue_task(new->task_manager
, task
);
1580 task
= (task_t
*)ike_auth_lifetime_create(&new->public, TRUE
);
1581 new->task_manager
->queue_task(new->task_manager
, task
);
1582 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1584 task
= (task_t
*)ike_mobike_create(&new->public, TRUE
);
1585 new->task_manager
->queue_task(new->task_manager
, task
);
1587 new->task_manager
->initiate(new->task_manager
);
1589 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1591 to_route
->destroy(to_route
);
1592 to_restart
->destroy(to_restart
);
1599 * Implementation of ike_sa_t.get_prf.
1601 static prf_t
*get_prf(private_ike_sa_t
*this)
1607 * Implementation of ike_sa_t.get_prf.
1609 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1611 return this->child_prf
;
1615 * Implementation of ike_sa_t.get_skp_bild
1617 static chunk_t
get_skp_build(private_ike_sa_t
*this)
1619 return this->skp_build
;
1623 * Implementation of ike_sa_t.get_skp_verify
1625 static chunk_t
get_skp_verify(private_ike_sa_t
*this)
1627 return this->skp_verify
;
1631 * Implementation of ike_sa_t.get_id.
1633 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1635 return this->ike_sa_id
;
1639 * Implementation of ike_sa_t.get_my_id.
1641 static identification_t
* get_my_id(private_ike_sa_t
*this)
1647 * Implementation of ike_sa_t.set_my_id.
1649 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1651 DESTROY_IF(this->my_id
);
1656 * Implementation of ike_sa_t.get_other_id.
1658 static identification_t
* get_other_id(private_ike_sa_t
*this)
1660 return this->other_id
;
1664 * Implementation of ike_sa_t.set_other_id.
1666 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1668 DESTROY_IF(this->other_id
);
1669 this->other_id
= other
;
1673 * Implementation of ike_sa_t.derive_keys.
1675 static status_t
derive_keys(private_ike_sa_t
*this,
1676 proposal_t
*proposal
, chunk_t secret
,
1677 chunk_t nonce_i
, chunk_t nonce_r
,
1678 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1680 prf_plus_t
*prf_plus
;
1681 chunk_t skeyseed
, key
, nonces
, prf_plus_seed
;
1682 u_int16_t alg
, key_size
;
1683 crypter_t
*crypter_i
, *crypter_r
;
1684 signer_t
*signer_i
, *signer_r
;
1685 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1686 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1687 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1689 /* Create SAs general purpose PRF first, we may use it here */
1690 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &alg
, NULL
))
1692 DBG1(DBG_IKE
, "no %N selected",
1693 transform_type_names
, PSEUDO_RANDOM_FUNCTION
);
1696 this->prf
= lib
->crypto
->create_prf(lib
->crypto
, alg
);
1697 if (this->prf
== NULL
)
1699 DBG1(DBG_IKE
, "%N %N not supported!",
1700 transform_type_names
, PSEUDO_RANDOM_FUNCTION
,
1701 pseudo_random_function_names
, alg
);
1705 DBG4(DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1706 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1707 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1708 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1709 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1711 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1713 * if we are rekeying, SKEYSEED is built on another way
1715 if (child_prf
== NULL
) /* not rekeying */
1717 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1718 this->prf
->set_key(this->prf
, nonces
);
1719 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1720 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1721 this->prf
->set_key(this->prf
, skeyseed
);
1722 chunk_free(&skeyseed
);
1723 chunk_free(&secret
);
1724 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1728 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1729 * use OLD SAs PRF functions for both prf_plus and prf */
1730 secret
= chunk_cat("mc", secret
, nonces
);
1731 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1732 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1733 old_prf
->set_key(old_prf
, skeyseed
);
1734 chunk_free(&skeyseed
);
1735 chunk_free(&secret
);
1736 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1738 chunk_free(&nonces
);
1739 chunk_free(&prf_plus_seed
);
1741 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1743 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1744 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &alg
, NULL
);
1745 this->child_prf
= lib
->crypto
->create_prf(lib
->crypto
, alg
);
1746 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1747 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1748 DBG4(DBG_IKE
, "Sk_d secret %B", &key
);
1749 this->child_prf
->set_key(this->child_prf
, key
);
1752 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1753 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &alg
, NULL
))
1755 DBG1(DBG_IKE
, "no %N selected",
1756 transform_type_names
, INTEGRITY_ALGORITHM
);
1759 signer_i
= lib
->crypto
->create_signer(lib
->crypto
, alg
);
1760 signer_r
= lib
->crypto
->create_signer(lib
->crypto
, alg
);
1761 if (signer_i
== NULL
|| signer_r
== NULL
)
1763 DBG1(DBG_IKE
, "%N %N not supported!",
1764 transform_type_names
, INTEGRITY_ALGORITHM
,
1765 integrity_algorithm_names
,alg
);
1766 prf_plus
->destroy(prf_plus
);
1769 key_size
= signer_i
->get_key_size(signer_i
);
1771 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1772 DBG4(DBG_IKE
, "Sk_ai secret %B", &key
);
1773 signer_i
->set_key(signer_i
, key
);
1776 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1777 DBG4(DBG_IKE
, "Sk_ar secret %B", &key
);
1778 signer_r
->set_key(signer_r
, key
);
1783 this->signer_in
= signer_r
;
1784 this->signer_out
= signer_i
;
1788 this->signer_in
= signer_i
;
1789 this->signer_out
= signer_r
;
1792 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1793 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &alg
, &key_size
))
1795 DBG1(DBG_IKE
, "no %N selected",
1796 transform_type_names
, ENCRYPTION_ALGORITHM
);
1797 prf_plus
->destroy(prf_plus
);
1800 crypter_i
= lib
->crypto
->create_crypter(lib
->crypto
, alg
, key_size
/ 8);
1801 crypter_r
= lib
->crypto
->create_crypter(lib
->crypto
, alg
, key_size
/ 8);
1802 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1804 DBG1(DBG_IKE
, "%N %N (key size %d) not supported!",
1805 transform_type_names
, ENCRYPTION_ALGORITHM
,
1806 encryption_algorithm_names
, alg
, key_size
);
1807 prf_plus
->destroy(prf_plus
);
1810 key_size
= crypter_i
->get_key_size(crypter_i
);
1812 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1813 DBG4(DBG_IKE
, "Sk_ei secret %B", &key
);
1814 crypter_i
->set_key(crypter_i
, key
);
1817 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1818 DBG4(DBG_IKE
, "Sk_er secret %B", &key
);
1819 crypter_r
->set_key(crypter_r
, key
);
1824 this->crypter_in
= crypter_r
;
1825 this->crypter_out
= crypter_i
;
1829 this->crypter_in
= crypter_i
;
1830 this->crypter_out
= crypter_r
;
1833 /* SK_pi/SK_pr used for authentication => stored for later */
1834 key_size
= this->prf
->get_key_size(this->prf
);
1835 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1836 DBG4(DBG_IKE
, "Sk_pi secret %B", &key
);
1839 this->skp_build
= key
;
1843 this->skp_verify
= key
;
1845 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1846 DBG4(DBG_IKE
, "Sk_pr secret %B", &key
);
1849 this->skp_verify
= key
;
1853 this->skp_build
= key
;
1856 /* all done, prf_plus not needed anymore */
1857 prf_plus
->destroy(prf_plus
);
1863 * Implementation of ike_sa_t.add_child_sa.
1865 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1867 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1871 * Implementation of ike_sa_t.get_child_sa.
1873 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1874 u_int32_t spi
, bool inbound
)
1876 iterator_t
*iterator
;
1877 child_sa_t
*current
, *found
= NULL
;
1879 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1880 while (iterator
->iterate(iterator
, (void**)¤t
))
1882 if (current
->get_spi(current
, inbound
) == spi
&&
1883 current
->get_protocol(current
) == protocol
)
1888 iterator
->destroy(iterator
);
1893 * Implementation of ike_sa_t.create_child_sa_iterator.
1895 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1897 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1901 * Implementation of ike_sa_t.rekey_child_sa.
1903 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1905 child_sa_t
*child_sa
;
1906 child_rekey_t
*child_rekey
;
1908 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1911 child_rekey
= child_rekey_create(&this->public, child_sa
);
1912 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1913 return this->task_manager
->initiate(this->task_manager
);
1919 * Implementation of ike_sa_t.delete_child_sa.
1921 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1923 child_sa_t
*child_sa
;
1924 child_delete_t
*child_delete
;
1926 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1929 child_delete
= child_delete_create(&this->public, child_sa
);
1930 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1931 return this->task_manager
->initiate(this->task_manager
);
1937 * Implementation of ike_sa_t.destroy_child_sa.
1939 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1942 iterator_t
*iterator
;
1943 child_sa_t
*child_sa
;
1944 status_t status
= NOT_FOUND
;
1946 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1947 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1949 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1950 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1952 child_sa
->destroy(child_sa
);
1953 iterator
->remove(iterator
);
1958 iterator
->destroy(iterator
);
1963 * Implementation of public_ike_sa_t.delete.
1965 static status_t
delete_(private_ike_sa_t
*this)
1967 ike_delete_t
*ike_delete
;
1969 switch (this->state
)
1971 case IKE_ESTABLISHED
:
1973 ike_delete
= ike_delete_create(&this->public, TRUE
);
1974 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1975 return this->task_manager
->initiate(this->task_manager
);
1977 SIG(IKE_DOWN_SUCCESS
, "deleting unestablished IKE_SA");
1980 SIG(IKE_DOWN_SUCCESS
, "destroying IKE_SA in state %N "
1981 "without notification", ike_sa_state_names
, this->state
);
1988 * Implementation of ike_sa_t.rekey.
1990 static status_t
rekey(private_ike_sa_t
*this)
1992 ike_rekey_t
*ike_rekey
;
1994 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1996 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1997 return this->task_manager
->initiate(this->task_manager
);
2001 * Implementation of ike_sa_t.reestablish
2003 static status_t
reestablish(private_ike_sa_t
*this)
2007 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
2008 * If the peer does not support RFC4478, there is no way to keep the
2010 if (!this->ike_initiator
)
2012 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
2013 if (this->other_virtual_ip
!= NULL
||
2014 has_condition(this, COND_EAP_AUTHENTICATED
))
2016 time_t now
= time(NULL
);
2018 DBG1(DBG_IKE
, "IKE_SA will timeout in %#V", &now
, &this->time
.delete);
2023 DBG1(DBG_IKE
, "reauthenticating actively");
2026 task
= (task_t
*)ike_reauth_create(&this->public);
2027 this->task_manager
->queue_task(this->task_manager
, task
);
2029 return this->task_manager
->initiate(this->task_manager
);
2033 * Implementation of ike_sa_t.set_auth_lifetime.
2035 static void set_auth_lifetime(private_ike_sa_t
*this, u_int32_t lifetime
)
2038 u_int32_t reduction
= this->peer_cfg
->get_over_time(this->peer_cfg
);
2040 this->time
.reauth
= time(NULL
) + lifetime
- reduction
;
2041 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
2043 if (lifetime
< reduction
)
2045 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, starting reauthentication",
2047 charon
->processor
->queue_job(charon
->processor
, job
);
2051 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
2052 " in %ds", lifetime
, lifetime
- reduction
);
2053 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
2054 (lifetime
- reduction
) * 1000);
2059 * Implementation of ike_sa_t.roam.
2061 static status_t
roam(private_ike_sa_t
*this, bool address
)
2064 ike_mobike_t
*mobike
;
2066 /* responder just updates the peer about changed address config */
2067 if (!this->ike_sa_id
->is_initiator(this->ike_sa_id
))
2069 if (supports_extension(this, EXT_MOBIKE
) && address
)
2071 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
2072 mobike
= ike_mobike_create(&this->public, TRUE
);
2073 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
2074 return this->task_manager
->initiate(this->task_manager
);
2079 /* get best address pair to use */
2080 other
= this->other_host
;
2081 me
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
2086 if (me
->ip_equals(me
, this->my_host
) &&
2087 other
->ip_equals(other
, this->other_host
))
2089 DBG2(DBG_IKE
, "keeping connection path %H - %H", this->other_host
, me
);
2096 /* update addresses with mobike, if supported ... */
2097 if (supports_extension(this, EXT_MOBIKE
))
2099 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
2100 mobike
= ike_mobike_create(&this->public, TRUE
);
2101 mobike
->roam(mobike
, address
);
2102 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
2103 return this->task_manager
->initiate(this->task_manager
);
2105 DBG1(DBG_IKE
, "reestablishing IKE_SA due address change");
2106 /* ... reestablish if not */
2107 return reestablish(this);
2111 * Implementation of ike_sa_t.inherit.
2113 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
2115 child_sa_t
*child_sa
;
2118 /* apply hosts and ids */
2119 this->my_host
->destroy(this->my_host
);
2120 this->other_host
->destroy(this->other_host
);
2121 this->my_id
->destroy(this->my_id
);
2122 this->other_id
->destroy(this->other_id
);
2123 this->my_host
= other
->my_host
->clone(other
->my_host
);
2124 this->other_host
= other
->other_host
->clone(other
->other_host
);
2125 this->my_id
= other
->my_id
->clone(other
->my_id
);
2126 this->other_id
= other
->other_id
->clone(other
->other_id
);
2127 this->ike_initiator
= other
->ike_initiator
;
2129 /* apply virtual assigned IPs... */
2130 if (other
->my_virtual_ip
)
2132 this->my_virtual_ip
= other
->my_virtual_ip
;
2133 other
->my_virtual_ip
= NULL
;
2135 if (other
->other_virtual_ip
)
2137 this->other_virtual_ip
= other
->other_virtual_ip
;
2138 other
->other_virtual_ip
= NULL
;
2141 /* ... and DNS servers */
2142 while (other
->dns_servers
->remove_last(other
->dns_servers
,
2143 (void**)&ip
) == SUCCESS
)
2145 this->dns_servers
->insert_first(this->dns_servers
, ip
);
2148 /* inherit NAT-T conditions */
2149 this->conditions
= other
->conditions
;
2150 if (this->conditions
& COND_NAT_HERE
)
2152 send_keepalive(this);
2156 if (other
->is_mediation_server
)
2158 act_as_mediation_server(this);
2160 else if (other
->server_reflexive_host
)
2162 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
2163 other
->server_reflexive_host
);
2167 /* adopt all children */
2168 while (other
->child_sas
->remove_last(other
->child_sas
,
2169 (void**)&child_sa
) == SUCCESS
)
2171 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2174 /* move pending tasks to the new IKE_SA */
2175 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2177 /* reauthentication timeout survives a rekeying */
2178 if (other
->time
.reauth
)
2180 time_t reauth
, delete, now
= time(NULL
);
2182 this->time
.reauth
= other
->time
.reauth
;
2183 reauth
= this->time
.reauth
- now
;
2184 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
2185 this->time
.delete = this->time
.reauth
+ delete;
2186 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
2187 "lifetime reduced to %ds", reauth
, delete);
2188 charon
->scheduler
->schedule_job(charon
->scheduler
,
2189 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2191 charon
->scheduler
->schedule_job(charon
->scheduler
,
2192 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
),
2195 /* we have to initate here, there may be new tasks to handle */
2196 return this->task_manager
->initiate(this->task_manager
);
2200 * Implementation of ike_sa_t.remove_dns_server
2202 static void remove_dns_servers(private_ike_sa_t
*this)
2206 chunk_t contents
, line
, orig_line
, token
;
2207 char string
[INET6_ADDRSTRLEN
];
2209 iterator_t
*iterator
;
2211 if (this->dns_servers
->get_count(this->dns_servers
) == 0)
2213 /* don't touch anything if we have no nameservers installed */
2217 file
= fopen(RESOLV_CONF
, "r");
2218 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2220 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2221 RESOLV_CONF
, strerror(errno
));
2225 contents
= chunk_alloca((size_t)stats
.st_size
);
2227 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2229 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2235 file
= fopen(RESOLV_CONF
, "w");
2238 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2239 RESOLV_CONF
, strerror(errno
));
2243 iterator
= this->dns_servers
->create_iterator(this->dns_servers
, TRUE
);
2244 while (fetchline(&contents
, &line
))
2248 if (extract_token(&token
, ' ', &line
) &&
2249 strncasecmp(token
.ptr
, "nameserver", token
.len
) == 0)
2251 if (!extract_token(&token
, ' ', &line
))
2255 iterator
->reset(iterator
);
2256 while (iterator
->iterate(iterator
, (void**)&ip
))
2258 snprintf(string
, sizeof(string
), "%H", ip
);
2259 if (strlen(string
) == token
.len
&&
2260 strncmp(token
.ptr
, string
, token
.len
) == 0)
2262 iterator
->remove(iterator
);
2272 /* write line untouched back to file */
2273 fwrite(orig_line
.ptr
, orig_line
.len
, 1, file
);
2274 fprintf(file
, "\n");
2277 iterator
->destroy(iterator
);
2282 * Implementation of ike_sa_t.add_dns_server
2284 static void add_dns_server(private_ike_sa_t
*this, host_t
*dns
)
2290 DBG1(DBG_IKE
, "installing DNS server %H", dns
);
2292 file
= fopen(RESOLV_CONF
, "a+");
2293 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2295 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2296 RESOLV_CONF
, strerror(errno
));
2300 contents
= chunk_alloca(stats
.st_size
);
2302 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2304 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2310 file
= fopen(RESOLV_CONF
, "w");
2313 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2314 RESOLV_CONF
, strerror(errno
));
2318 if (fprintf(file
, "nameserver %H # added by strongSwan, assigned by %D\n",
2319 dns
, this->other_id
) < 0)
2321 DBG1(DBG_IKE
, "unable to write DNS configuration: %s", strerror(errno
));
2325 this->dns_servers
->insert_last(this->dns_servers
, dns
->clone(dns
));
2327 fwrite(contents
.ptr
, contents
.len
, 1, file
);
2333 * Implementation of ike_sa_t.destroy.
2335 static void destroy(private_ike_sa_t
*this)
2337 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2339 DESTROY_IF(this->crypter_in
);
2340 DESTROY_IF(this->crypter_out
);
2341 DESTROY_IF(this->signer_in
);
2342 DESTROY_IF(this->signer_out
);
2343 DESTROY_IF(this->prf
);
2344 DESTROY_IF(this->child_prf
);
2345 chunk_free(&this->skp_verify
);
2346 chunk_free(&this->skp_build
);
2348 if (this->my_virtual_ip
)
2350 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
2351 this->my_virtual_ip
);
2352 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
2354 if (this->other_virtual_ip
)
2356 if (this->peer_cfg
&& this->peer_cfg
->get_pool(this->peer_cfg
))
2358 charon
->attributes
->release_address(charon
->attributes
,
2359 this->peer_cfg
->get_pool(this->peer_cfg
),
2360 this->other_virtual_ip
);
2362 this->other_virtual_ip
->destroy(this->other_virtual_ip
);
2365 remove_dns_servers(this);
2366 this->dns_servers
->destroy_offset(this->dns_servers
,
2367 offsetof(host_t
, destroy
));
2368 this->additional_addresses
->destroy_offset(this->additional_addresses
,
2369 offsetof(host_t
, destroy
));
2371 if (this->is_mediation_server
)
2373 charon
->mediation_manager
->remove(charon
->mediation_manager
, this->ike_sa_id
);
2375 DESTROY_IF(this->server_reflexive_host
);
2376 chunk_free(&this->connect_id
);
2379 DESTROY_IF(this->my_host
);
2380 DESTROY_IF(this->other_host
);
2381 DESTROY_IF(this->my_id
);
2382 DESTROY_IF(this->other_id
);
2384 DESTROY_IF(this->ike_cfg
);
2385 DESTROY_IF(this->peer_cfg
);
2386 DESTROY_IF(this->my_auth
);
2387 DESTROY_IF(this->other_auth
);
2389 this->ike_sa_id
->destroy(this->ike_sa_id
);
2390 this->task_manager
->destroy(this->task_manager
);
2395 * Described in header.
2397 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2399 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2400 static u_int32_t unique_id
= 0;
2402 /* Public functions */
2403 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
2404 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2405 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
2406 this->public.get_statistic
= (u_int32_t(*)(ike_sa_t
*, statistic_t kind
))get_statistic
;
2407 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
2408 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
2409 this->public.route
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) route
;
2410 this->public.unroute
= (status_t (*)(ike_sa_t
*,u_int32_t
)) unroute
;
2411 this->public.acquire
= (status_t (*)(ike_sa_t
*,u_int32_t
)) acquire
;
2412 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
2413 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
2414 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
2415 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
2416 this->public.get_my_auth
= (auth_info_t
*(*)(ike_sa_t
*))get_my_auth
;
2417 this->public.get_other_auth
= (auth_info_t
*(*)(ike_sa_t
*))get_other_auth
;
2418 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
2419 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
2420 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
2421 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
2422 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
2423 this->public.update_hosts
= (void(*)(ike_sa_t
*, host_t
*me
, host_t
*other
))update_hosts
;
2424 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
2425 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2426 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
2427 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2428 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
2429 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
2430 this->public.set_condition
= (void (*)(ike_sa_t
*, ike_condition_t
,bool)) set_condition
;
2431 this->public.has_condition
= (bool (*)(ike_sa_t
*,ike_condition_t
)) has_condition
;
2432 this->public.set_pending_updates
= (void(*)(ike_sa_t
*, u_int32_t updates
))set_pending_updates
;
2433 this->public.get_pending_updates
= (u_int32_t(*)(ike_sa_t
*))get_pending_updates
;
2434 this->public.is_ike_initiator
= (bool (*)(ike_sa_t
*))is_ike_initiator
;
2435 this->public.create_additional_address_iterator
= (iterator_t
*(*)(ike_sa_t
*))create_additional_address_iterator
;
2436 this->public.add_additional_address
= (void(*)(ike_sa_t
*, host_t
*host
))add_additional_address
;
2437 this->public.retransmit
= (status_t (*)(ike_sa_t
*, u_int32_t
)) retransmit
;
2438 this->public.delete = (status_t (*)(ike_sa_t
*))delete_
;
2439 this->public.destroy
= (void (*)(ike_sa_t
*))destroy
;
2440 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
2441 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
2442 this->public.get_prf
= (prf_t
* (*)(ike_sa_t
*)) get_prf
;
2443 this->public.get_child_prf
= (prf_t
* (*)(ike_sa_t
*)) get_child_prf
;
2444 this->public.get_skp_verify
= (chunk_t (*)(ike_sa_t
*)) get_skp_verify
;
2445 this->public.get_skp_build
= (chunk_t (*)(ike_sa_t
*)) get_skp_build
;
2446 this->public.derive_keys
= (status_t (*)(ike_sa_t
*,proposal_t
*,chunk_t
,chunk_t
,chunk_t
,bool,prf_t
*,prf_t
*)) derive_keys
;
2447 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2448 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2449 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2450 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2451 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2452 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2453 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
2454 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
2455 this->public.set_auth_lifetime
= (void(*)(ike_sa_t
*, u_int32_t lifetime
))set_auth_lifetime
;
2456 this->public.roam
= (status_t(*)(ike_sa_t
*,bool))roam
;
2457 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
2458 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
2459 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
2460 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
2461 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
2462 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
2463 this->public.add_dns_server
= (void (*)(ike_sa_t
*,host_t
*))add_dns_server
;
2465 this->public.act_as_mediation_server
= (void (*)(ike_sa_t
*)) act_as_mediation_server
;
2466 this->public.get_server_reflexive_host
= (host_t
* (*)(ike_sa_t
*)) get_server_reflexive_host
;
2467 this->public.set_server_reflexive_host
= (void (*)(ike_sa_t
*,host_t
*)) set_server_reflexive_host
;
2468 this->public.get_connect_id
= (chunk_t (*)(ike_sa_t
*)) get_connect_id
;
2469 this->public.initiate_mediation
= (status_t (*)(ike_sa_t
*,peer_cfg_t
*)) initiate_mediation
;
2470 this->public.initiate_mediated
= (status_t (*)(ike_sa_t
*,host_t
*,host_t
*,linked_list_t
*,chunk_t
)) initiate_mediated
;
2471 this->public.relay
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
,chunk_t
,linked_list_t
*,bool)) relay
;
2472 this->public.callback
= (status_t (*)(ike_sa_t
*,identification_t
*)) callback
;
2473 this->public.respond
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
)) respond
;
2476 /* initialize private fields */
2477 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2478 this->child_sas
= linked_list_create();
2479 this->my_host
= host_create_any(AF_INET
);
2480 this->other_host
= host_create_any(AF_INET
);
2481 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2482 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2483 this->extensions
= 0;
2484 this->conditions
= 0;
2485 this->crypter_in
= NULL
;
2486 this->crypter_out
= NULL
;
2487 this->signer_in
= NULL
;
2488 this->signer_out
= NULL
;
2490 this->skp_verify
= chunk_empty
;
2491 this->skp_build
= chunk_empty
;
2492 this->child_prf
= NULL
;
2493 this->state
= IKE_CREATED
;
2494 this->time
.inbound
= this->time
.outbound
= time(NULL
);
2495 this->time
.established
= 0;
2496 this->time
.rekey
= 0;
2497 this->time
.reauth
= 0;
2498 this->time
.delete = 0;
2499 this->ike_cfg
= NULL
;
2500 this->peer_cfg
= NULL
;
2501 this->my_auth
= auth_info_create();
2502 this->other_auth
= auth_info_create();
2503 this->task_manager
= task_manager_create(&this->public);
2504 this->unique_id
= ++unique_id
;
2505 this->my_virtual_ip
= NULL
;
2506 this->other_virtual_ip
= NULL
;
2507 this->dns_servers
= linked_list_create();
2508 this->additional_addresses
= linked_list_create();
2509 this->pending_updates
= 0;
2510 this->keyingtry
= 0;
2511 this->ike_initiator
= FALSE
;
2513 this->is_mediation_server
= FALSE
;
2514 this->server_reflexive_host
= NULL
;
2515 this->connect_id
= chunk_empty
;
2518 return &this->public;