2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2009 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
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
29 #include <utils/linked_list.h>
30 #include <utils/lexparser.h>
31 #include <sa/task_manager.h>
32 #include <sa/tasks/ike_init.h>
33 #include <sa/tasks/ike_natd.h>
34 #include <sa/tasks/ike_mobike.h>
35 #include <sa/tasks/ike_auth.h>
36 #include <sa/tasks/ike_auth_lifetime.h>
37 #include <sa/tasks/ike_config.h>
38 #include <sa/tasks/ike_cert_pre.h>
39 #include <sa/tasks/ike_cert_post.h>
40 #include <sa/tasks/ike_rekey.h>
41 #include <sa/tasks/ike_reauth.h>
42 #include <sa/tasks/ike_delete.h>
43 #include <sa/tasks/ike_dpd.h>
44 #include <sa/tasks/child_create.h>
45 #include <sa/tasks/child_delete.h>
46 #include <sa/tasks/child_rekey.h>
47 #include <processing/jobs/retransmit_job.h>
48 #include <processing/jobs/delete_ike_sa_job.h>
49 #include <processing/jobs/send_dpd_job.h>
50 #include <processing/jobs/send_keepalive_job.h>
51 #include <processing/jobs/rekey_ike_sa_job.h>
54 #include <sa/tasks/ike_me.h>
55 #include <processing/jobs/initiate_mediation_job.h>
58 ENUM(ike_sa_state_names
, IKE_CREATED
, IKE_DESTROYING
,
68 typedef struct private_ike_sa_t private_ike_sa_t
;
69 typedef struct attribute_entry_t attribute_entry_t
;
72 * Private data of an ike_sa_t object.
74 struct private_ike_sa_t
{
82 * Identifier for the current IKE_SA.
84 ike_sa_id_t
*ike_sa_id
;
87 * unique numerical ID for this IKE_SA.
92 * Current state of the IKE_SA
97 * IKE configuration used to set up this IKE_SA
102 * Peer and authentication information to establish IKE_SA.
104 peer_cfg_t
*peer_cfg
;
107 * currently used authentication ruleset, local (as auth_cfg_t)
112 * currently used authentication constraints, remote (as auth_cfg_t)
114 auth_cfg_t
*other_auth
;
117 * Selected IKE proposal
119 proposal_t
*proposal
;
122 * Juggles tasks to process messages
124 task_manager_t
*task_manager
;
127 * Address of local host
132 * Address of remote host
138 * Are we mediation server
140 bool is_mediation_server
;
143 * Server reflexive host
145 host_t
*server_reflexive_host
;
154 * Identification used for us
156 identification_t
*my_id
;
159 * Identification used for other
161 identification_t
*other_id
;
164 * EAP Identity exchange in EAP-Identity method
166 identification_t
*eap_identity
;;
169 * set of extensions the peer supports
171 ike_extension_t extensions
;
174 * set of condition flags currently enabled for this IKE_SA
176 ike_condition_t conditions
;
179 * Linked List containing the child sa's of the current IKE_SA.
181 linked_list_t
*child_sas
;
184 * keymat of this IKE_SA
189 * Virtual IP on local host, if any
191 host_t
*my_virtual_ip
;
194 * Virtual IP on remote host, if any
196 host_t
*other_virtual_ip
;
199 * List of configuration attributes (attribute_entry_t)
201 linked_list_t
*attributes
;
204 * list of peers additional addresses, transmitted via MOBIKE
206 linked_list_t
*additional_addresses
;
209 * previously value of received DESTINATION_IP hash
211 chunk_t nat_detection_dest
;
214 * number pending UPDATE_SA_ADDRESS (MOBIKE)
216 u_int32_t pending_updates
;
219 * NAT keep alive interval
221 u_int32_t keepalive_interval
;
224 * Timestamps for this IKE_SA
226 u_int32_t stats
[STAT_MAX
];
229 * how many times we have retried so far (keyingtries)
234 * local host address to be used for IKE, set via MIGRATE kernel message
239 * remote host address to be used for IKE, set via MIGRATE kernel message
245 * Entry to maintain install configuration attributes during IKE_SA lifetime
247 struct attribute_entry_t
{
248 /** handler used to install this attribute */
249 attribute_handler_t
*handler
;
250 /** attribute type */
251 configuration_attribute_type_t type
;
252 /** attribute data */
257 * get the time of the latest traffic processed by the kernel
259 static time_t get_use_time(private_ike_sa_t
* this, bool inbound
)
261 enumerator_t
*enumerator
;
262 child_sa_t
*child_sa
;
267 use_time
= this->stats
[STAT_INBOUND
];
271 use_time
= this->stats
[STAT_OUTBOUND
];
273 enumerator
= this->child_sas
->create_enumerator(this->child_sas
);
274 while (enumerator
->enumerate(enumerator
, &child_sa
))
276 use_time
= max(use_time
, child_sa
->get_usetime(child_sa
, inbound
));
278 enumerator
->destroy(enumerator
);
284 * Implementation of ike_sa_t.get_unique_id
286 static u_int32_t
get_unique_id(private_ike_sa_t
*this)
288 return this->unique_id
;
292 * Implementation of ike_sa_t.get_name.
294 static char *get_name(private_ike_sa_t
*this)
298 return this->peer_cfg
->get_name(this->peer_cfg
);
304 * Implementation of ike_sa_t.get_statistic.
306 static u_int32_t
get_statistic(private_ike_sa_t
*this, statistic_t kind
)
310 return this->stats
[kind
];
316 * Implementation of ike_sa_t.get_my_host.
318 static host_t
*get_my_host(private_ike_sa_t
*this)
320 return this->my_host
;
324 * Implementation of ike_sa_t.set_my_host.
326 static void set_my_host(private_ike_sa_t
*this, host_t
*me
)
328 DESTROY_IF(this->my_host
);
333 * Implementation of ike_sa_t.get_other_host.
335 static host_t
*get_other_host(private_ike_sa_t
*this)
337 return this->other_host
;
341 * Implementation of ike_sa_t.set_other_host.
343 static void set_other_host(private_ike_sa_t
*this, host_t
*other
)
345 DESTROY_IF(this->other_host
);
346 this->other_host
= other
;
350 * Implementation of ike_sa_t.get_peer_cfg
352 static peer_cfg_t
* get_peer_cfg(private_ike_sa_t
*this)
354 return this->peer_cfg
;
358 * Implementation of ike_sa_t.set_peer_cfg
360 static void set_peer_cfg(private_ike_sa_t
*this, peer_cfg_t
*peer_cfg
)
362 DESTROY_IF(this->peer_cfg
);
363 peer_cfg
->get_ref(peer_cfg
);
364 this->peer_cfg
= peer_cfg
;
366 if (this->ike_cfg
== NULL
)
368 this->ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
369 this->ike_cfg
->get_ref(this->ike_cfg
);
374 * Implementation of ike_sa_t.get_auth_cfg
376 static auth_cfg_t
* get_auth_cfg(private_ike_sa_t
*this, bool local
)
380 return this->my_auth
;
382 return this->other_auth
;
386 * Implementation of ike_sa_t.get_proposal
388 static proposal_t
* get_proposal(private_ike_sa_t
*this)
390 return this->proposal
;
394 * Implementation of ike_sa_t.set_proposal
396 static void set_proposal(private_ike_sa_t
*this, proposal_t
*proposal
)
398 DESTROY_IF(this->proposal
);
399 this->proposal
= proposal
->clone(proposal
);
403 * Implementation of ike_sa_t.set_message_id
405 static void set_message_id(private_ike_sa_t
*this, bool initiate
, u_int32_t mid
)
409 this->task_manager
->reset(this->task_manager
, mid
, UINT_MAX
);
413 this->task_manager
->reset(this->task_manager
, UINT_MAX
, mid
);
418 * Implementation of ike_sa_t.send_keepalive
420 static void send_keepalive(private_ike_sa_t
*this)
422 send_keepalive_job_t
*job
;
423 time_t last_out
, now
, diff
;
425 if (!(this->conditions
& COND_NAT_HERE
) || this->keepalive_interval
== 0)
426 { /* disable keep alives if we are not NATed anymore */
430 last_out
= get_use_time(this, FALSE
);
433 diff
= now
- last_out
;
435 if (diff
>= this->keepalive_interval
)
440 packet
= packet_create();
441 packet
->set_source(packet
, this->my_host
->clone(this->my_host
));
442 packet
->set_destination(packet
, this->other_host
->clone(this->other_host
));
443 data
.ptr
= malloc(1);
446 packet
->set_data(packet
, data
);
447 DBG1(DBG_IKE
, "sending keep alive");
448 charon
->sender
->send(charon
->sender
, packet
);
451 job
= send_keepalive_job_create(this->ike_sa_id
);
452 charon
->scheduler
->schedule_job(charon
->scheduler
, (job_t
*)job
,
453 this->keepalive_interval
- diff
);
457 * Implementation of ike_sa_t.get_ike_cfg
459 static ike_cfg_t
*get_ike_cfg(private_ike_sa_t
*this)
461 return this->ike_cfg
;
465 * Implementation of ike_sa_t.set_ike_cfg
467 static void set_ike_cfg(private_ike_sa_t
*this, ike_cfg_t
*ike_cfg
)
469 ike_cfg
->get_ref(ike_cfg
);
470 this->ike_cfg
= ike_cfg
;
474 * Implementation of ike_sa_t.enable_extension.
476 static void enable_extension(private_ike_sa_t
*this, ike_extension_t extension
)
478 this->extensions
|= extension
;
482 * Implementation of ike_sa_t.has_extension.
484 static bool supports_extension(private_ike_sa_t
*this, ike_extension_t extension
)
486 return (this->extensions
& extension
) != FALSE
;
490 * Implementation of ike_sa_t.has_condition.
492 static bool has_condition(private_ike_sa_t
*this, ike_condition_t condition
)
494 return (this->conditions
& condition
) != FALSE
;
498 * Implementation of ike_sa_t.enable_condition.
500 static void set_condition(private_ike_sa_t
*this, ike_condition_t condition
,
503 if (has_condition(this, condition
) != enable
)
507 this->conditions
|= condition
;
511 DBG1(DBG_IKE
, "local host is behind NAT, sending keep alives");
512 this->conditions
|= COND_NAT_ANY
;
513 send_keepalive(this);
516 DBG1(DBG_IKE
, "remote host is behind NAT");
517 this->conditions
|= COND_NAT_ANY
;
520 DBG1(DBG_IKE
, "faking NAT situation to enforce UDP encapsulation");
521 this->conditions
|= COND_NAT_ANY
;
529 this->conditions
&= ~condition
;
535 set_condition(this, COND_NAT_ANY
,
536 has_condition(this, COND_NAT_HERE
) ||
537 has_condition(this, COND_NAT_THERE
) ||
538 has_condition(this, COND_NAT_FAKE
));
548 * Implementation of ike_sa_t.send_dpd
550 static status_t
send_dpd(private_ike_sa_t
*this)
555 delay
= this->peer_cfg
->get_dpd(this->peer_cfg
);
563 if (this->task_manager
->busy(this->task_manager
))
565 /* an exchange is in the air, no need to start a DPD check */
570 /* check if there was any inbound traffic */
572 last_in
= get_use_time(this, TRUE
);
574 diff
= now
- last_in
;
577 /* to long ago, initiate dead peer detection */
579 ike_mobike_t
*mobike
;
581 if (supports_extension(this, EXT_MOBIKE
) &&
582 has_condition(this, COND_NAT_HERE
))
584 /* use mobike enabled DPD to detect NAT mapping changes */
585 mobike
= ike_mobike_create(&this->public, TRUE
);
587 task
= &mobike
->task
;
591 task
= (task_t
*)ike_dpd_create(TRUE
);
594 DBG1(DBG_IKE
, "sending DPD request");
596 this->task_manager
->queue_task(this->task_manager
, task
);
597 this->task_manager
->initiate(this->task_manager
);
600 /* recheck in "interval" seconds */
601 job
= (job_t
*)send_dpd_job_create(this->ike_sa_id
);
602 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, delay
- diff
);
607 * Implementation of ike_sa_t.get_state.
609 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
615 * Implementation of ike_sa_t.set_state.
617 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
619 DBG2(DBG_IKE
, "IKE_SA %s[%d] state change: %N => %N",
620 get_name(this), this->unique_id
,
621 ike_sa_state_names
, this->state
,
622 ike_sa_state_names
, state
);
626 case IKE_ESTABLISHED
:
628 if (this->state
== IKE_CONNECTING
||
629 this->state
== IKE_PASSIVE
)
634 /* calculate rekey, reauth and lifetime */
635 this->stats
[STAT_ESTABLISHED
] = time(NULL
);
637 /* schedule rekeying if we have a time which is smaller than
638 * an already scheduled rekeying */
639 t
= this->peer_cfg
->get_rekey_time(this->peer_cfg
);
640 if (t
&& (this->stats
[STAT_REKEY
] == 0 ||
641 (this->stats
[STAT_REKEY
] > t
+ this->stats
[STAT_ESTABLISHED
])))
643 this->stats
[STAT_REKEY
] = t
+ this->stats
[STAT_ESTABLISHED
];
644 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, FALSE
);
645 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, t
);
646 DBG1(DBG_IKE
, "scheduling rekeying in %ds", t
);
648 t
= this->peer_cfg
->get_reauth_time(this->peer_cfg
);
649 if (t
&& (this->stats
[STAT_REAUTH
] == 0 ||
650 (this->stats
[STAT_REAUTH
] > t
+ this->stats
[STAT_ESTABLISHED
])))
652 this->stats
[STAT_REAUTH
] = t
+ this->stats
[STAT_ESTABLISHED
];
653 job
= (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
);
654 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, t
);
655 DBG1(DBG_IKE
, "scheduling reauthentication in %ds", t
);
657 t
= this->peer_cfg
->get_over_time(this->peer_cfg
);
658 if (this->stats
[STAT_REKEY
] || this->stats
[STAT_REAUTH
])
660 if (this->stats
[STAT_REAUTH
] == 0)
662 this->stats
[STAT_DELETE
] = this->stats
[STAT_REKEY
];
664 else if (this->stats
[STAT_REKEY
] == 0)
666 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
];
670 this->stats
[STAT_DELETE
] = min(this->stats
[STAT_REKEY
],
671 this->stats
[STAT_REAUTH
]);
673 this->stats
[STAT_DELETE
] += t
;
674 t
= this->stats
[STAT_DELETE
] - this->stats
[STAT_ESTABLISHED
];
675 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
676 charon
->scheduler
->schedule_job(charon
->scheduler
, job
, t
);
677 DBG1(DBG_IKE
, "maximum IKE_SA lifetime %ds", t
);
680 /* start DPD checks */
687 /* delete may fail if a packet gets lost, so set a timeout */
688 job_t
*job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
);
689 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
690 HALF_OPEN_IKE_SA_TIMEOUT
);
696 charon
->bus
->ike_state_change(charon
->bus
, &this->public, state
);
701 * Implementation of ike_sa_t.reset
703 static void reset(private_ike_sa_t
*this)
705 /* the responder ID is reset, as peer may choose another one */
706 if (this->ike_sa_id
->is_initiator(this->ike_sa_id
))
708 this->ike_sa_id
->set_responder_spi(this->ike_sa_id
, 0);
711 set_state(this, IKE_CREATED
);
713 this->task_manager
->reset(this->task_manager
, 0, 0);
717 * Implementation of ike_sa_t.get_keymat
719 static keymat_t
* get_keymat(private_ike_sa_t
*this)
725 * Implementation of ike_sa_t.set_virtual_ip
727 static void set_virtual_ip(private_ike_sa_t
*this, bool local
, host_t
*ip
)
731 DBG1(DBG_IKE
, "installing new virtual IP %H", ip
);
732 if (charon
->kernel_interface
->add_ip(charon
->kernel_interface
, ip
,
733 this->my_host
) == SUCCESS
)
735 if (this->my_virtual_ip
)
737 DBG1(DBG_IKE
, "removing old virtual IP %H", this->my_virtual_ip
);
738 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
739 this->my_virtual_ip
);
741 DESTROY_IF(this->my_virtual_ip
);
742 this->my_virtual_ip
= ip
->clone(ip
);
746 DBG1(DBG_IKE
, "installing virtual IP %H failed", ip
);
747 this->my_virtual_ip
= NULL
;
752 DESTROY_IF(this->other_virtual_ip
);
753 this->other_virtual_ip
= ip
->clone(ip
);
758 * Implementation of ike_sa_t.get_virtual_ip
760 static host_t
* get_virtual_ip(private_ike_sa_t
*this, bool local
)
764 return this->my_virtual_ip
;
768 return this->other_virtual_ip
;
773 * Implementation of ike_sa_t.add_additional_address.
775 static void add_additional_address(private_ike_sa_t
*this, host_t
*host
)
777 this->additional_addresses
->insert_last(this->additional_addresses
, host
);
781 * Implementation of ike_sa_t.create_additional_address_iterator.
783 static iterator_t
* create_additional_address_iterator(private_ike_sa_t
*this)
785 return this->additional_addresses
->create_iterator(
786 this->additional_addresses
, TRUE
);
790 * Implementation of ike_sa_t.has_mapping_changed
792 static bool has_mapping_changed(private_ike_sa_t
*this, chunk_t hash
)
794 if (this->nat_detection_dest
.ptr
== NULL
)
796 this->nat_detection_dest
= chunk_clone(hash
);
799 if (chunk_equals(hash
, this->nat_detection_dest
))
803 free(this->nat_detection_dest
.ptr
);
804 this->nat_detection_dest
= chunk_clone(hash
);
809 * Implementation of ike_sa_t.set_pending_updates.
811 static void set_pending_updates(private_ike_sa_t
*this, u_int32_t updates
)
813 this->pending_updates
= updates
;
817 * Implementation of ike_sa_t.get_pending_updates.
819 static u_int32_t
get_pending_updates(private_ike_sa_t
*this)
821 return this->pending_updates
;
825 * Update hosts, as addresses may change (NAT)
827 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
837 other
= this->other_host
;
840 /* apply hosts on first received message */
841 if (this->my_host
->is_anyaddr(this->my_host
) ||
842 this->other_host
->is_anyaddr(this->other_host
))
844 set_my_host(this, me
->clone(me
));
845 set_other_host(this, other
->clone(other
));
850 /* update our address in any case */
851 if (!me
->equals(me
, this->my_host
))
853 set_my_host(this, me
->clone(me
));
857 if (!other
->equals(other
, this->other_host
))
859 /* update others adress if we are NOT NATed,
860 * and allow port changes if we are NATed */
861 if (!has_condition(this, COND_NAT_HERE
) ||
862 other
->ip_equals(other
, this->other_host
))
864 set_other_host(this, other
->clone(other
));
870 /* update all associated CHILD_SAs, if required */
873 iterator_t
*iterator
;
874 child_sa_t
*child_sa
;
876 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
877 while (iterator
->iterate(iterator
, (void**)&child_sa
))
879 if (child_sa
->update(child_sa
, this->my_host
,
880 this->other_host
, this->my_virtual_ip
,
881 has_condition(this, COND_NAT_ANY
)) == NOT_SUPPORTED
)
883 this->public.rekey_child_sa(&this->public,
884 child_sa
->get_protocol(child_sa
),
885 child_sa
->get_spi(child_sa
, TRUE
));
888 iterator
->destroy(iterator
);
893 * Implementation of ike_sa_t.generate
895 static status_t
generate_message(private_ike_sa_t
*this, message_t
*message
,
898 this->stats
[STAT_OUTBOUND
] = time(NULL
);
899 message
->set_ike_sa_id(message
, this->ike_sa_id
);
900 return message
->generate(message
,
901 this->keymat
->get_crypter(this->keymat
, FALSE
),
902 this->keymat
->get_signer(this->keymat
, FALSE
), packet
);
906 * send a notify back to the sender
908 static void send_notify_response(private_ike_sa_t
*this, message_t
*request
,
914 response
= message_create();
915 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
916 response
->set_request(response
, FALSE
);
917 response
->set_message_id(response
, request
->get_message_id(request
));
918 response
->add_notify(response
, FALSE
, type
, chunk_empty
);
919 if (this->my_host
->is_anyaddr(this->my_host
))
921 this->my_host
->destroy(this->my_host
);
922 this->my_host
= request
->get_destination(request
);
923 this->my_host
= this->my_host
->clone(this->my_host
);
925 if (this->other_host
->is_anyaddr(this->other_host
))
927 this->other_host
->destroy(this->other_host
);
928 this->other_host
= request
->get_source(request
);
929 this->other_host
= this->other_host
->clone(this->other_host
);
931 response
->set_source(response
, this->my_host
->clone(this->my_host
));
932 response
->set_destination(response
, this->other_host
->clone(this->other_host
));
933 if (generate_message(this, response
, &packet
) == SUCCESS
)
935 charon
->sender
->send(charon
->sender
, packet
);
937 response
->destroy(response
);
941 * Implementation of ike_sa_t.set_kmaddress.
943 static void set_kmaddress(private_ike_sa_t
*this, host_t
*local
, host_t
*remote
)
945 DESTROY_IF(this->local_host
);
946 DESTROY_IF(this->remote_host
);
947 this->local_host
= local
->clone(local
);
948 this->remote_host
= remote
->clone(remote
);
953 * Implementation of ike_sa_t.act_as_mediation_server.
955 static void act_as_mediation_server(private_ike_sa_t
*this)
957 charon
->mediation_manager
->update_sa_id(charon
->mediation_manager
,
958 this->other_id
, this->ike_sa_id
);
959 this->is_mediation_server
= TRUE
;
963 * Implementation of ike_sa_t.get_server_reflexive_host.
965 static host_t
*get_server_reflexive_host(private_ike_sa_t
*this)
967 return this->server_reflexive_host
;
971 * Implementation of ike_sa_t.set_server_reflexive_host.
973 static void set_server_reflexive_host(private_ike_sa_t
*this, host_t
*host
)
975 DESTROY_IF(this->server_reflexive_host
);
976 this->server_reflexive_host
= host
;
980 * Implementation of ike_sa_t.get_connect_id.
982 static chunk_t
get_connect_id(private_ike_sa_t
*this)
984 return this->connect_id
;
988 * Implementation of ike_sa_t.respond
990 static status_t
respond(private_ike_sa_t
*this, identification_t
*peer_id
,
993 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
994 task
->respond(task
, peer_id
, connect_id
);
995 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
996 return this->task_manager
->initiate(this->task_manager
);
1000 * Implementation of ike_sa_t.callback
1002 static status_t
callback(private_ike_sa_t
*this, identification_t
*peer_id
)
1004 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1005 task
->callback(task
, peer_id
);
1006 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1007 return this->task_manager
->initiate(this->task_manager
);
1011 * Implementation of ike_sa_t.relay
1013 static status_t
relay(private_ike_sa_t
*this, identification_t
*requester
,
1014 chunk_t connect_id
, chunk_t connect_key
, linked_list_t
*endpoints
, bool response
)
1016 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1017 task
->relay(task
, requester
, connect_id
, connect_key
, endpoints
, response
);
1018 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1019 return this->task_manager
->initiate(this->task_manager
);
1023 * Implementation of ike_sa_t.initiate_mediation
1025 static status_t
initiate_mediation(private_ike_sa_t
*this, peer_cfg_t
*mediated_cfg
)
1027 ike_me_t
*task
= ike_me_create(&this->public, TRUE
);
1028 task
->connect(task
, mediated_cfg
->get_peer_id(mediated_cfg
));
1029 this->task_manager
->queue_task(this->task_manager
, (task_t
*)task
);
1030 return this->task_manager
->initiate(this->task_manager
);
1034 * Implementation of ike_sa_t.initiate_mediated
1036 static status_t
initiate_mediated(private_ike_sa_t
*this, host_t
*me
, host_t
*other
,
1039 set_my_host(this, me
->clone(me
));
1040 set_other_host(this, other
->clone(other
));
1041 chunk_free(&this->connect_id
);
1042 this->connect_id
= chunk_clone(connect_id
);
1044 return this->task_manager
->initiate(this->task_manager
);
1049 * Resolve DNS host in configuration
1051 static void resolve_hosts(private_ike_sa_t
*this)
1055 if (this->remote_host
)
1057 host
= this->remote_host
->clone(this->remote_host
);
1058 host
->set_port(host
, IKEV2_UDP_PORT
);
1062 host
= host_create_from_dns(this->ike_cfg
->get_other_addr(this->ike_cfg
),
1067 set_other_host(this, host
);
1070 if (this->local_host
)
1072 host
= this->local_host
->clone(this->local_host
);
1073 host
->set_port(host
, IKEV2_UDP_PORT
);
1077 host
= host_create_from_dns(this->ike_cfg
->get_my_addr(this->ike_cfg
),
1078 this->my_host
->get_family(this->my_host
),
1081 if (host
&& host
->is_anyaddr(host
) &&
1082 !this->other_host
->is_anyaddr(this->other_host
))
1084 host
->destroy(host
);
1085 host
= charon
->kernel_interface
->get_source_addr(
1086 charon
->kernel_interface
, this->other_host
, NULL
);
1089 host
->set_port(host
, IKEV2_UDP_PORT
);
1092 { /* fallback to address family specific %any(6), if configured */
1093 host
= host_create_from_dns(
1094 this->ike_cfg
->get_my_addr(this->ike_cfg
),
1101 set_my_host(this, host
);
1106 * Initiates a CHILD_SA using the appropriate reqid
1108 static status_t
initiate_with_reqid(private_ike_sa_t
*this, child_cfg_t
*child_cfg
, u_int32_t reqid
)
1112 if (this->state
== IKE_CREATED
)
1114 resolve_hosts(this);
1116 if (this->other_host
->is_anyaddr(this->other_host
)
1118 && !this->peer_cfg
->get_mediated_by(this->peer_cfg
)
1122 child_cfg
->destroy(child_cfg
);
1123 DBG1(DBG_IKE
, "unable to initiate to %%any");
1127 set_condition(this, COND_ORIGINAL_INITIATOR
, TRUE
);
1129 task
= (task_t
*)ike_init_create(&this->public, TRUE
, NULL
);
1130 this->task_manager
->queue_task(this->task_manager
, task
);
1131 task
= (task_t
*)ike_natd_create(&this->public, TRUE
);
1132 this->task_manager
->queue_task(this->task_manager
, task
);
1133 task
= (task_t
*)ike_cert_pre_create(&this->public, TRUE
);
1134 this->task_manager
->queue_task(this->task_manager
, task
);
1135 task
= (task_t
*)ike_auth_create(&this->public, TRUE
);
1136 this->task_manager
->queue_task(this->task_manager
, task
);
1137 task
= (task_t
*)ike_cert_post_create(&this->public, TRUE
);
1138 this->task_manager
->queue_task(this->task_manager
, task
);
1139 task
= (task_t
*)ike_config_create(&this->public, TRUE
);
1140 this->task_manager
->queue_task(this->task_manager
, task
);
1141 task
= (task_t
*)ike_auth_lifetime_create(&this->public, TRUE
);
1142 this->task_manager
->queue_task(this->task_manager
, task
);
1143 if (this->peer_cfg
->use_mobike(this->peer_cfg
))
1145 task
= (task_t
*)ike_mobike_create(&this->public, TRUE
);
1146 this->task_manager
->queue_task(this->task_manager
, task
);
1149 task
= (task_t
*)ike_me_create(&this->public, TRUE
);
1150 this->task_manager
->queue_task(this->task_manager
, task
);
1155 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1157 if (this->state
== IKE_ESTABLISHED
)
1159 /* mediation connection is already established, retrigger state change
1160 * to notify bus listeners */
1161 DBG1(DBG_IKE
, "mediation connection is already up");
1162 set_state(this, IKE_ESTABLISHED
);
1164 DESTROY_IF(child_cfg
);
1169 /* normal IKE_SA with CHILD_SA */
1170 task
= (task_t
*)child_create_create(&this->public, child_cfg
);
1171 child_cfg
->destroy(child_cfg
);
1174 child_create_t
*child_create
= (child_create_t
*)task
;
1175 child_create
->use_reqid(child_create
, reqid
);
1177 this->task_manager
->queue_task(this->task_manager
, task
);
1180 if (this->peer_cfg
->get_mediated_by(this->peer_cfg
))
1182 /* mediated connection, initiate mediation process */
1183 job_t
*job
= (job_t
*)initiate_mediation_job_create(this->ike_sa_id
);
1184 charon
->processor
->queue_job(charon
->processor
, job
);
1190 return this->task_manager
->initiate(this->task_manager
);
1194 * Implementation of ike_sa_t.initiate.
1196 static status_t
initiate(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1198 return initiate_with_reqid(this, child_cfg
, 0);
1202 * Implementation of ike_sa_t.acquire.
1204 static status_t
acquire(private_ike_sa_t
*this, u_int32_t reqid
)
1206 child_cfg_t
*child_cfg
;
1207 iterator_t
*iterator
;
1208 child_sa_t
*current
, *child_sa
= NULL
;
1210 switch (this->state
)
1213 DBG1(DBG_IKE
, "acquiring CHILD_SA {reqid %d} failed: "
1214 "IKE_SA is deleting", reqid
);
1217 /* do not process acquires if passive */
1224 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1225 while (iterator
->iterate(iterator
, (void**)¤t
))
1227 if (current
->get_reqid(current
) == reqid
)
1233 iterator
->destroy(iterator
);
1236 DBG1(DBG_IKE
, "acquiring CHILD_SA {reqid %d} failed: "
1237 "CHILD_SA not found", reqid
);
1241 child_cfg
= child_sa
->get_config(child_sa
);
1242 child_cfg
->get_ref(child_cfg
);
1244 return initiate_with_reqid(this, child_cfg
, reqid
);
1248 * Implementation of ike_sa_t.route.
1250 static status_t
route(private_ike_sa_t
*this, child_cfg_t
*child_cfg
)
1252 child_sa_t
*child_sa
;
1253 iterator_t
*iterator
;
1254 linked_list_t
*my_ts
, *other_ts
;
1258 /* check if not already routed*/
1259 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1260 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1262 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1263 streq(child_sa
->get_name(child_sa
), child_cfg
->get_name(child_cfg
)))
1265 iterator
->destroy(iterator
);
1266 DBG1(DBG_IKE
, "routing CHILD_SA failed: already routed");
1270 iterator
->destroy(iterator
);
1272 switch (this->state
)
1276 DBG1(DBG_IKE
, "routing CHILD_SA failed: IKE_SA is %N",
1277 ike_sa_state_names
, this->state
);
1280 case IKE_CONNECTING
:
1281 case IKE_ESTABLISHED
:
1287 resolve_hosts(this);
1289 /* install kernel policies */
1290 child_sa
= child_sa_create(this->my_host
, this->other_host
,
1291 child_cfg
, 0, FALSE
);
1293 if (this->my_virtual_ip
)
1295 me
= this->my_virtual_ip
;
1297 other
= this->other_host
;
1298 if (this->other_virtual_ip
)
1300 other
= this->other_virtual_ip
;
1303 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, me
);
1304 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, other
);
1306 child_sa
->set_mode(child_sa
, child_cfg
->get_mode(child_cfg
));
1307 status
= child_sa
->add_policies(child_sa
, my_ts
, other_ts
);
1309 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
1310 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
1311 if (status
== SUCCESS
)
1313 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1314 DBG1(DBG_IKE
, "CHILD_SA routed");
1318 child_sa
->destroy(child_sa
);
1319 DBG1(DBG_IKE
, "routing CHILD_SA failed");
1325 * Implementation of ike_sa_t.unroute.
1327 static status_t
unroute(private_ike_sa_t
*this, u_int32_t reqid
)
1329 iterator_t
*iterator
;
1330 child_sa_t
*child_sa
;
1333 /* find CHILD_SA in ROUTED state */
1334 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1335 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1337 if (child_sa
->get_state(child_sa
) == CHILD_ROUTED
&&
1338 child_sa
->get_reqid(child_sa
) == reqid
)
1340 iterator
->remove(iterator
);
1341 DBG1(DBG_IKE
, "CHILD_SA unrouted");
1342 child_sa
->destroy(child_sa
);
1347 iterator
->destroy(iterator
);
1351 DBG1(DBG_IKE
, "unrouting CHILD_SA failed: reqid %d not found", reqid
);
1354 /* if we are not established, and we have no more routed childs, remove whole SA */
1355 if (this->state
== IKE_CREATED
&&
1356 this->child_sas
->get_count(this->child_sas
) == 0)
1364 * Implementation of ike_sa_t.process_message.
1366 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
1371 if (this->state
== IKE_PASSIVE
)
1372 { /* do not handle messages in passive state */
1376 is_request
= message
->get_request(message
);
1378 status
= message
->parse_body(message
,
1379 this->keymat
->get_crypter(this->keymat
, TRUE
),
1380 this->keymat
->get_signer(this->keymat
, TRUE
));
1381 if (status
!= SUCCESS
)
1389 DBG1(DBG_IKE
, "critical unknown payloads found");
1392 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
1396 DBG1(DBG_IKE
, "message parsing failed");
1399 send_notify_response(this, message
, INVALID_SYNTAX
);
1403 DBG1(DBG_IKE
, "message verification failed");
1406 send_notify_response(this, message
, INVALID_SYNTAX
);
1410 DBG1(DBG_IKE
, "integrity check failed");
1414 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1417 send_notify_response(this, message
, INVALID_SYNTAX
);
1423 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1424 exchange_type_names
, message
->get_exchange_type(message
),
1425 message
->get_request(message
) ?
"request" : "response",
1426 message
->get_message_id(message
));
1428 if (this->state
== IKE_CREATED
)
1429 { /* invalid initiation attempt, close SA */
1437 private_ike_sa_t
*new;
1438 iterator_t
*iterator
;
1440 bool has_routed
= FALSE
;
1442 me
= message
->get_destination(message
);
1443 other
= message
->get_source(message
);
1445 /* if this IKE_SA is virgin, we check for a config */
1446 if (this->ike_cfg
== NULL
)
1449 this->ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1451 if (this->ike_cfg
== NULL
)
1453 /* no config found for these hosts, destroy */
1454 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1455 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1456 send_notify_response(this, message
, NO_PROPOSAL_CHOSEN
);
1459 /* add a timeout if peer does not establish it completely */
1460 job
= (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, FALSE
);
1461 charon
->scheduler
->schedule_job(charon
->scheduler
, job
,
1462 HALF_OPEN_IKE_SA_TIMEOUT
);
1464 this->stats
[STAT_INBOUND
] = time(NULL
);
1465 /* check if message is trustworthy, and update host information */
1466 if (this->state
== IKE_CREATED
|| this->state
== IKE_CONNECTING
||
1467 message
->get_exchange_type(message
) != IKE_SA_INIT
)
1469 if (!supports_extension(this, EXT_MOBIKE
))
1470 { /* with MOBIKE, we do no implicit updates */
1471 update_hosts(this, me
, other
);
1474 status
= this->task_manager
->process_message(this->task_manager
, message
);
1475 if (status
!= DESTROY_ME
)
1479 /* if IKE_SA gets closed for any reasons, reroute routed children */
1480 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1481 while (iterator
->iterate(iterator
, (void**)&child
))
1483 if (child
->get_state(child
) == CHILD_ROUTED
)
1489 iterator
->destroy(iterator
);
1494 /* move routed children to a new IKE_SA, apply connection info */
1495 new = (private_ike_sa_t
*)charon
->ike_sa_manager
->checkout_new(
1496 charon
->ike_sa_manager
, TRUE
);
1497 set_peer_cfg(new, this->peer_cfg
);
1498 new->other_host
->destroy(new->other_host
);
1499 new->other_host
= this->other_host
->clone(this->other_host
);
1500 if (!has_condition(this, COND_NAT_THERE
))
1502 new->other_host
->set_port(new->other_host
, IKEV2_UDP_PORT
);
1504 if (this->my_virtual_ip
)
1506 set_virtual_ip(new, TRUE
, this->my_virtual_ip
);
1508 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1509 while (iterator
->iterate(iterator
, (void**)&child
))
1511 if (child
->get_state(child
) == CHILD_ROUTED
)
1513 route(new, child
->get_config(child
));
1516 iterator
->destroy(iterator
);
1517 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, &new->public);
1523 * Implementation of ike_sa_t.get_id.
1525 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1527 return this->ike_sa_id
;
1531 * Implementation of ike_sa_t.get_my_id.
1533 static identification_t
* get_my_id(private_ike_sa_t
*this)
1539 * Implementation of ike_sa_t.set_my_id.
1541 static void set_my_id(private_ike_sa_t
*this, identification_t
*me
)
1543 DESTROY_IF(this->my_id
);
1548 * Implementation of ike_sa_t.get_other_id.
1550 static identification_t
* get_other_id(private_ike_sa_t
*this)
1552 return this->other_id
;
1556 * Implementation of ike_sa_t.set_other_id.
1558 static void set_other_id(private_ike_sa_t
*this, identification_t
*other
)
1560 DESTROY_IF(this->other_id
);
1561 this->other_id
= other
;
1565 * Implementation of ike_sa_t.get_eap_identity.
1567 static identification_t
* get_eap_identity(private_ike_sa_t
*this)
1569 return this->eap_identity
;
1573 * Implementation of ike_sa_t.set_eap_identity.
1575 static void set_eap_identity(private_ike_sa_t
*this, identification_t
*id
)
1577 DESTROY_IF(this->eap_identity
);
1578 this->eap_identity
= id
;
1582 * Implementation of ike_sa_t.add_child_sa.
1584 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1586 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1590 * Implementation of ike_sa_t.get_child_sa.
1592 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1593 u_int32_t spi
, bool inbound
)
1595 iterator_t
*iterator
;
1596 child_sa_t
*current
, *found
= NULL
;
1598 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1599 while (iterator
->iterate(iterator
, (void**)¤t
))
1601 if (current
->get_spi(current
, inbound
) == spi
&&
1602 current
->get_protocol(current
) == protocol
)
1607 iterator
->destroy(iterator
);
1612 * Implementation of ike_sa_t.create_child_sa_iterator.
1614 static iterator_t
* create_child_sa_iterator(private_ike_sa_t
*this)
1616 return this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1620 * Implementation of ike_sa_t.rekey_child_sa.
1622 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1625 child_rekey_t
*child_rekey
;
1627 child_rekey
= child_rekey_create(&this->public, protocol
, spi
);
1628 this->task_manager
->queue_task(this->task_manager
, &child_rekey
->task
);
1629 return this->task_manager
->initiate(this->task_manager
);
1633 * Implementation of ike_sa_t.delete_child_sa.
1635 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1638 child_delete_t
*child_delete
;
1640 child_delete
= child_delete_create(&this->public, protocol
, spi
);
1641 this->task_manager
->queue_task(this->task_manager
, &child_delete
->task
);
1642 return this->task_manager
->initiate(this->task_manager
);
1646 * Implementation of ike_sa_t.destroy_child_sa.
1648 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1651 iterator_t
*iterator
;
1652 child_sa_t
*child_sa
;
1653 status_t status
= NOT_FOUND
;
1655 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1656 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1658 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1659 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1661 child_sa
->destroy(child_sa
);
1662 iterator
->remove(iterator
);
1667 iterator
->destroy(iterator
);
1672 * Implementation of public_ike_sa_t.delete.
1674 static status_t
delete_(private_ike_sa_t
*this)
1676 ike_delete_t
*ike_delete
;
1678 switch (this->state
)
1680 case IKE_ESTABLISHED
:
1682 ike_delete
= ike_delete_create(&this->public, TRUE
);
1683 this->task_manager
->queue_task(this->task_manager
, &ike_delete
->task
);
1684 return this->task_manager
->initiate(this->task_manager
);
1686 DBG1(DBG_IKE
, "deleting unestablished IKE_SA");
1691 DBG1(DBG_IKE
, "destroying IKE_SA in state %N "
1692 "without notification", ike_sa_state_names
, this->state
);
1699 * Implementation of ike_sa_t.rekey.
1701 static status_t
rekey(private_ike_sa_t
*this)
1703 ike_rekey_t
*ike_rekey
;
1705 ike_rekey
= ike_rekey_create(&this->public, TRUE
);
1707 this->task_manager
->queue_task(this->task_manager
, &ike_rekey
->task
);
1708 return this->task_manager
->initiate(this->task_manager
);
1712 * Implementation of ike_sa_t.reauth
1714 static status_t
reauth(private_ike_sa_t
*this)
1718 /* we can't reauthenticate as responder when we use EAP or virtual IPs.
1719 * If the peer does not support RFC4478, there is no way to keep the
1721 if (!has_condition(this, COND_ORIGINAL_INITIATOR
))
1723 DBG1(DBG_IKE
, "initiator did not reauthenticate as requested");
1724 if (this->other_virtual_ip
!= NULL
||
1725 has_condition(this, COND_EAP_AUTHENTICATED
)
1727 /* if we are mediation server we too cannot reauth the IKE_SA */
1728 || this->is_mediation_server
1732 time_t now
= time(NULL
);
1734 DBG1(DBG_IKE
, "IKE_SA will timeout in %V",
1735 &now
, &this->stats
[STAT_DELETE
]);
1740 DBG1(DBG_IKE
, "reauthenticating actively");
1743 task
= (task_t
*)ike_reauth_create(&this->public);
1744 this->task_manager
->queue_task(this->task_manager
, task
);
1746 return this->task_manager
->initiate(this->task_manager
);
1750 * Implementation of ike_sa_t.reestablish
1752 static status_t
reestablish(private_ike_sa_t
*this)
1757 iterator_t
*iterator
;
1758 child_sa_t
*child_sa
;
1759 child_cfg_t
*child_cfg
;
1760 bool required
= FALSE
;
1761 status_t status
= FAILED
;
1763 /* check if we have children to keep up at all*/
1764 iterator
= create_child_sa_iterator(this);
1765 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1767 child_cfg
= child_sa
->get_config(child_sa
);
1768 if (this->state
== IKE_DELETING
)
1770 action
= child_cfg
->get_close_action(child_cfg
);
1774 action
= child_cfg
->get_dpd_action(child_cfg
);
1778 case ACTION_RESTART
:
1785 iterator
->destroy(iterator
);
1787 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
1788 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1798 /* check if we are able to reestablish this IKE_SA */
1799 if (!has_condition(this, COND_ORIGINAL_INITIATOR
) &&
1800 (this->other_virtual_ip
!= NULL
||
1801 has_condition(this, COND_EAP_AUTHENTICATED
)
1803 || this->is_mediation_server
1807 DBG1(DBG_IKE
, "unable to reestablish IKE_SA due asymetric setup");
1811 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
, TRUE
);
1812 new->set_peer_cfg(new, this->peer_cfg
);
1813 host
= this->other_host
;
1814 new->set_other_host(new, host
->clone(host
));
1815 host
= this->my_host
;
1816 new->set_my_host(new, host
->clone(host
));
1817 /* if we already have a virtual IP, we reuse it */
1818 host
= this->my_virtual_ip
;
1821 new->set_virtual_ip(new, TRUE
, host
);
1825 if (this->peer_cfg
->is_mediation(this->peer_cfg
))
1827 status
= new->initiate(new, NULL
);
1832 iterator
= create_child_sa_iterator(this);
1833 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1835 child_cfg
= child_sa
->get_config(child_sa
);
1836 if (this->state
== IKE_DELETING
)
1838 action
= child_cfg
->get_close_action(child_cfg
);
1842 action
= child_cfg
->get_dpd_action(child_cfg
);
1846 case ACTION_RESTART
:
1847 DBG1(DBG_IKE
, "restarting CHILD_SA %s",
1848 child_cfg
->get_name(child_cfg
));
1849 child_cfg
->get_ref(child_cfg
);
1850 status
= new->initiate(new, child_cfg
);
1853 status
= new->route(new, child_cfg
);
1858 if (status
== DESTROY_ME
)
1863 iterator
->destroy(iterator
);
1866 if (status
== DESTROY_ME
)
1868 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1873 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1879 * Implementation of ike_sa_t.retransmit.
1881 static status_t
retransmit(private_ike_sa_t
*this, u_int32_t message_id
)
1883 this->stats
[STAT_OUTBOUND
] = time(NULL
);
1884 if (this->task_manager
->retransmit(this->task_manager
, message_id
) != SUCCESS
)
1886 /* send a proper signal to brief interested bus listeners */
1887 switch (this->state
)
1889 case IKE_CONNECTING
:
1891 /* retry IKE_SA_INIT if we have multiple keyingtries */
1892 u_int32_t tries
= this->peer_cfg
->get_keyingtries(this->peer_cfg
);
1894 if (tries
== 0 || tries
> this->keyingtry
)
1896 DBG1(DBG_IKE
, "peer not responding, trying again (%d/%d)",
1897 this->keyingtry
+ 1, tries
);
1899 return this->task_manager
->initiate(this->task_manager
);
1901 DBG1(DBG_IKE
, "establishing IKE_SA failed, peer not responding");
1905 DBG1(DBG_IKE
, "proper IKE_SA delete failed, peer not responding");
1908 DBG1(DBG_IKE
, "rekeying IKE_SA failed, peer not responding");
1920 * Implementation of ike_sa_t.set_auth_lifetime.
1922 static void set_auth_lifetime(private_ike_sa_t
*this, u_int32_t lifetime
)
1924 u_int32_t reduction
= this->peer_cfg
->get_over_time(this->peer_cfg
);
1925 u_int32_t reauth_time
= time(NULL
) + lifetime
- reduction
;
1927 if (lifetime
< reduction
)
1929 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, starting reauthentication",
1931 charon
->processor
->queue_job(charon
->processor
,
1932 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
));
1934 else if (this->stats
[STAT_REAUTH
] == 0 ||
1935 this->stats
[STAT_REAUTH
] > reauth_time
)
1937 this->stats
[STAT_REAUTH
] = reauth_time
;
1938 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, scheduling reauthentication"
1939 " in %ds", lifetime
, lifetime
- reduction
);
1940 charon
->scheduler
->schedule_job(charon
->scheduler
,
1941 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
),
1942 lifetime
- reduction
);
1946 DBG1(DBG_IKE
, "received AUTH_LIFETIME of %ds, reauthentication already "
1947 "scheduled in %ds", lifetime
, this->stats
[STAT_REAUTH
] - time(NULL
));
1952 * Implementation of ike_sa_t.roam.
1954 static status_t
roam(private_ike_sa_t
*this, bool address
)
1957 ike_mobike_t
*mobike
;
1959 switch (this->state
)
1968 /* responder just updates the peer about changed address config */
1969 if (!this->ike_sa_id
->is_initiator(this->ike_sa_id
))
1971 if (supports_extension(this, EXT_MOBIKE
) && address
)
1973 DBG1(DBG_IKE
, "sending address list update using MOBIKE");
1974 mobike
= ike_mobike_create(&this->public, TRUE
);
1975 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
1976 return this->task_manager
->initiate(this->task_manager
);
1981 /* keep existing path if possible */
1982 src
= charon
->kernel_interface
->get_source_addr(charon
->kernel_interface
,
1983 this->other_host
, this->my_host
);
1986 if (src
->ip_equals(src
, this->my_host
))
1988 DBG2(DBG_IKE
, "keeping connection path %H - %H",
1989 src
, this->other_host
);
1996 /* update addresses with mobike, if supported ... */
1997 if (supports_extension(this, EXT_MOBIKE
))
1999 DBG1(DBG_IKE
, "requesting address change using MOBIKE");
2000 mobike
= ike_mobike_create(&this->public, TRUE
);
2001 mobike
->roam(mobike
, address
);
2002 this->task_manager
->queue_task(this->task_manager
, (task_t
*)mobike
);
2003 return this->task_manager
->initiate(this->task_manager
);
2005 DBG1(DBG_IKE
, "reauthenticating IKE_SA due to address change");
2006 /* ... reauth if not */
2007 return reauth(this);
2011 * Implementation of ike_sa_t.add_configuration_attribute
2013 static void add_configuration_attribute(private_ike_sa_t
*this,
2014 configuration_attribute_type_t type
, chunk_t data
)
2016 attribute_entry_t
*entry
;
2017 attribute_handler_t
*handler
;
2019 handler
= charon
->attributes
->handle(charon
->attributes
,
2020 &this->public, type
, data
);
2023 entry
= malloc_thing(attribute_entry_t
);
2024 entry
->handler
= handler
;
2026 entry
->data
= chunk_clone(data
);
2028 this->attributes
->insert_last(this->attributes
, entry
);
2033 * Implementation of ike_sa_t.inherit.
2035 static status_t
inherit(private_ike_sa_t
*this, private_ike_sa_t
*other
)
2037 child_sa_t
*child_sa
;
2038 attribute_entry_t
*entry
;
2040 /* apply hosts and ids */
2041 this->my_host
->destroy(this->my_host
);
2042 this->other_host
->destroy(this->other_host
);
2043 this->my_id
->destroy(this->my_id
);
2044 this->other_id
->destroy(this->other_id
);
2045 this->my_host
= other
->my_host
->clone(other
->my_host
);
2046 this->other_host
= other
->other_host
->clone(other
->other_host
);
2047 this->my_id
= other
->my_id
->clone(other
->my_id
);
2048 this->other_id
= other
->other_id
->clone(other
->other_id
);
2050 /* apply virtual assigned IPs... */
2051 if (other
->my_virtual_ip
)
2053 this->my_virtual_ip
= other
->my_virtual_ip
;
2054 other
->my_virtual_ip
= NULL
;
2056 if (other
->other_virtual_ip
)
2058 this->other_virtual_ip
= other
->other_virtual_ip
;
2059 other
->other_virtual_ip
= NULL
;
2062 /* ... and configuration attributes */
2063 while (other
->attributes
->remove_last(other
->attributes
,
2064 (void**)&entry
) == SUCCESS
)
2066 this->attributes
->insert_first(this->attributes
, entry
);
2069 /* inherit all conditions */
2070 this->conditions
= other
->conditions
;
2071 if (this->conditions
& COND_NAT_HERE
)
2073 send_keepalive(this);
2077 if (other
->is_mediation_server
)
2079 act_as_mediation_server(this);
2081 else if (other
->server_reflexive_host
)
2083 this->server_reflexive_host
= other
->server_reflexive_host
->clone(
2084 other
->server_reflexive_host
);
2088 /* adopt all children */
2089 while (other
->child_sas
->remove_last(other
->child_sas
,
2090 (void**)&child_sa
) == SUCCESS
)
2092 this->child_sas
->insert_first(this->child_sas
, (void*)child_sa
);
2095 /* move pending tasks to the new IKE_SA */
2096 this->task_manager
->adopt_tasks(this->task_manager
, other
->task_manager
);
2098 /* reauthentication timeout survives a rekeying */
2099 if (other
->stats
[STAT_REAUTH
])
2101 time_t reauth
, delete, now
= time(NULL
);
2103 this->stats
[STAT_REAUTH
] = other
->stats
[STAT_REAUTH
];
2104 reauth
= this->stats
[STAT_REAUTH
] - now
;
2105 delete = reauth
+ this->peer_cfg
->get_over_time(this->peer_cfg
);
2106 this->stats
[STAT_DELETE
] = this->stats
[STAT_REAUTH
] + delete;
2107 DBG1(DBG_IKE
, "rescheduling reauthentication in %ds after rekeying, "
2108 "lifetime reduced to %ds", reauth
, delete);
2109 charon
->scheduler
->schedule_job(charon
->scheduler
,
2110 (job_t
*)rekey_ike_sa_job_create(this->ike_sa_id
, TRUE
), reauth
);
2111 charon
->scheduler
->schedule_job(charon
->scheduler
,
2112 (job_t
*)delete_ike_sa_job_create(this->ike_sa_id
, TRUE
), delete);
2114 /* we have to initate here, there may be new tasks to handle */
2115 return this->task_manager
->initiate(this->task_manager
);
2119 * Implementation of ike_sa_t.destroy.
2121 static void destroy(private_ike_sa_t
*this)
2123 attribute_entry_t
*entry
;
2125 charon
->bus
->set_sa(charon
->bus
, &this->public);
2127 set_state(this, IKE_DESTROYING
);
2129 /* remove attributes first, as we pass the IKE_SA to the handler */
2130 while (this->attributes
->remove_last(this->attributes
,
2131 (void**)&entry
) == SUCCESS
)
2133 charon
->attributes
->release(charon
->attributes
, entry
->handler
,
2134 &this->public, entry
->type
, entry
->data
);
2135 free(entry
->data
.ptr
);
2138 this->attributes
->destroy(this->attributes
);
2140 this->child_sas
->destroy_offset(this->child_sas
, offsetof(child_sa_t
, destroy
));
2142 /* unset SA after here to avoid usage by the listeners */
2143 charon
->bus
->set_sa(charon
->bus
, NULL
);
2145 this->task_manager
->destroy(this->task_manager
);
2146 this->keymat
->destroy(this->keymat
);
2148 if (this->my_virtual_ip
)
2150 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
2151 this->my_virtual_ip
);
2152 this->my_virtual_ip
->destroy(this->my_virtual_ip
);
2154 if (this->other_virtual_ip
)
2156 if (this->peer_cfg
&& this->peer_cfg
->get_pool(this->peer_cfg
))
2158 charon
->attributes
->release_address(charon
->attributes
,
2159 this->peer_cfg
->get_pool(this->peer_cfg
),
2160 this->other_virtual_ip
, this->other_id
);
2162 this->other_virtual_ip
->destroy(this->other_virtual_ip
);
2164 this->additional_addresses
->destroy_offset(this->additional_addresses
,
2165 offsetof(host_t
, destroy
));
2167 if (this->is_mediation_server
)
2169 charon
->mediation_manager
->remove(charon
->mediation_manager
, this->ike_sa_id
);
2171 DESTROY_IF(this->server_reflexive_host
);
2172 chunk_free(&this->connect_id
);
2174 free(this->nat_detection_dest
.ptr
);
2176 DESTROY_IF(this->my_host
);
2177 DESTROY_IF(this->other_host
);
2178 DESTROY_IF(this->my_id
);
2179 DESTROY_IF(this->other_id
);
2180 DESTROY_IF(this->local_host
);
2181 DESTROY_IF(this->remote_host
);
2182 DESTROY_IF(this->eap_identity
);
2184 DESTROY_IF(this->ike_cfg
);
2185 DESTROY_IF(this->peer_cfg
);
2186 DESTROY_IF(this->proposal
);
2187 this->my_auth
->destroy(this->my_auth
);
2188 this->other_auth
->destroy(this->other_auth
);
2190 this->ike_sa_id
->destroy(this->ike_sa_id
);
2195 * Described in header.
2197 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
2199 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
2200 static u_int32_t unique_id
= 0;
2202 /* Public functions */
2203 this->public.get_state
= (ike_sa_state_t (*)(ike_sa_t
*)) get_state
;
2204 this->public.set_state
= (void (*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
2205 this->public.get_name
= (char* (*)(ike_sa_t
*))get_name
;
2206 this->public.get_statistic
= (u_int32_t(*)(ike_sa_t
*, statistic_t kind
))get_statistic
;
2207 this->public.process_message
= (status_t (*)(ike_sa_t
*, message_t
*)) process_message
;
2208 this->public.initiate
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) initiate
;
2209 this->public.route
= (status_t (*)(ike_sa_t
*,child_cfg_t
*)) route
;
2210 this->public.unroute
= (status_t (*)(ike_sa_t
*,u_int32_t
)) unroute
;
2211 this->public.acquire
= (status_t (*)(ike_sa_t
*,u_int32_t
)) acquire
;
2212 this->public.get_ike_cfg
= (ike_cfg_t
* (*)(ike_sa_t
*))get_ike_cfg
;
2213 this->public.set_ike_cfg
= (void (*)(ike_sa_t
*,ike_cfg_t
*))set_ike_cfg
;
2214 this->public.get_peer_cfg
= (peer_cfg_t
* (*)(ike_sa_t
*))get_peer_cfg
;
2215 this->public.set_peer_cfg
= (void (*)(ike_sa_t
*,peer_cfg_t
*))set_peer_cfg
;
2216 this->public.get_auth_cfg
= (auth_cfg_t
*(*)(ike_sa_t
*, bool local
))get_auth_cfg
;
2217 this->public.get_proposal
= (proposal_t
*(*)(ike_sa_t
*))get_proposal
;
2218 this->public.set_proposal
= (void(*)(ike_sa_t
*, proposal_t
*proposal
))set_proposal
;
2219 this->public.get_id
= (ike_sa_id_t
* (*)(ike_sa_t
*)) get_id
;
2220 this->public.get_my_host
= (host_t
* (*)(ike_sa_t
*)) get_my_host
;
2221 this->public.set_my_host
= (void (*)(ike_sa_t
*,host_t
*)) set_my_host
;
2222 this->public.get_other_host
= (host_t
* (*)(ike_sa_t
*)) get_other_host
;
2223 this->public.set_other_host
= (void (*)(ike_sa_t
*,host_t
*)) set_other_host
;
2224 this->public.set_message_id
= (void(*)(ike_sa_t
*, bool inbound
, u_int32_t mid
))set_message_id
;
2225 this->public.update_hosts
= (void(*)(ike_sa_t
*, host_t
*me
, host_t
*other
))update_hosts
;
2226 this->public.get_my_id
= (identification_t
* (*)(ike_sa_t
*)) get_my_id
;
2227 this->public.set_my_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_my_id
;
2228 this->public.get_other_id
= (identification_t
* (*)(ike_sa_t
*)) get_other_id
;
2229 this->public.set_other_id
= (void (*)(ike_sa_t
*,identification_t
*)) set_other_id
;
2230 this->public.get_eap_identity
= (identification_t
* (*)(ike_sa_t
*)) get_eap_identity
;
2231 this->public.set_eap_identity
= (void (*)(ike_sa_t
*,identification_t
*)) set_eap_identity
;
2232 this->public.enable_extension
= (void(*)(ike_sa_t
*, ike_extension_t extension
))enable_extension
;
2233 this->public.supports_extension
= (bool(*)(ike_sa_t
*, ike_extension_t extension
))supports_extension
;
2234 this->public.set_condition
= (void (*)(ike_sa_t
*, ike_condition_t
,bool)) set_condition
;
2235 this->public.has_condition
= (bool (*)(ike_sa_t
*,ike_condition_t
)) has_condition
;
2236 this->public.set_pending_updates
= (void(*)(ike_sa_t
*, u_int32_t updates
))set_pending_updates
;
2237 this->public.get_pending_updates
= (u_int32_t(*)(ike_sa_t
*))get_pending_updates
;
2238 this->public.create_additional_address_iterator
= (iterator_t
*(*)(ike_sa_t
*))create_additional_address_iterator
;
2239 this->public.add_additional_address
= (void(*)(ike_sa_t
*, host_t
*host
))add_additional_address
;
2240 this->public.has_mapping_changed
= (bool(*)(ike_sa_t
*, chunk_t hash
))has_mapping_changed
;
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_keymat
= (keymat_t
*(*)(ike_sa_t
*))get_keymat
;
2247 this->public.add_child_sa
= (void (*)(ike_sa_t
*,child_sa_t
*)) add_child_sa
;
2248 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
2249 this->public.create_child_sa_iterator
= (iterator_t
* (*)(ike_sa_t
*)) create_child_sa_iterator
;
2250 this->public.rekey_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
2251 this->public.delete_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
2252 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
2253 this->public.rekey
= (status_t (*)(ike_sa_t
*))rekey
;
2254 this->public.reauth
= (status_t (*)(ike_sa_t
*))reauth
;
2255 this->public.reestablish
= (status_t (*)(ike_sa_t
*))reestablish
;
2256 this->public.set_auth_lifetime
= (void(*)(ike_sa_t
*, u_int32_t lifetime
))set_auth_lifetime
;
2257 this->public.roam
= (status_t(*)(ike_sa_t
*,bool))roam
;
2258 this->public.inherit
= (status_t (*)(ike_sa_t
*,ike_sa_t
*))inherit
;
2259 this->public.generate_message
= (status_t (*)(ike_sa_t
*,message_t
*,packet_t
**))generate_message
;
2260 this->public.reset
= (void (*)(ike_sa_t
*))reset
;
2261 this->public.get_unique_id
= (u_int32_t (*)(ike_sa_t
*))get_unique_id
;
2262 this->public.set_virtual_ip
= (void (*)(ike_sa_t
*,bool,host_t
*))set_virtual_ip
;
2263 this->public.get_virtual_ip
= (host_t
* (*)(ike_sa_t
*,bool))get_virtual_ip
;
2264 this->public.add_configuration_attribute
= (void(*)(ike_sa_t
*, configuration_attribute_type_t type
, chunk_t data
))add_configuration_attribute
;
2265 this->public.set_kmaddress
= (void (*)(ike_sa_t
*,host_t
*,host_t
*))set_kmaddress
;
2267 this->public.act_as_mediation_server
= (void (*)(ike_sa_t
*)) act_as_mediation_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.get_connect_id
= (chunk_t (*)(ike_sa_t
*)) get_connect_id
;
2271 this->public.initiate_mediation
= (status_t (*)(ike_sa_t
*,peer_cfg_t
*)) initiate_mediation
;
2272 this->public.initiate_mediated
= (status_t (*)(ike_sa_t
*,host_t
*,host_t
*,chunk_t
)) initiate_mediated
;
2273 this->public.relay
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
,chunk_t
,linked_list_t
*,bool)) relay
;
2274 this->public.callback
= (status_t (*)(ike_sa_t
*,identification_t
*)) callback
;
2275 this->public.respond
= (status_t (*)(ike_sa_t
*,identification_t
*,chunk_t
)) respond
;
2278 /* initialize private fields */
2279 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
2280 this->child_sas
= linked_list_create();
2281 this->my_host
= host_create_any(AF_INET
);
2282 this->my_host
->set_port(this->my_host
, IKEV2_UDP_PORT
);
2283 this->other_host
= host_create_any(AF_INET
);
2284 this->my_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2285 this->other_id
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
2286 this->eap_identity
= NULL
;
2287 this->extensions
= 0;
2288 this->conditions
= 0;
2289 this->keymat
= keymat_create(ike_sa_id
->is_initiator(ike_sa_id
));
2290 this->state
= IKE_CREATED
;
2291 this->keepalive_interval
= lib
->settings
->get_time(lib
->settings
,
2292 "charon.keep_alive", KEEPALIVE_INTERVAL
);
2293 memset(this->stats
, 0, sizeof(this->stats
));
2294 this->stats
[STAT_INBOUND
] = this->stats
[STAT_OUTBOUND
] = time(NULL
);
2295 this->ike_cfg
= NULL
;
2296 this->peer_cfg
= NULL
;
2297 this->my_auth
= auth_cfg_create();
2298 this->other_auth
= auth_cfg_create();
2299 this->proposal
= NULL
;
2300 this->task_manager
= task_manager_create(&this->public);
2301 this->unique_id
= ++unique_id
;
2302 this->my_virtual_ip
= NULL
;
2303 this->other_virtual_ip
= NULL
;
2304 this->additional_addresses
= linked_list_create();
2305 this->attributes
= linked_list_create();
2306 this->nat_detection_dest
= chunk_empty
;
2307 this->pending_updates
= 0;
2308 this->keyingtry
= 0;
2309 this->local_host
= NULL
;
2310 this->remote_host
= NULL
;
2312 this->is_mediation_server
= FALSE
;
2313 this->server_reflexive_host
= NULL
;
2314 this->connect_id
= chunk_empty
;
2317 return &this->public;