4 * @brief Implementation of ike_sa_t.
9 * Copyright (C) 2006-2007 Tobias Brunner
10 * Copyright (C) 2006 Daniel Roethlisberger
11 * Copyright (C) 2005-2006 Martin Willi
12 * Copyright (C) 2005 Jan Hutter
13 * Hochschule fuer Technik Rapperswil
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
36 #include <utils/linked_list.h>
37 #include <utils/lexparser.h>
38 #include <crypto/diffie_hellman.h>
39 #include <crypto/prf_plus.h>
40 #include <crypto/crypters/crypter.h>
41 #include <crypto/hashers/hasher.h>
42 #include <encoding/payloads/sa_payload.h>
43 #include <encoding/payloads/nonce_payload.h>
44 #include <encoding/payloads/ke_payload.h>
45 #include <encoding/payloads/delete_payload.h>
46 #include <encoding/payloads/transform_substructure.h>
47 #include <encoding/payloads/transform_attribute.h>
48 #include <encoding/payloads/ts_payload.h>
49 #include <sa/task_manager.h>
50 #include <sa/tasks/ike_init.h>
51 #include <sa/tasks/ike_natd.h>
52 #include <sa/tasks/ike_mobike.h>
53 #include <sa/tasks/ike_auth.h>
54 #include <sa/tasks/ike_config.h>
55 #include <sa/tasks/ike_cert.h>
56 #include <sa/tasks/ike_rekey.h>
57 #include <sa/tasks/ike_reauth.h>
58 #include <sa/tasks/ike_delete.h>
59 #include <sa/tasks/ike_dpd.h>
60 #include <sa/tasks/child_create.h>
61 #include <sa/tasks/child_delete.h>
62 #include <sa/tasks/child_rekey.h>
63 #include <processing/jobs/retransmit_job.h>
64 #include <processing/jobs/delete_ike_sa_job.h>
65 #include <processing/jobs/send_dpd_job.h>
66 #include <processing/jobs/send_keepalive_job.h>
67 #include <processing/jobs/rekey_ike_sa_job.h>
70 #include <sa/tasks/ike_p2p.h>
74 #define RESOLV_CONF "/etc/resolv.conf"
77 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DELETING
,
85 typedef struct private_ike_sa_t private_ike_sa_t
;
88 * Private data of an ike_sa_t object.
90 struct private_ike_sa_t
{
98 * Identifier for the current IKE_SA.
100 ike_sa_id_t
*ike_sa_id
;
103 * unique numerical ID for this IKE_SA.
108 * Current state of the IKE_SA
110 ike_sa_state_t state
;
113 * IKE configuration used to set up this IKE_SA
118 * Peer and authentication information to establish IKE_SA.
120 peer_cfg_t
*peer_cfg
;
123 * Juggles tasks to process messages
125 task_manager_t
*task_manager
;
128 * Address of local host
133 * Address of remote host
139 * Server reflexive host
141 host_t
*server_reflexive_host
;
145 * Identification used for us
147 identification_t
*my_id
;
150 * Identification used for other
152 identification_t
*other_id
;
155 * CA that issued the certificate of other
160 * set of extensions the peer supports
162 ike_extension_t extensions
;
165 * set of condition flags currently enabled for this IKE_SA
167 ike_condition_t conditions
;
170 * Linked List containing the child sa's of the current IKE_SA.
172 linked_list_t
*child_sas
;
175 * crypter for inbound traffic
177 crypter_t
*crypter_in
;
180 * crypter for outbound traffic
182 crypter_t
*crypter_out
;
185 * Signer for inbound traffic
190 * Signer for outbound traffic
192 signer_t
*signer_out
;
195 * Multi purpose prf, set key, use it, forget it
200 * Prf function for derivating keymat child SAs
205 * Key to build outging authentication data (SKp)
210 * Key to verify incoming authentication data (SKp)
215 * Virtual IP on local host, if any
217 host_t
*my_virtual_ip
;
220 * Virtual IP on remote host, if any
222 host_t
*other_virtual_ip
;
225 * List of DNS servers installed by us
227 linked_list_t
*dns_servers
;
230 * list of peers additional addresses, transmitted via MOBIKE
232 linked_list_t
*additional_addresses
;
235 * number pending UPDATE_SA_ADDRESS (MOBIKE)
237 u_int32_t pending_updates
;
240 * Timestamps for this IKE_SA
243 /** last IKE message received */
245 /** last IKE message sent */
247 /** when IKE_SA became established */
248 u_int32_t established
;
249 /** when IKE_SA gets rekeyed */
251 /** when IKE_SA gets deleted */
256 * how many times we have retried so far (keyingtries)
262 * get the time of the latest traffic processed by the kernel
264 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
266 iterator_t
*iterator
;
267 child_sa_t
*child_sa
;
268 time_t latest
= 0, use_time
;
270 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
271 while (iterator
->iterate(iterator
, (void**)&child_sa
))
273 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
275 latest
= max(latest
, use_time
);
278 iterator
->destroy(iterator
);
282 return max(this->time
.inbound
, latest
);
286 return max(this->time
.outbound
, latest
);
291 * Implementation of ike_sa_t.get_unique_id
293 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
295 return this->unique_id
;
299 * Implementation of ike_sa_t.get_name.
301 static char *get_name(private_ike_sa_t
*this)
305 return this->peer_cfg
->get_name(this->peer_cfg
);
312 * Implementation of ike_sa_t.get_stats.
314 static void get_stats(private_ike_sa_t
*this, u_int32_t
*next_rekeying
)
318 *next_rekeying
= this->time
.rekey
;
323 * Implementation of ike_sa_t.get_my_host.
325 static host_t
*get_my_host(private_ike_sa_t
*this)
327 return this->my_host
;
331 * Implementation of ike_sa_t.set_my_host.
333 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
335 DESTROY_IF(this->my_host
);
340 * Implementation of ike_sa_t.get_other_host.
342 static host_t
*get_other_host(private_ike_sa_t
*this)
344 return this->other_host
;
348 * Implementation of ike_sa_t.set_other_host.
350 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
352 DESTROY_IF(this->other_host
);
353 this->other_host
= other
;
357 * Implementation of ike_sa_t.get_peer_cfg
359 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
361 return this->peer_cfg
;
365 * Implementation of ike_sa_t.set_peer_cfg
367 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
369 peer_cfg
->get_ref(peer_cfg
);
370 this->peer_cfg
= peer_cfg
;
372 if (this->ike_cfg
== NULL
)
374 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
375 this->ike_cfg
->get_ref(this->ike_cfg
);
378 /* apply values, so we are ready to initate/acquire */
379 if (this->my_host
->is_anyaddr(this->my_host
))
381 host_t
*me
= this->ike_cfg
->get_my_host(this->ike_cfg
);
382 set_my_host(this, me
->clone(me
));
384 if (this->other_host
->is_anyaddr(this->other_host
))
386 host_t
*other
= this->ike_cfg
->get_other_host(this->ike_cfg
);
387 set_other_host(this, other
->clone(other
));
389 /* apply IDs if they are not already set */
390 if (this->my_id
->contains_wildcards(this->my_id
))
392 DESTROY_IF(this->my_id
);
393 this->my_id
= this->peer_cfg
->get_my_id(this->peer_cfg
);
394 this->my_id
= this->my_id
->clone(this->my_id
);
396 if (this->other_id
->contains_wildcards(this->other_id
))
398 DESTROY_IF(this->other_id
);
399 this->other_id
= this->peer_cfg
->get_other_id(this->peer_cfg
);
400 this->other_id
= this->other_id
->clone(this->other_id
);
405 * Implementation of ike_sa_t.send_keepalive
407 static void send_keepalive(private_ike_sa_t
*this)
409 send_keepalive_job_t
*job
;
410 time_t last_out
, now
, diff
;
412 if (!(this->conditions
& COND_NAT_HERE
))
413 { /* disable keep alives if we are not NATed anymore */
417 last_out
= get_use_time(this, FALSE
);
420 diff
= now
- last_out
;
422 if (diff
>= KEEPALIVE_INTERVAL
)
427 packet
= packet_create();
428 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
429 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
430 data
.ptr
= malloc(1);
433 packet
->set_data(packet
, data
);
434 DBG1(DBG_IKE
, "sending keep alive");
435 charon
->sender
->send(charon
->sender
, packet
);
438 job
= send_keepalive_job_create(this->ike_sa_id
);
439 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
440 (KEEPALIVE_INTERVAL
- diff
) * 1000);
444 * Implementation of ike_sa_t.get_ike_cfg
446 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
448 return this->ike_cfg
;
452 * Implementation of ike_sa_t.set_ike_cfg
454 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
456 ike_cfg
->get_ref(ike_cfg
);
457 this->ike_cfg
= ike_cfg
;
460 * Implementation of ike_sa_t.enable_extension.
462 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
464 this->extensions
|= extension
;
468 * Implementation of ike_sa_t.has_extension.
470 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
472 return (this->extensions
& extension
) != FALSE
;
476 * Implementation of ike_sa_t.has_condition.
478 static bool has_condition(private_ike_sa_t
*this, ike_condition_t condition
)
480 return (this->conditions
& condition
) != FALSE
;
484 * Implementation of ike_sa_t.enable_condition.
486 static void set_condition(private_ike_sa_t
*this, ike_condition_t condition
,
489 if (has_condition(this, condition
) != enable
)
493 this->conditions
|= condition
;
497 DBG1(DBG_IKE
, "no route to %H, setting IKE_SA to stale",
501 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
502 this->conditions
|= COND_NAT_ANY
;
503 send_keepalive(this);
506 DBG1(DBG_IKE
, "remote host is behind NAT");
507 this->conditions
|= COND_NAT_ANY
;
510 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
511 this->conditions
|= COND_NAT_ANY
;
519 this->conditions
&= ~condition
;
523 DBG1(DBG_IKE
, "new route to %H found", this->other_host
);
528 set_condition(this, COND_NAT_ANY
,
529 has_condition(this, COND_NAT_HERE
) ||
530 has_condition(this, COND_NAT_THERE
) ||
531 has_condition(this, COND_NAT_FAKE
));
541 * Implementation of ike_sa_t.send_dpd
543 static status_t
send_dpd(private_ike_sa_t
*this)
548 delay
= this->peer_cfg
->get_dpd_delay(this->peer_cfg
);
556 if (this->task_manager
->busy(this->task_manager
))
558 /* an exchange is in the air, no need to start a DPD check */
563 /* check if there was any inbound traffic */
565 last_in
= get_use_time(this, TRUE
);
567 diff
= now
- last_in
;
570 /* to long ago, initiate dead peer detection */
573 task
= (task_t
*)ike_dpd_create(TRUE
);
575 DBG1(DBG_IKE
, "sending DPD request");
577 this->task_manager
->queue_task(this->task_manager
, task
);
578 this->task_manager
->initiate(this->task_manager
);
581 /* recheck in "interval" seconds */
582 job
= send_dpd_job_create(this->ike_sa_id
);
583 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
584 (delay
- diff
) * 1000);
589 * Implementation of ike_sa_t.get_state.
591 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
597 * Implementation of ike_sa_t.set_state.
599 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
601 DBG1(DBG_IKE
, "IKE_SA '%s' state change: %N => %N",
603 ike_sa_state_names
, this->state
,
604 ike_sa_state_names
, state
);
608 case IKE_ESTABLISHED
:
610 if (this->state
== IKE_CONNECTING
)
613 u_int32_t now
= time(NULL
);
614 u_int32_t soft
, hard
;
617 this->time
.established
= now
;
618 /* start DPD checks */
621 /* schedule rekeying/reauthentication */
622 soft
= this->peer_cfg
->get_lifetime(this->peer_cfg
, TRUE
);
623 hard
= this->peer_cfg
->get_lifetime(this->peer_cfg
, FALSE
);
624 reauth
= this->peer_cfg
->use_reauth(this->peer_cfg
);
625 DBG1(DBG_IKE
, "scheduling %s in %ds, maximum lifetime %ds",
626 reauth ?
"reauthentication": "rekeying", soft
, hard
);
630 this->time
.rekey
= now
+ soft
;
631 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, reauth
);
632 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
638 this->time
.delete = now
+ hard
;
639 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
640 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
648 /* delete may fail if a packet gets lost, so set a timeout */
649 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
650 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
651 HALF_OPEN_IKE_SA_TIMEOUT
);
662 * Implementation of ike_sa_t.reset
664 static void reset(private_ike_sa_t
*this)
666 /* the responder ID is reset, as peer may choose another one */
667 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
669 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
672 set_state(this, IKE_CREATED
);
674 this->task_manager
->reset(this->task_manager
);
678 * Implementation of ike_sa_t.set_virtual_ip
680 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
684 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
685 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
686 this->my_host
) == SUCCESS
)
688 if (this->my_virtual_ip
)
690 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
691 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
692 this->my_virtual_ip
);
694 DESTROY_IF(this->my_virtual_ip
);
695 this->my_virtual_ip
= ip
->clone(ip
);
699 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
700 this->my_virtual_ip
= NULL
;
705 DESTROY_IF(this->other_virtual_ip
);
706 this->other_virtual_ip
= ip
->clone(ip
);
711 * Implementation of ike_sa_t.get_virtual_ip
713 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
717 return this->my_virtual_ip
;
721 return this->other_virtual_ip
;
726 * Implementation of ike_sa_t.add_additional_address.
728 static void add_additional_address(private_ike_sa_t
*this, host_t
*host
)
730 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
734 * Implementation of ike_sa_t.create_additional_address_iterator.
736 static iterator_t
* create_additional_address_iterator(private_ike_sa_t
*this)
738 return this->additional_addresses
->create_iterator(
739 this->additional_addresses
, TRUE
);
743 * Implementation of ike_sa_t.set_pending_updates.
745 static void set_pending_updates(private_ike_sa_t
*this, u_int32_t updates
)
747 this->pending_updates
= updates
;
751 * Implementation of ike_sa_t.get_pending_updates.
753 static u_int32_t
get_pending_updates(private_ike_sa_t
*this)
755 return this->pending_updates
;
759 * Update hosts, as addresses may change (NAT)
761 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
765 if (supports_extension(this, EXT_MOBIKE
))
766 { /* if peer speaks mobike, address updates are explicit only */
776 other
= this->other_host
;
779 /* apply hosts on first received message */
780 if (this->my_host
->is_anyaddr(this->my_host
) ||
781 this->other_host
->is_anyaddr(this->other_host
))
783 set_my_host(this, me
->clone(me
));
784 set_other_host(this, other
->clone(other
));
789 /* update our address in any case */
790 if (!me
->equals(me
, this->my_host
))
792 set_my_host(this, me
->clone(me
));
796 if (!other
->equals(other
, this->other_host
))
798 /* update others adress if we are NOT NATed,
799 * and allow port changes if we are NATed */
800 if (!has_condition(this, COND_NAT_HERE
) ||
801 other
->ip_equals(other
, this->other_host
))
803 set_other_host(this, other
->clone(other
));
809 /* update all associated CHILD_SAs, if required */
812 iterator_t
*iterator
;
813 child_sa_t
*child_sa
;
815 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
816 while (iterator
->iterate(iterator
, (void**)&child_sa
))
818 child_sa
->update_hosts(child_sa
, this->my_host
, this->other_host
,
819 has_condition(this, COND_NAT_ANY
));
821 iterator
->destroy(iterator
);
826 * Implementation of ike_sa_t.generate
828 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
831 this->time
.outbound
= time(NULL
);
832 message
->set_ike_sa_id(message
, this->ike_sa_id
);
833 return message
->generate(message
, this->crypter_out
, this->signer_out
, packet
);
837 * send a notify back to the sender
839 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
845 response
= message_create();
846 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
847 response
->set_request(response
, FALSE
);
848 response
->set_message_id(response
, request
->get_message_id(request
));
849 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
850 if (this->my_host
->is_anyaddr(this->my_host
))
852 this->my_host
->destroy(this->my_host
);
853 this->my_host
= request
->get_destination(request
);
854 this->my_host
= this->my_host
->clone(this->my_host
);
856 if (this->other_host
->is_anyaddr(this->other_host
))
858 this->other_host
->destroy(this->other_host
);
859 this->other_host
= request
->get_source(request
);
860 this->other_host
= this->other_host
->clone(this->other_host
);
862 if (generate_message(this, response
, &packet
) == SUCCESS
)
864 charon
->sender
->send(charon
->sender
, packet
);
866 response
->destroy(response
);
871 * Implementation of ike_sa_t.get_server_reflexive_host.
873 static host_t
*get_server_reflexive_host(private_ike_sa_t
*this)
875 return this->server_reflexive_host
;
879 * Implementation of ike_sa_t.set_server_reflexive_host.
881 static void set_server_reflexive_host(private_ike_sa_t
*this, host_t
*host
)
883 DESTROY_IF(this->server_reflexive_host
);
884 this->server_reflexive_host
= host
;
888 * Implementation of ike_sa_t.respond
890 static status_t
respond(private_ike_sa_t
*this, identification_t
*peer_id
,
893 ike_p2p_t
*task
= ike_p2p_create(&this->public, TRUE
);
894 task
->respond(task
, peer_id
, session_id
);
895 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
896 return this->task_manager
->initiate(this->task_manager
);
900 * Implementation of ike_sa_t.callback
902 static status_t
callback(private_ike_sa_t
*this, identification_t
*peer_id
)
904 ike_p2p_t
*task
= ike_p2p_create(&this->public, TRUE
);
905 task
->callback(task
, peer_id
);
906 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
907 return this->task_manager
->initiate(this->task_manager
);
911 * Implementation of ike_sa_t.relay
913 static status_t
relay(private_ike_sa_t
*this, identification_t
*requester
,
914 chunk_t session_id
, chunk_t session_key
, linked_list_t
*endpoints
, bool response
)
916 ike_p2p_t
*task
= ike_p2p_create(&this->public, TRUE
);
917 task
->relay(task
, requester
, session_id
, session_key
, endpoints
, response
);
918 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
919 return this->task_manager
->initiate(this->task_manager
);
923 * Implementation of ike_sa_t.initiate_mediation
925 static status_t
initiate_mediation(private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
927 ike_p2p_t
*task
= ike_p2p_create(&this->public, TRUE
);
928 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
929 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
930 return this->task_manager
->initiate(this->task_manager
);
934 * Implementation of ike_sa_t.initiate_mediated
936 static status_t
initiate_mediated(private_ike_sa_t
*this, host_t
*me
, host_t
*other
,
937 linked_list_t
*childs
)
939 this->my_host
= me
->clone(me
);
940 this->other_host
= other
->clone(other
);
943 child_cfg_t
*child_cfg
;
944 iterator_t
*iterator
= childs
->create_iterator(childs
, TRUE
);
945 while (iterator
->iterate(iterator
, (void**)&child_cfg
))
947 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
948 this->task_manager
->queue_task(this->task_manager
, task
);
950 iterator
->destroy(iterator
);
951 return this->task_manager
->initiate(this->task_manager
);
956 * Implementation of ike_sa_t.initiate.
958 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
962 if (this->state
== IKE_CREATED
)
964 if (this->other_host
->is_anyaddr(this->other_host
)
966 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
970 child_cfg
->destroy(child_cfg
);
971 SIG(IKE_UP_START
, "initiating IKE_SA");
972 SIG(IKE_UP_FAILED
, "unable to initiate to %%any");
976 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
977 this->task_manager
->queue_task(this->task_manager
, task
);
978 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
979 this->task_manager
->queue_task(this->task_manager
, task
);
980 task
= (task_t
*)ike_cert_create(&this->public, TRUE
);
981 this->task_manager
->queue_task(this->task_manager
, task
);
982 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
983 this->task_manager
->queue_task(this->task_manager
, task
);
984 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
985 this->task_manager
->queue_task(this->task_manager
, task
);
986 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
988 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
989 this->task_manager
->queue_task(this->task_manager
, task
);
992 task
= (task_t
*)ike_p2p_create(&this->public, TRUE
);
993 this->task_manager
->queue_task(this->task_manager
, task
);
998 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1000 // mediated connection, initiate mediation process
1001 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
, child_cfg
);
1002 child_cfg
->destroy(child_cfg
);
1003 charon
->processor
->queue_job(charon
->processor
, job
);
1006 else if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1008 if (this->state
== IKE_ESTABLISHED
)
1009 {// FIXME: we should try to find a better solution to this
1010 SIG(CHILD_UP_SUCCESS
, "mediation connection is already up and running");
1016 // normal IKE_SA with CHILD_SA
1017 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
1018 child_cfg
->destroy(child_cfg
);
1019 this->task_manager
->queue_task(this->task_manager
, task
);
1022 return this->task_manager
->initiate(this->task_manager
);
1026 * Implementation of ike_sa_t.acquire.
1028 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
1029 {// FIXME: P2P-NAT-T
1030 child_cfg_t
*child_cfg
;
1031 iterator_t
*iterator
;
1032 child_sa_t
*current
, *child_sa
= NULL
;
1034 child_create_t
*child_create
;
1036 if (this->state
== IKE_DELETING
)
1038 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
1039 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1040 "IKE_SA is deleting", reqid
);
1045 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1046 while (iterator
->iterate(iterator
, (void**)¤t
))
1048 if (current
->get_reqid(current
) == reqid
)
1054 iterator
->destroy(iterator
);
1057 SIG(CHILD_UP_START
, "acquiring CHILD_SA on kernel request");
1058 SIG(CHILD_UP_FAILED
, "acquiring CHILD_SA (reqid %d) failed: "
1059 "CHILD_SA not found", reqid
);
1064 if (this->state
== IKE_CREATED
)
1066 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1067 this->task_manager
->queue_task(this->task_manager
, task
);
1068 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1069 this->task_manager
->queue_task(this->task_manager
, task
);
1070 task
= (task_t
*)ike_cert_create(&this->public, TRUE
);
1071 this->task_manager
->queue_task(this->task_manager
, task
);
1072 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1073 this->task_manager
->queue_task(this->task_manager
, task
);
1074 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1075 this->task_manager
->queue_task(this->task_manager
, task
);
1076 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1078 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1079 this->task_manager
->queue_task(this->task_manager
, task
);
1083 child_cfg
= child_sa
->get_config(child_sa
);
1084 child_create
= child_create_create(&this->public, child_cfg
);
1085 child_create
->use_reqid(child_create
, reqid
);
1086 this->task_manager
->queue_task(this->task_manager
, (task_t
*)child_create
);
1088 return this->task_manager
->initiate(this->task_manager
);
1092 * Implementation of ike_sa_t.route.
1094 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1096 child_sa_t
*child_sa
;
1097 iterator_t
*iterator
;
1098 linked_list_t
*my_ts
, *other_ts
;
1102 SIG(CHILD_ROUTE_START
, "routing CHILD_SA");
1104 /* check if not already routed*/
1105 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1106 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1108 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1109 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
1111 iterator
->destroy(iterator
);
1112 SIG(CHILD_ROUTE_FAILED
, "CHILD_SA with such a config already routed");
1116 iterator
->destroy(iterator
);
1118 switch (this->state
)
1122 SIG(CHILD_ROUTE_FAILED
,
1123 "unable to route CHILD_SA, as its IKE_SA gets deleted");
1126 case IKE_CONNECTING
:
1127 case IKE_ESTABLISHED
:
1132 /* install kernel policies */
1133 child_sa
= child_sa_create(this->my_host
, this->other_host
, this->my_id
,
1134 this->other_id
, child_cfg
, FALSE
, 0);
1136 if (this->my_virtual_ip
)
1138 me
= this->my_virtual_ip
;
1140 other
= this->other_host
;
1141 if (this->other_virtual_ip
)
1143 other
= this->other_virtual_ip
;
1146 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, me
);
1147 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, other
);
1148 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
,
1149 child_cfg
->get_mode(child_cfg
));
1150 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1151 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1152 if (status
== SUCCESS
)
1154 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1155 SIG(CHILD_ROUTE_SUCCESS
, "CHILD_SA routed");
1159 SIG(CHILD_ROUTE_FAILED
, "routing CHILD_SA failed");
1165 * Implementation of ike_sa_t.unroute.
1167 static status_t
unroute(private_ike_sa_t
*this, u_int32_t reqid
)
1169 iterator_t
*iterator
;
1170 child_sa_t
*child_sa
;
1173 SIG(CHILD_UNROUTE_START
, "unrouting CHILD_SA");
1175 /* find CHILD_SA in ROUTED state */
1176 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1177 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1179 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1180 child_sa
->get_reqid(child_sa
) == reqid
)
1182 iterator
->remove(iterator
);
1183 SIG(CHILD_UNROUTE_SUCCESS
, "CHILD_SA unrouted");
1184 child_sa
->destroy(child_sa
);
1189 iterator
->destroy(iterator
);
1193 SIG(CHILD_UNROUTE_FAILED
, "CHILD_SA to unroute not found");
1196 /* if we are not established, and we have no more routed childs, remove whole SA */
1197 if (this->state
== IKE_CREATED
&&
1198 this->child_sas
->get_count(this->child_sas
) == 0)
1205 * Implementation of ike_sa_t.process_message.
1207 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
1212 is_request
= message
->get_request(message
);
1214 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
1215 if (status
!= SUCCESS
)
1223 DBG1(DBG_IKE
, "ciritcal unknown payloads found");
1226 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
1230 DBG1(DBG_IKE
, "message parsing failed");
1233 send_notify_response(this, message
, INVALID_SYNTAX
);
1237 DBG1(DBG_IKE
, "message verification failed");
1240 send_notify_response(this, message
, INVALID_SYNTAX
);
1244 DBG1(DBG_IKE
, "integrity check failed");
1248 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1251 send_notify_response(this, message
, INVALID_SYNTAX
);
1257 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1258 exchange_type_names
, message
->get_exchange_type(message
),
1259 message
->get_request(message
) ?
"request" : "response",
1260 message
->get_message_id(message
));
1266 private_ike_sa_t
*new;
1267 iterator_t
*iterator
;
1269 bool has_routed
= FALSE
;
1271 me
= message
->get_destination(message
);
1272 other
= message
->get_source(message
);
1274 /* if this IKE_SA is virgin, we check for a config */
1275 if (this->ike_cfg
== NULL
)
1278 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1280 if (this->ike_cfg
== NULL
)
1282 /* no config found for these hosts, destroy */
1283 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1284 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1285 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
1288 /* add a timeout if peer does not establish it completely */
1289 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
1290 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
1291 HALF_OPEN_IKE_SA_TIMEOUT
);
1294 /* check if message is trustworthy, and update host information */
1295 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
1296 message
->get_exchange_type(message
) != IKE_SA_INIT
)
1298 update_hosts(this, me
, other
);
1299 this->time
.inbound
= time(NULL
);
1301 status
= this->task_manager
->process_message(this->task_manager
, message
);
1302 if (status
!= DESTROY_ME
)
1306 /* if IKE_SA gets closed for any reasons, reroute routed children */
1307 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1308 while (iterator
->iterate(iterator
, (void**)&child
))
1310 if (child
->get_state(child
) == CHILD_ROUTED
)
1316 iterator
->destroy(iterator
);
1321 /* move routed children to a new IKE_SA, apply connection info */
1322 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1323 charon
->ike_sa_manager
, TRUE
);
1324 set_peer_cfg(new, this->peer_cfg
);
1325 new->other_host
->destroy(new->other_host
);
1326 new->other_host
= this->other_host
->clone(this->other_host
);
1327 if (!has_condition(this, COND_NAT_THERE
))
1329 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1331 if (this->my_virtual_ip
)
1333 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1335 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1336 while (iterator
->iterate(iterator
, (void**)&child
))
1338 if (child
->get_state(child
) == CHILD_ROUTED
)
1340 route(new, child
->get_config(child
));
1343 iterator
->destroy(iterator
);
1344 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1350 * Implementation of ike_sa_t.retransmit.
1352 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
1353 {// FIXME: P2P-NAT-T
1354 this->time
.outbound
= time(NULL
);
1355 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1357 child_cfg_t
*child_cfg
;
1358 child_sa_t
* child_sa
;
1359 linked_list_t
*to_route
, *to_restart
;
1360 iterator_t
*iterator
;
1362 /* send a proper signal to brief interested bus listeners */
1363 switch (this->state
)
1365 case IKE_CONNECTING
:
1367 /* retry IKE_SA_INIT if we have multiple keyingtries */
1368 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1370 if (tries
== 0 || tries
> this->keyingtry
)
1372 SIG(IKE_UP_FAILED
, "peer not responding, trying again "
1373 "(%d/%d) in background ", this->keyingtry
+ 1, tries
);
1375 return this->task_manager
->initiate(this->task_manager
);
1377 SIG(IKE_UP_FAILED
, "establishing IKE_SA failed, peer not responding");
1381 SIG(IKE_REKEY_FAILED
, "rekeying IKE_SA failed, peer not responding");
1384 SIG(IKE_DOWN_FAILED
, "proper IKE_SA delete failed, peer not responding");
1390 /* summarize how we have to handle each child */
1391 to_route
= linked_list_create();
1392 to_restart
= linked_list_create();
1393 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1394 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1396 child_cfg
= child_sa
->get_config(child_sa
);
1398 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
)
1400 /* reroute routed CHILD_SAs */
1401 to_route
->insert_last(to_route
, child_cfg
);
1405 /* use DPD action for established CHILD_SAs */
1406 switch (this->peer_cfg
->get_dpd_action(this->peer_cfg
))
1409 to_route
->insert_last(to_route
, child_cfg
);
1412 to_restart
->insert_last(to_restart
, child_cfg
);
1419 iterator
->destroy(iterator
);
1421 /* create a new IKE_SA if we have to route or to restart */
1422 if (to_route
->get_count(to_route
) || to_restart
->get_count(to_restart
))
1424 private_ike_sa_t
*new;
1427 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1428 charon
->ike_sa_manager
, TRUE
);
1430 set_peer_cfg(new, this->peer_cfg
);
1431 /* use actual used host, not the wildcarded one in config */
1432 new->other_host
->destroy(new->other_host
);
1433 new->other_host
= this->other_host
->clone(this->other_host
);
1434 /* reset port to 500, but only if peer is not NATed */
1435 if (!has_condition(this, COND_NAT_THERE
))
1437 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1439 /* take over virtual ip, as we need it for a proper route */
1440 if (this->my_virtual_ip
)
1442 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1445 /* install routes */
1446 while (to_route
->remove_last(to_route
, (void**)&child_cfg
) == SUCCESS
)
1448 route(new, child_cfg
);
1451 /* restart children */
1452 if (to_restart
->get_count(to_restart
))
1454 task
= (task_t
*)ike_init_create(&new->public, TRUE
, NULL
);
1455 new->task_manager
->queue_task(new->task_manager
, task
);
1456 task
= (task_t
*)ike_natd_create(&new->public, TRUE
);
1457 new->task_manager
->queue_task(new->task_manager
, task
);
1458 task
= (task_t
*)ike_cert_create(&new->public, TRUE
);
1459 new->task_manager
->queue_task(new->task_manager
, task
);
1460 task
= (task_t
*)ike_config_create(&new->public, TRUE
);
1461 new->task_manager
->queue_task(new->task_manager
, task
);
1462 task
= (task_t
*)ike_auth_create(&new->public, TRUE
);
1463 new->task_manager
->queue_task(new->task_manager
, task
);
1465 while (to_restart
->remove_last(to_restart
, (void**)&child_cfg
) == SUCCESS
)
1467 task
= (task_t
*)child_create_create(&new->public, child_cfg
);
1468 new->task_manager
->queue_task(new->task_manager
, task
);
1470 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1472 task
= (task_t
*)ike_mobike_create(&new->public, TRUE
);
1473 new->task_manager
->queue_task(new->task_manager
, task
);
1475 new->task_manager
->initiate(new->task_manager
);
1477 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1479 to_route
->destroy(to_route
);
1480 to_restart
->destroy(to_restart
);
1487 * Implementation of ike_sa_t.get_prf.
1489 static prf_t
*get_prf(private_ike_sa_t
*this)
1495 * Implementation of ike_sa_t.get_prf.
1497 static prf_t
*get_child_prf(private_ike_sa_t
*this)
1499 return this->child_prf
;
1503 * Implementation of ike_sa_t.get_skp_bild
1505 static chunk_t
get_skp_build(private_ike_sa_t
*this)
1507 return this->skp_build
;
1511 * Implementation of ike_sa_t.get_skp_verify
1513 static chunk_t
get_skp_verify(private_ike_sa_t
*this)
1515 return this->skp_verify
;
1519 * Implementation of ike_sa_t.get_id.
1521 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1523 return this->ike_sa_id
;
1527 * Implementation of ike_sa_t.get_my_id.
1529 static identification_t
* get_my_id(private_ike_sa_t
*this)
1535 * Implementation of ike_sa_t.set_my_id.
1537 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1539 DESTROY_IF(this->my_id
);
1544 * Implementation of ike_sa_t.get_other_id.
1546 static identification_t
* get_other_id(private_ike_sa_t
*this)
1548 return this->other_id
;
1552 * Implementation of ike_sa_t.set_other_id.
1554 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1556 DESTROY_IF(this->other_id
);
1557 this->other_id
= other
;
1561 * Implementation of ike_sa_t.get_other_ca.
1563 static ca_info_t
* get_other_ca(private_ike_sa_t
*this)
1565 return this->other_ca
;
1569 * Implementation of ike_sa_t.set_other_ca.
1571 static void set_other_ca(private_ike_sa_t
*this, ca_info_t
*other_ca
)
1573 this->other_ca
= other_ca
;
1577 * Implementation of ike_sa_t.derive_keys.
1579 static status_t
derive_keys(private_ike_sa_t
*this,
1580 proposal_t
*proposal
, chunk_t secret
,
1581 chunk_t nonce_i
, chunk_t nonce_r
,
1582 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
)
1584 prf_plus_t
*prf_plus
;
1585 chunk_t skeyseed
, key
, nonces
, prf_plus_seed
;
1588 crypter_t
*crypter_i
, *crypter_r
;
1589 signer_t
*signer_i
, *signer_r
;
1590 u_int8_t spi_i_buf
[sizeof(u_int64_t
)], spi_r_buf
[sizeof(u_int64_t
)];
1591 chunk_t spi_i
= chunk_from_buf(spi_i_buf
);
1592 chunk_t spi_r
= chunk_from_buf(spi_r_buf
);
1594 /* Create SAs general purpose PRF first, we may use it here */
1595 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
1597 DBG1(DBG_IKE
, "key derivation failed: no PSEUDO_RANDOM_FUNCTION");;
1600 this->prf
= prf_create(algo
->algorithm
);
1601 if (this->prf
== NULL
)
1603 DBG1(DBG_IKE
, "key derivation failed: PSEUDO_RANDOM_FUNCTION "
1604 "%N not supported!", pseudo_random_function_names
, algo
->algorithm
);
1608 DBG4(DBG_IKE
, "shared Diffie Hellman secret %B", &secret
);
1609 nonces
= chunk_cat("cc", nonce_i
, nonce_r
);
1610 *((u_int64_t
*)spi_i
.ptr
) = this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
1611 *((u_int64_t
*)spi_r
.ptr
) = this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
1612 prf_plus_seed
= chunk_cat("ccc", nonces
, spi_i
, spi_r
);
1614 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
1616 * if we are rekeying, SKEYSEED is built on another way
1618 if (child_prf
== NULL
) /* not rekeying */
1620 /* SKEYSEED = prf(Ni | Nr, g^ir) */
1621 this->prf
->set_key(this->prf
, nonces
);
1622 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
1623 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1624 this->prf
->set_key(this->prf
, skeyseed
);
1625 chunk_free(&skeyseed
);
1626 chunk_free(&secret
);
1627 prf_plus
= prf_plus_create(this->prf
, prf_plus_seed
);
1631 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
1632 * use OLD SAs PRF functions for both prf_plus and prf */
1633 secret
= chunk_cat("mc", secret
, nonces
);
1634 child_prf
->allocate_bytes(child_prf
, secret
, &skeyseed
);
1635 DBG4(DBG_IKE
, "SKEYSEED %B", &skeyseed
);
1636 old_prf
->set_key(old_prf
, skeyseed
);
1637 chunk_free(&skeyseed
);
1638 chunk_free(&secret
);
1639 prf_plus
= prf_plus_create(old_prf
, prf_plus_seed
);
1641 chunk_free(&nonces
);
1642 chunk_free(&prf_plus_seed
);
1644 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
1646 /* SK_d is used for generating CHILD_SA key mat => child_prf */
1647 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1648 this->child_prf
= prf_create(algo
->algorithm
);
1649 key_size
= this->child_prf
->get_key_size(this->child_prf
);
1650 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1651 DBG4(DBG_IKE
, "Sk_d secret %B", &key
);
1652 this->child_prf
->set_key(this->child_prf
, key
);
1655 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
1656 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1658 DBG1(DBG_IKE
, "key derivation failed: no INTEGRITY_ALGORITHM");
1661 signer_i
= signer_create(algo
->algorithm
);
1662 signer_r
= signer_create(algo
->algorithm
);
1663 if (signer_i
== NULL
|| signer_r
== NULL
)
1665 DBG1(DBG_IKE
, "key derivation failed: INTEGRITY_ALGORITHM "
1666 "%N not supported!", integrity_algorithm_names
,algo
->algorithm
);
1669 key_size
= signer_i
->get_key_size(signer_i
);
1671 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1672 DBG4(DBG_IKE
, "Sk_ai secret %B", &key
);
1673 signer_i
->set_key(signer_i
, key
);
1676 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1677 DBG4(DBG_IKE
, "Sk_ar secret %B", &key
);
1678 signer_r
->set_key(signer_r
, key
);
1683 this->signer_in
= signer_r
;
1684 this->signer_out
= signer_i
;
1688 this->signer_in
= signer_i
;
1689 this->signer_out
= signer_r
;
1692 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
1693 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1695 DBG1(DBG_IKE
, "key derivation failed: no ENCRYPTION_ALGORITHM");
1698 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1699 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1700 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1702 DBG1(DBG_IKE
, "key derivation failed: ENCRYPTION_ALGORITHM "
1703 "%N (key size %d) not supported!",
1704 encryption_algorithm_names
, algo
->algorithm
, algo
->key_size
);
1707 key_size
= crypter_i
->get_key_size(crypter_i
);
1709 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1710 DBG4(DBG_IKE
, "Sk_ei secret %B", &key
);
1711 crypter_i
->set_key(crypter_i
, key
);
1714 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1715 DBG4(DBG_IKE
, "Sk_er secret %B", &key
);
1716 crypter_r
->set_key(crypter_r
, key
);
1721 this->crypter_in
= crypter_r
;
1722 this->crypter_out
= crypter_i
;
1726 this->crypter_in
= crypter_i
;
1727 this->crypter_out
= crypter_r
;
1730 /* SK_pi/SK_pr used for authentication => stored for later */
1731 key_size
= this->prf
->get_key_size(this->prf
);
1732 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1733 DBG4(DBG_IKE
, "Sk_pi secret %B", &key
);
1736 this->skp_build
= key
;
1740 this->skp_verify
= key
;
1742 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1743 DBG4(DBG_IKE
, "Sk_pr secret %B", &key
);
1746 this->skp_verify
= key
;
1750 this->skp_build
= key
;
1753 /* all done, prf_plus not needed anymore */
1754 prf_plus
->destroy(prf_plus
);
1760 * Implementation of ike_sa_t.add_child_sa.
1762 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1764 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1768 * Implementation of ike_sa_t.get_child_sa.
1770 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1771 u_int32_t spi
, bool inbound
)
1773 iterator_t
*iterator
;
1774 child_sa_t
*current
, *found
= NULL
;
1776 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1777 while (iterator
->iterate(iterator
, (void**)¤t
))
1779 if (current
->get_spi(current
, inbound
) == spi
&&
1780 current
->get_protocol(current
) == protocol
)
1785 iterator
->destroy(iterator
);
1790 * Implementation of ike_sa_t.create_child_sa_iterator.
1792 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1794 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1798 * Implementation of ike_sa_t.rekey_child_sa.
1800 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1802 child_sa_t
*child_sa
;
1803 child_rekey_t
*child_rekey
;
1805 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1808 child_rekey
= child_rekey_create(&this->public, child_sa
);
1809 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1810 return this->task_manager
->initiate(this->task_manager
);
1816 * Implementation of ike_sa_t.delete_child_sa.
1818 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1820 child_sa_t
*child_sa
;
1821 child_delete_t
*child_delete
;
1823 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1826 child_delete
= child_delete_create(&this->public, child_sa
);
1827 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1828 return this->task_manager
->initiate(this->task_manager
);
1834 * Implementation of ike_sa_t.destroy_child_sa.
1836 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1839 iterator_t
*iterator
;
1840 child_sa_t
*child_sa
;
1841 status_t status
= NOT_FOUND
;
1843 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1844 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1846 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1847 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1849 child_sa
->destroy(child_sa
);
1850 iterator
->remove(iterator
);
1855 iterator
->destroy(iterator
);
1860 * Implementation of public_ike_sa_t.delete.
1862 static status_t
delete_(private_ike_sa_t
*this)
1864 ike_delete_t
*ike_delete
;
1866 switch (this->state
)
1868 case IKE_ESTABLISHED
:
1870 ike_delete
= ike_delete_create(&this->public, TRUE
);
1871 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1872 return this->task_manager
->initiate(this->task_manager
);
1874 SIG(IKE_DOWN_SUCCESS
, "deleting unestablished IKE_SA");
1877 SIG(IKE_DOWN_SUCCESS
, "destroying IKE_SA in state %N "
1878 "without notification", ike_sa_state_names
, this->state
);
1885 * Implementation of ike_sa_t.rekey.
1887 static status_t
rekey(private_ike_sa_t
*this)
1889 ike_rekey_t
*ike_rekey
;
1891 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1893 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1894 return this->task_manager
->initiate(this->task_manager
);
1898 * Implementation of ike_sa_t.reestablish
1900 static status_t
reestablish(private_ike_sa_t
*this)
1904 task
= (task_t
*)ike_reauth_create(&this->public);
1905 this->task_manager
->queue_task(this->task_manager
, task
);
1907 return this->task_manager
->initiate(this->task_manager
);
1911 * Implementation of ike_sa_t.roam.
1913 static status_t
roam(private_ike_sa_t
*this, bool address
)
1916 ike_mobike_t
*mobike
;
1918 /* responder just updates the peer about changed address config */
1919 if (!this->ike_sa_id
->is_initiator(this->ike_sa_id
))
1921 if (supports_extension(this, EXT_MOBIKE
) && address
)
1923 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1924 mobike
= ike_mobike_create(&this->public, TRUE
);
1925 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1926 return this->task_manager
->initiate(this->task_manager
);
1931 /* get best address pair to use */
1932 other
= this->other_host
;
1933 me
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
1936 set_condition(this, COND_STALE
, FALSE
);
1939 if (me
->ip_equals(me
, this->my_host
) &&
1940 other
->ip_equals(other
, this->other_host
))
1942 DBG2(DBG_IKE
, "keeping connection path %H - %H", this->other_host
, me
);
1949 /* update addresses with mobike, if supported ... */
1950 if (supports_extension(this, EXT_MOBIKE
))
1952 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
1953 mobike
= ike_mobike_create(&this->public, TRUE
);
1954 mobike
->roam(mobike
, address
);
1955 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1956 return this->task_manager
->initiate(this->task_manager
);
1958 DBG1(DBG_IKE
, "reestablishing IKE_SA due address change");
1959 /* ... reestablish if not */
1960 return reestablish(this);
1964 * Implementation of ike_sa_t.inherit.
1966 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
1968 child_sa_t
*child_sa
;
1971 /* apply hosts and ids */
1972 this->my_host
->destroy(this->my_host
);
1973 this->other_host
->destroy(this->other_host
);
1974 this->my_id
->destroy(this->my_id
);
1975 this->other_id
->destroy(this->other_id
);
1976 this->my_host
= other
->my_host
->clone(other
->my_host
);
1977 this->other_host
= other
->other_host
->clone(other
->other_host
);
1978 this->my_id
= other
->my_id
->clone(other
->my_id
);
1979 this->other_id
= other
->other_id
->clone(other
->other_id
);
1981 /* apply virtual assigned IPs... */
1982 if (other
->my_virtual_ip
)
1984 this->my_virtual_ip
= other
->my_virtual_ip
;
1985 other
->my_virtual_ip
= NULL
;
1987 if (other
->other_virtual_ip
)
1989 this->other_virtual_ip
= other
->other_virtual_ip
;
1990 other
->other_virtual_ip
= NULL
;
1993 /* ... and DNS servers */
1994 while (other
->dns_servers
->remove_last(other
->dns_servers
,
1995 (void**)&ip
) == SUCCESS
)
1997 this->dns_servers
->insert_first(this->dns_servers
, ip
);
2000 /* adopt all children */
2001 while (other
->child_sas
->remove_last(other
->child_sas
,
2002 (void**)&child_sa
) == SUCCESS
)
2004 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2007 /* move pending tasks to the new IKE_SA */
2008 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2010 /* we have to initate here, there may be new tasks to handle */
2011 return this->task_manager
->initiate(this->task_manager
);
2015 * Implementation of ike_sa_t.remove_dns_server
2017 static void remove_dns_servers(private_ike_sa_t
*this)
2021 chunk_t contents
, line
, orig_line
, token
;
2022 char string
[INET6_ADDRSTRLEN
];
2024 iterator_t
*iterator
;
2026 if (this->dns_servers
->get_count(this->dns_servers
) == 0)
2028 /* don't touch anything if we have no nameservers installed */
2032 file
= fopen(RESOLV_CONF
, "r");
2033 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2035 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2036 RESOLV_CONF
, strerror(errno
));
2040 contents
= chunk_alloca((size_t)stats
.st_size
);
2042 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2044 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2050 file
= fopen(RESOLV_CONF
, "w");
2053 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2054 RESOLV_CONF
, strerror(errno
));
2058 iterator
= this->dns_servers
->create_iterator(this->dns_servers
, TRUE
);
2059 while (fetchline(&contents
, &line
))
2063 if (extract_token(&token
, ' ', &line
) &&
2064 strncasecmp(token
.ptr
, "nameserver", token
.len
) == 0)
2066 if (!extract_token(&token
, ' ', &line
))
2070 iterator
->reset(iterator
);
2071 while (iterator
->iterate(iterator
, (void**)&ip
))
2073 snprintf(string
, sizeof(string
), "%H", ip
);
2074 if (strlen(string
) == token
.len
&&
2075 strncmp(token
.ptr
, string
, token
.len
) == 0)
2077 iterator
->remove(iterator
);
2087 /* write line untouched back to file */
2088 fwrite(orig_line
.ptr
, orig_line
.len
, 1, file
);
2089 fprintf(file
, "\n");
2092 iterator
->destroy(iterator
);
2097 * Implementation of ike_sa_t.add_dns_server
2099 static void add_dns_server(private_ike_sa_t
*this, host_t
*dns
)
2105 DBG1(DBG_IKE
, "installing DNS server %H", dns
);
2107 file
= fopen(RESOLV_CONF
, "a+");
2108 if (file
== NULL
|| stat(RESOLV_CONF
, &stats
) != 0)
2110 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2111 RESOLV_CONF
, strerror(errno
));
2115 contents
= chunk_alloca(stats
.st_size
);
2117 if (fread(contents
.ptr
, 1, contents
.len
, file
) != contents
.len
)
2119 DBG1(DBG_IKE
, "unable to read DNS configuration file: %s", strerror(errno
));
2125 file
= fopen(RESOLV_CONF
, "w");
2128 DBG1(DBG_IKE
, "unable to open DNS configuration file %s: %s",
2129 RESOLV_CONF
, strerror(errno
));
2133 if (fprintf(file
, "nameserver %H # added by strongSwan, assigned by %D\n",
2134 dns
, this->other_id
) < 0)
2136 DBG1(DBG_IKE
, "unable to write DNS configuration: %s", strerror(errno
));
2140 this->dns_servers
->insert_last(this->dns_servers
, dns
->clone(dns
));
2142 fwrite(contents
.ptr
, contents
.len
, 1, file
);
2148 * Implementation of ike_sa_t.destroy.
2150 static void destroy(private_ike_sa_t
*this)
2152 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2154 DESTROY_IF(this->crypter_in
);
2155 DESTROY_IF(this->crypter_out
);
2156 DESTROY_IF(this->signer_in
);
2157 DESTROY_IF(this->signer_out
);
2158 DESTROY_IF(this->prf
);
2159 DESTROY_IF(this->child_prf
);
2160 chunk_free(&this->skp_verify
);
2161 chunk_free(&this->skp_build
);
2163 if (this->my_virtual_ip
)
2165 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
2166 this->my_virtual_ip
);
2167 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
2169 DESTROY_IF(this->other_virtual_ip
);
2171 remove_dns_servers(this);
2172 this->dns_servers
->destroy_offset(this->dns_servers
,
2173 offsetof(host_t
, destroy
));
2174 this->additional_addresses
->destroy_offset(this->additional_addresses
,
2175 offsetof(host_t
, destroy
));
2177 if (this->peer_cfg
&& this->peer_cfg
->is_mediation(this->peer_cfg
) &&
2178 !this->ike_sa_id
->is_initiator(this->ike_sa_id
))
2181 charon
->mediation_manager
->remove(charon
->mediation_manager
, this->ike_sa_id
);
2183 DESTROY_IF(this->server_reflexive_host
);
2186 DESTROY_IF(this->my_host
);
2187 DESTROY_IF(this->other_host
);
2188 DESTROY_IF(this->my_id
);
2189 DESTROY_IF(this->other_id
);
2191 DESTROY_IF(this->ike_cfg
);
2192 DESTROY_IF(this->peer_cfg
);
2194 this->ike_sa_id
->destroy(this->ike_sa_id
);
2195 this->task_manager
->destroy(this->task_manager
);
2200 * Described in header.
2202 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2204 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2205 static u_int32_t unique_id
= 0;
2207 /* Public functions */
2208 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
2209 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2210 this->public.get_stats
= (void (*)(ike_sa_t
*,u_int32_t
*))get_stats
;
2211 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
2212 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
2213 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
2214 this->public.route
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) route
;
2215 this->public.unroute
= (status_t (*)(ike_sa_t
*,u_int32_t
)) unroute
;
2216 this->public.acquire
= (status_t (*)(ike_sa_t
*,u_int32_t
)) acquire
;
2217 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
2218 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
2219 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
2220 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
2221 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
2222 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
2223 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
2224 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
2225 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
2226 this->public.update_hosts
= (void(*)(ike_sa_t
*, host_t
*me
, host_t
*other
))update_hosts
;
2227 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
2228 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2229 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
2230 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2231 this->public.get_other_ca
= (ca_info_t
* (*)(ike_sa_t
*)) get_other_ca
;
2232 this->public.set_other_ca
= (void (*)(ike_sa_t
*,ca_info_t
*)) set_other_ca
;
2233 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
2234 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
2235 this->public.set_condition
= (void (*)(ike_sa_t
*, ike_condition_t
,bool)) set_condition
;
2236 this->public.has_condition
= (bool (*)(ike_sa_t
*,ike_condition_t
)) has_condition
;
2237 this->public.set_pending_updates
= (void(*)(ike_sa_t
*, u_int32_t updates
))set_pending_updates
;
2238 this->public.get_pending_updates
= (u_int32_t(*)(ike_sa_t
*))get_pending_updates
;
2239 this->public.create_additional_address_iterator
= (iterator_t
*(*)(ike_sa_t
*))create_additional_address_iterator
;
2240 this->public.add_additional_address
= (void(*)(ike_sa_t
*, host_t
*host
))add_additional_address
;
2241 this->public.retransmit
= (status_t (*)(ike_sa_t
*, u_int32_t
)) retransmit
;
2242 this->public.delete = (status_t (*)(ike_sa_t
*))delete_
;
2243 this->public.destroy
= (void (*)(ike_sa_t
*))destroy
;
2244 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
2245 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
2246 this->public.get_prf
= (prf_t
* (*)(ike_sa_t
*)) get_prf
;
2247 this->public.get_child_prf
= (prf_t
* (*)(ike_sa_t
*)) get_child_prf
;
2248 this->public.get_skp_verify
= (chunk_t (*)(ike_sa_t
*)) get_skp_verify
;
2249 this->public.get_skp_build
= (chunk_t (*)(ike_sa_t
*)) get_skp_build
;
2250 this->public.derive_keys
= (status_t (*)(ike_sa_t
*,proposal_t
*,chunk_t
,chunk_t
,chunk_t
,bool,prf_t
*,prf_t
*)) derive_keys
;
2251 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2252 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2253 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2254 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2255 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2256 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2257 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
2258 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
2259 this->public.roam
= (status_t(*)(ike_sa_t
*,bool))roam
;
2260 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
2261 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
2262 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
2263 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
2264 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
2265 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
2266 this->public.add_dns_server
= (void (*)(ike_sa_t
*,host_t
*))add_dns_server
;
2268 this->public.get_server_reflexive_host
= (host_t
* (*)(ike_sa_t
*)) get_server_reflexive_host
;
2269 this->public.set_server_reflexive_host
= (void (*)(ike_sa_t
*,host_t
*)) set_server_reflexive_host
;
2270 this->public.initiate_mediation
= (status_t (*)(ike_sa_t
*,peer_cfg_t
*)) initiate_mediation
;
2271 this->public.initiate_mediated
= (status_t (*)(ike_sa_t
*,host_t
*,host_t
*,linked_list_t
*)) initiate_mediated
;
2272 this->public.relay
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
,chunk_t
,linked_list_t
*,bool)) relay
;
2273 this->public.callback
= (status_t (*)(ike_sa_t
*,identification_t
*)) callback
;
2274 this->public.respond
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
)) respond
;
2277 /* initialize private fields */
2278 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2279 this->child_sas
= linked_list_create();
2280 this->my_host
= host_create_any(AF_INET
);
2281 this->other_host
= host_create_any(AF_INET
);
2282 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2283 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2284 this->other_ca
= NULL
;
2285 this->extensions
= 0;
2286 this->conditions
= 0;
2287 this->crypter_in
= NULL
;
2288 this->crypter_out
= NULL
;
2289 this->signer_in
= NULL
;
2290 this->signer_out
= NULL
;
2292 this->skp_verify
= chunk_empty
;
2293 this->skp_build
= chunk_empty
;
2294 this->child_prf
= NULL
;
2295 this->state
= IKE_CREATED
;
2296 this->time
.inbound
= this->time
.outbound
= time(NULL
);
2297 this->time
.established
= 0;
2298 this->time
.rekey
= 0;
2299 this->time
.delete = 0;
2300 this->ike_cfg
= NULL
;
2301 this->peer_cfg
= NULL
;
2302 this->task_manager
= task_manager_create(&this->public);
2303 this->unique_id
= ++unique_id
;
2304 this->my_virtual_ip
= NULL
;
2305 this->other_virtual_ip
= NULL
;
2306 this->dns_servers
= linked_list_create();
2307 this->additional_addresses
= linked_list_create();
2308 this->pending_updates
= 0;
2309 this->keyingtry
= 0;
2311 this->server_reflexive_host
= NULL
;
2314 return &this->public;