2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2010 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "task_manager_v2.h"
21 #include <collections/array.h>
23 #include <sa/ikev2/tasks/ike_init.h>
24 #include <sa/ikev2/tasks/ike_natd.h>
25 #include <sa/ikev2/tasks/ike_mobike.h>
26 #include <sa/ikev2/tasks/ike_auth.h>
27 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
28 #include <sa/ikev2/tasks/ike_cert_pre.h>
29 #include <sa/ikev2/tasks/ike_cert_post.h>
30 #include <sa/ikev2/tasks/ike_rekey.h>
31 #include <sa/ikev2/tasks/ike_reauth.h>
32 #include <sa/ikev2/tasks/ike_delete.h>
33 #include <sa/ikev2/tasks/ike_config.h>
34 #include <sa/ikev2/tasks/ike_dpd.h>
35 #include <sa/ikev2/tasks/ike_vendor.h>
36 #include <sa/ikev2/tasks/child_create.h>
37 #include <sa/ikev2/tasks/child_rekey.h>
38 #include <sa/ikev2/tasks/child_delete.h>
39 #include <encoding/payloads/delete_payload.h>
40 #include <encoding/payloads/unknown_payload.h>
41 #include <processing/jobs/retransmit_job.h>
42 #include <processing/jobs/delete_ike_sa_job.h>
45 #include <sa/ikev2/tasks/ike_me.h>
48 typedef struct exchange_t exchange_t
;
51 * An exchange in the air, used do detect and handle retransmission
56 * Message ID used for this transaction
61 * generated packet for retransmission
66 typedef struct private_task_manager_t private_task_manager_t
;
69 * private data of the task manager
71 struct private_task_manager_t
{
76 task_manager_v2_t
public;
79 * associated IKE_SA we are serving
84 * Exchange we are currently handling as responder
88 * Message ID of the exchange
93 * packet for retransmission
100 * Exchange we are currently handling as initiator
104 * Message ID of the exchange
109 * how many times we have retransmitted so far
114 * packet for retransmission
119 * type of the initated exchange
121 exchange_type_t type
;
126 * Array of queued tasks not yet in action
128 array_t
*queued_tasks
;
131 * Array of active tasks, initiated by ourselve
133 array_t
*active_tasks
;
136 * Array of tasks initiated by peer
138 array_t
*passive_tasks
;
141 * the task manager has been reset
146 * Number of times we retransmit messages before giving up
148 u_int retransmit_tries
;
151 * Retransmission timeout
153 double retransmit_timeout
;
156 * Base to calculate retransmission timeout
158 double retransmit_base
;
161 METHOD(task_manager_t
, flush_queue
, void,
162 private_task_manager_t
*this, task_queue_t queue
)
169 case TASK_QUEUE_ACTIVE
:
170 array
= this->active_tasks
;
172 case TASK_QUEUE_PASSIVE
:
173 array
= this->passive_tasks
;
175 case TASK_QUEUE_QUEUED
:
176 array
= this->queued_tasks
;
181 while (array_remove(array
, ARRAY_TAIL
, &task
))
187 METHOD(task_manager_t
, flush
, void,
188 private_task_manager_t
*this)
190 flush_queue(this, TASK_QUEUE_QUEUED
);
191 flush_queue(this, TASK_QUEUE_PASSIVE
);
192 flush_queue(this, TASK_QUEUE_ACTIVE
);
196 * move a task of a specific type from the queue to the active list
198 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
200 enumerator_t
*enumerator
;
204 enumerator
= array_create_enumerator(this->queued_tasks
);
205 while (enumerator
->enumerate(enumerator
, (void**)&task
))
207 if (task
->get_type(task
) == type
)
209 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
210 array_remove_at(this->queued_tasks
, enumerator
);
211 array_insert(this->active_tasks
, ARRAY_TAIL
, task
);
216 enumerator
->destroy(enumerator
);
220 METHOD(task_manager_t
, retransmit
, status_t
,
221 private_task_manager_t
*this, u_int32_t message_id
)
223 if (this->initiating
.packet
&& message_id
== this->initiating
.mid
)
227 enumerator_t
*enumerator
;
230 ike_mobike_t
*mobike
= NULL
;
232 /* check if we are retransmitting a MOBIKE routability check */
233 enumerator
= array_create_enumerator(this->active_tasks
);
234 while (enumerator
->enumerate(enumerator
, (void*)&task
))
236 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
238 mobike
= (ike_mobike_t
*)task
;
239 if (!mobike
->is_probing(mobike
))
246 enumerator
->destroy(enumerator
);
250 if (this->initiating
.retransmitted
<= this->retransmit_tries
)
252 timeout
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
253 pow(this->retransmit_base
, this->initiating
.retransmitted
));
257 DBG1(DBG_IKE
, "giving up after %d retransmits",
258 this->initiating
.retransmitted
- 1);
259 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
,
260 this->initiating
.packet
);
264 if (this->initiating
.retransmitted
)
266 DBG1(DBG_IKE
, "retransmit %d of request with message ID %d",
267 this->initiating
.retransmitted
, message_id
);
268 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
,
269 this->initiating
.packet
);
271 packet
= this->initiating
.packet
->clone(this->initiating
.packet
);
272 charon
->sender
->send(charon
->sender
, packet
);
275 { /* for routeability checks, we use a more aggressive behavior */
276 if (this->initiating
.retransmitted
<= ROUTEABILITY_CHECK_TRIES
)
278 timeout
= ROUTEABILITY_CHECK_INTERVAL
;
282 DBG1(DBG_IKE
, "giving up after %d path probings",
283 this->initiating
.retransmitted
- 1);
287 if (this->initiating
.retransmitted
)
289 DBG1(DBG_IKE
, "path probing attempt %d",
290 this->initiating
.retransmitted
);
292 mobike
->transmit(mobike
, this->initiating
.packet
);
295 this->initiating
.retransmitted
++;
296 job
= (job_t
*)retransmit_job_create(this->initiating
.mid
,
297 this->ike_sa
->get_id(this->ike_sa
));
298 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, timeout
);
303 METHOD(task_manager_t
, initiate
, status_t
,
304 private_task_manager_t
*this)
306 enumerator_t
*enumerator
;
311 exchange_type_t exchange
= 0;
313 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
)
315 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
316 exchange_type_names
, this->initiating
.type
);
317 /* do not initiate if we already have a message in the air */
321 if (array_count(this->active_tasks
) == 0)
323 DBG2(DBG_IKE
, "activating new tasks");
324 switch (this->ike_sa
->get_state(this->ike_sa
))
327 activate_task(this, TASK_IKE_VENDOR
);
328 if (activate_task(this, TASK_IKE_INIT
))
330 this->initiating
.mid
= 0;
331 exchange
= IKE_SA_INIT
;
332 activate_task(this, TASK_IKE_NATD
);
333 activate_task(this, TASK_IKE_CERT_PRE
);
335 /* this task has to be activated before the TASK_IKE_AUTH
336 * task, because that task pregenerates the packet after
337 * which no payloads can be added to the message anymore.
339 activate_task(this, TASK_IKE_ME
);
341 activate_task(this, TASK_IKE_AUTH
);
342 activate_task(this, TASK_IKE_CERT_POST
);
343 activate_task(this, TASK_IKE_CONFIG
);
344 activate_task(this, TASK_CHILD_CREATE
);
345 activate_task(this, TASK_IKE_AUTH_LIFETIME
);
346 activate_task(this, TASK_IKE_MOBIKE
);
349 case IKE_ESTABLISHED
:
350 if (activate_task(this, TASK_CHILD_CREATE
))
352 exchange
= CREATE_CHILD_SA
;
355 if (activate_task(this, TASK_CHILD_DELETE
))
357 exchange
= INFORMATIONAL
;
360 if (activate_task(this, TASK_CHILD_REKEY
))
362 exchange
= CREATE_CHILD_SA
;
365 if (activate_task(this, TASK_IKE_DELETE
))
367 exchange
= INFORMATIONAL
;
370 if (activate_task(this, TASK_IKE_REKEY
))
372 exchange
= CREATE_CHILD_SA
;
375 if (activate_task(this, TASK_IKE_REAUTH
))
377 exchange
= INFORMATIONAL
;
380 if (activate_task(this, TASK_IKE_MOBIKE
))
382 exchange
= INFORMATIONAL
;
385 if (activate_task(this, TASK_IKE_DPD
))
387 exchange
= INFORMATIONAL
;
390 if (activate_task(this, TASK_IKE_AUTH_LIFETIME
))
392 exchange
= INFORMATIONAL
;
396 if (activate_task(this, TASK_IKE_ME
))
398 exchange
= ME_CONNECT
;
403 if (activate_task(this, TASK_IKE_DELETE
))
405 exchange
= INFORMATIONAL
;
415 DBG2(DBG_IKE
, "reinitiating already active tasks");
416 enumerator
= array_create_enumerator(this->active_tasks
);
417 while (enumerator
->enumerate(enumerator
, &task
))
419 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
420 switch (task
->get_type(task
))
423 exchange
= IKE_SA_INIT
;
428 case TASK_CHILD_CREATE
:
429 case TASK_CHILD_REKEY
:
431 exchange
= CREATE_CHILD_SA
;
433 case TASK_IKE_MOBIKE
:
434 exchange
= INFORMATIONAL
;
441 enumerator
->destroy(enumerator
);
446 DBG2(DBG_IKE
, "nothing to initiate");
447 /* nothing to do yet... */
451 me
= this->ike_sa
->get_my_host(this->ike_sa
);
452 other
= this->ike_sa
->get_other_host(this->ike_sa
);
454 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
455 message
->set_message_id(message
, this->initiating
.mid
);
456 message
->set_source(message
, me
->clone(me
));
457 message
->set_destination(message
, other
->clone(other
));
458 message
->set_exchange_type(message
, exchange
);
459 this->initiating
.type
= exchange
;
460 this->initiating
.retransmitted
= 0;
462 enumerator
= array_create_enumerator(this->active_tasks
);
463 while (enumerator
->enumerate(enumerator
, &task
))
465 switch (task
->build(task
, message
))
468 /* task completed, remove it */
469 array_remove_at(this->active_tasks
, enumerator
);
473 /* processed, but task needs another exchange */
477 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
478 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
480 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
484 /* critical failure, destroy IKE_SA */
485 enumerator
->destroy(enumerator
);
486 message
->destroy(message
);
491 enumerator
->destroy(enumerator
);
493 /* update exchange type if a task changed it */
494 this->initiating
.type
= message
->get_exchange_type(message
);
496 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
497 &this->initiating
.packet
);
498 if (status
!= SUCCESS
)
500 /* message generation failed. There is nothing more to do than to
502 message
->destroy(message
);
504 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
507 message
->destroy(message
);
509 array_compress(this->active_tasks
);
510 array_compress(this->queued_tasks
);
512 return retransmit(this, this->initiating
.mid
);
516 * handle an incoming response message
518 static status_t
process_response(private_task_manager_t
*this,
521 enumerator_t
*enumerator
;
524 if (message
->get_exchange_type(message
) != this->initiating
.type
)
526 DBG1(DBG_IKE
, "received %N response, but expected %N",
527 exchange_type_names
, message
->get_exchange_type(message
),
528 exchange_type_names
, this->initiating
.type
);
529 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
533 /* catch if we get resetted while processing */
535 enumerator
= array_create_enumerator(this->active_tasks
);
536 while (enumerator
->enumerate(enumerator
, &task
))
538 switch (task
->process(task
, message
))
541 /* task completed, remove it */
542 array_remove_at(this->active_tasks
, enumerator
);
546 /* processed, but task needs another exchange */
550 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
553 /* critical failure, destroy IKE_SA */
554 array_remove_at(this->active_tasks
, enumerator
);
555 enumerator
->destroy(enumerator
);
560 { /* start all over again if we were reset */
562 enumerator
->destroy(enumerator
);
563 return initiate(this);
566 enumerator
->destroy(enumerator
);
568 this->initiating
.mid
++;
569 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
570 this->initiating
.packet
->destroy(this->initiating
.packet
);
571 this->initiating
.packet
= NULL
;
573 array_compress(this->active_tasks
);
575 return initiate(this);
579 * handle exchange collisions
581 static bool handle_collisions(private_task_manager_t
*this, task_t
*task
)
583 enumerator_t
*enumerator
;
587 type
= task
->get_type(task
);
589 /* do we have to check */
590 if (type
== TASK_IKE_REKEY
|| type
== TASK_CHILD_REKEY
||
591 type
== TASK_CHILD_DELETE
|| type
== TASK_IKE_DELETE
||
592 type
== TASK_IKE_REAUTH
)
594 /* find an exchange collision, and notify these tasks */
595 enumerator
= array_create_enumerator(this->active_tasks
);
596 while (enumerator
->enumerate(enumerator
, &active
))
598 switch (active
->get_type(active
))
601 if (type
== TASK_IKE_REKEY
|| type
== TASK_IKE_DELETE
||
602 type
== TASK_IKE_REAUTH
)
604 ike_rekey_t
*rekey
= (ike_rekey_t
*)active
;
605 rekey
->collide(rekey
, task
);
609 case TASK_CHILD_REKEY
:
610 if (type
== TASK_CHILD_REKEY
|| type
== TASK_CHILD_DELETE
)
612 child_rekey_t
*rekey
= (child_rekey_t
*)active
;
613 rekey
->collide(rekey
, task
);
620 enumerator
->destroy(enumerator
);
623 enumerator
->destroy(enumerator
);
629 * build a response depending on the "passive" task list
631 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
633 enumerator_t
*enumerator
;
637 bool delete = FALSE
, hook
= FALSE
;
638 ike_sa_id_t
*id
= NULL
;
639 u_int64_t responder_spi
;
642 me
= request
->get_destination(request
);
643 other
= request
->get_source(request
);
645 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
646 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
647 /* send response along the path the request came in */
648 message
->set_source(message
, me
->clone(me
));
649 message
->set_destination(message
, other
->clone(other
));
650 message
->set_message_id(message
, this->responding
.mid
);
651 message
->set_request(message
, FALSE
);
653 enumerator
= array_create_enumerator(this->passive_tasks
);
654 while (enumerator
->enumerate(enumerator
, (void*)&task
))
656 switch (task
->build(task
, message
))
659 /* task completed, remove it */
660 array_remove_at(this->passive_tasks
, enumerator
);
661 if (!handle_collisions(this, task
))
667 /* processed, but task needs another exchange */
668 if (handle_collisions(this, task
))
670 array_remove_at(this->passive_tasks
, enumerator
);
678 /* destroy IKE_SA, but SEND response first */
687 enumerator
->destroy(enumerator
);
689 /* RFC 5996, section 2.6 mentions that in the event of a failure during
690 * IKE_SA_INIT the responder's SPI will be 0 in the response, while it
691 * actually explicitly allows it to be non-zero. Since we use the responder
692 * SPI to create hashes in the IKE_SA manager we can only set the SPI to
693 * zero temporarily, otherwise checking the SA in would fail. */
694 if (delete && request
->get_exchange_type(request
) == IKE_SA_INIT
)
696 id
= this->ike_sa
->get_id(this->ike_sa
);
697 responder_spi
= id
->get_responder_spi(id
);
698 id
->set_responder_spi(id
, 0);
701 /* message complete, send it */
702 DESTROY_IF(this->responding
.packet
);
703 this->responding
.packet
= NULL
;
704 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
705 &this->responding
.packet
);
706 message
->destroy(message
);
709 id
->set_responder_spi(id
, responder_spi
);
711 if (status
!= SUCCESS
)
713 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
717 charon
->sender
->send(charon
->sender
,
718 this->responding
.packet
->clone(this->responding
.packet
));
723 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
728 array_compress(this->passive_tasks
);
734 * handle an incoming request message
736 static status_t
process_request(private_task_manager_t
*this,
739 enumerator_t
*enumerator
;
742 notify_payload_t
*notify
;
743 delete_payload_t
*delete;
745 if (array_count(this->passive_tasks
) == 0)
746 { /* create tasks depending on request type, if not already some queued */
747 switch (message
->get_exchange_type(message
))
751 task
= (task_t
*)ike_vendor_create(this->ike_sa
, FALSE
);
752 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
753 task
= (task_t
*)ike_init_create(this->ike_sa
, FALSE
, NULL
);
754 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
755 task
= (task_t
*)ike_natd_create(this->ike_sa
, FALSE
);
756 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
757 task
= (task_t
*)ike_cert_pre_create(this->ike_sa
, FALSE
);
758 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
760 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
761 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
763 task
= (task_t
*)ike_auth_create(this->ike_sa
, FALSE
);
764 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
765 task
= (task_t
*)ike_cert_post_create(this->ike_sa
, FALSE
);
766 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
767 task
= (task_t
*)ike_config_create(this->ike_sa
, FALSE
);
768 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
769 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
, FALSE
,
771 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
772 task
= (task_t
*)ike_auth_lifetime_create(this->ike_sa
, FALSE
);
773 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
774 task
= (task_t
*)ike_mobike_create(this->ike_sa
, FALSE
);
775 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
778 case CREATE_CHILD_SA
:
779 { /* FIXME: we should prevent this on mediation connections */
780 bool notify_found
= FALSE
, ts_found
= FALSE
;
782 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
783 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
)
785 DBG1(DBG_IKE
, "received CREATE_CHILD_SA request for "
786 "unestablished IKE_SA, rejected");
790 enumerator
= message
->create_payload_enumerator(message
);
791 while (enumerator
->enumerate(enumerator
, &payload
))
793 switch (payload
->get_type(payload
))
796 { /* if we find a rekey notify, its CHILD_SA rekeying */
797 notify
= (notify_payload_t
*)payload
;
798 if (notify
->get_notify_type(notify
) == REKEY_SA
&&
799 (notify
->get_protocol_id(notify
) == PROTO_AH
||
800 notify
->get_protocol_id(notify
) == PROTO_ESP
))
806 case TRAFFIC_SELECTOR_INITIATOR
:
807 case TRAFFIC_SELECTOR_RESPONDER
:
808 { /* if we don't find a TS, its IKE rekeying */
816 enumerator
->destroy(enumerator
);
822 task
= (task_t
*)child_rekey_create(this->ike_sa
,
827 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
,
833 task
= (task_t
*)ike_rekey_create(this->ike_sa
, FALSE
);
835 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
840 enumerator
= message
->create_payload_enumerator(message
);
841 while (enumerator
->enumerate(enumerator
, &payload
))
843 switch (payload
->get_type(payload
))
847 notify
= (notify_payload_t
*)payload
;
848 switch (notify
->get_notify_type(notify
))
850 case ADDITIONAL_IP4_ADDRESS
:
851 case ADDITIONAL_IP6_ADDRESS
:
852 case NO_ADDITIONAL_ADDRESSES
:
853 case UPDATE_SA_ADDRESSES
:
854 case NO_NATS_ALLOWED
:
855 case UNACCEPTABLE_ADDRESSES
:
856 case UNEXPECTED_NAT_DETECTED
:
858 case NAT_DETECTION_SOURCE_IP
:
859 case NAT_DETECTION_DESTINATION_IP
:
860 task
= (task_t
*)ike_mobike_create(
861 this->ike_sa
, FALSE
);
864 task
= (task_t
*)ike_auth_lifetime_create(
865 this->ike_sa
, FALSE
);
867 case AUTHENTICATION_FAILED
:
868 /* initiator failed to authenticate us.
869 * We use ike_delete to handle this, which
870 * invokes all the required hooks. */
871 task
= (task_t
*)ike_delete_create(
872 this->ike_sa
, FALSE
);
880 delete = (delete_payload_t
*)payload
;
881 if (delete->get_protocol_id(delete) == PROTO_IKE
)
883 task
= (task_t
*)ike_delete_create(this->ike_sa
,
888 task
= (task_t
*)child_delete_create(this->ike_sa
,
889 PROTO_NONE
, 0, FALSE
);
901 enumerator
->destroy(enumerator
);
905 task
= (task_t
*)ike_dpd_create(FALSE
);
907 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
913 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
914 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
922 /* let the tasks process the message */
923 enumerator
= array_create_enumerator(this->passive_tasks
);
924 while (enumerator
->enumerate(enumerator
, (void*)&task
))
926 switch (task
->process(task
, message
))
929 /* task completed, remove it */
930 array_remove_at(this->passive_tasks
, enumerator
);
934 /* processed, but task needs at least another call to build() */
938 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
941 /* critical failure, destroy IKE_SA */
942 array_remove_at(this->passive_tasks
, enumerator
);
943 enumerator
->destroy(enumerator
);
948 enumerator
->destroy(enumerator
);
950 return build_response(this, message
);
953 METHOD(task_manager_t
, incr_mid
, void,
954 private_task_manager_t
*this, bool initiate
)
958 this->initiating
.mid
++;
962 this->responding
.mid
++;
967 * Send a notify back to the sender
969 static void send_notify_response(private_task_manager_t
*this,
970 message_t
*request
, notify_type_t type
,
977 response
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
978 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
979 response
->set_request(response
, FALSE
);
980 response
->set_message_id(response
, request
->get_message_id(request
));
981 response
->add_notify(response
, FALSE
, type
, data
);
982 me
= this->ike_sa
->get_my_host(this->ike_sa
);
983 if (me
->is_anyaddr(me
))
985 me
= request
->get_destination(request
);
986 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
988 other
= this->ike_sa
->get_other_host(this->ike_sa
);
989 if (other
->is_anyaddr(other
))
991 other
= request
->get_source(request
);
992 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
994 response
->set_source(response
, me
->clone(me
));
995 response
->set_destination(response
, other
->clone(other
));
996 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
999 charon
->sender
->send(charon
->sender
, packet
);
1001 response
->destroy(response
);
1005 * Parse the given message and verify that it is valid.
1007 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
1012 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
1014 if (status
== SUCCESS
)
1015 { /* check for unsupported critical payloads */
1016 enumerator_t
*enumerator
;
1017 unknown_payload_t
*unknown
;
1020 enumerator
= msg
->create_payload_enumerator(msg
);
1021 while (enumerator
->enumerate(enumerator
, &payload
))
1023 unknown
= (unknown_payload_t
*)payload
;
1024 type
= payload
->get_type(payload
);
1025 if (!payload_is_known(type
) &&
1026 unknown
->is_critical(unknown
))
1028 DBG1(DBG_ENC
, "payload type %N is not supported, "
1029 "but its critical!", payload_type_names
, type
);
1030 status
= NOT_SUPPORTED
;
1034 enumerator
->destroy(enumerator
);
1037 if (status
!= SUCCESS
)
1039 bool is_request
= msg
->get_request(msg
);
1044 DBG1(DBG_IKE
, "critical unknown payloads found");
1047 send_notify_response(this, msg
,
1048 UNSUPPORTED_CRITICAL_PAYLOAD
,
1049 chunk_from_thing(type
));
1050 incr_mid(this, FALSE
);
1054 DBG1(DBG_IKE
, "message parsing failed");
1057 send_notify_response(this, msg
,
1058 INVALID_SYNTAX
, chunk_empty
);
1059 incr_mid(this, FALSE
);
1063 DBG1(DBG_IKE
, "message verification failed");
1066 send_notify_response(this, msg
,
1067 INVALID_SYNTAX
, chunk_empty
);
1068 incr_mid(this, FALSE
);
1072 DBG1(DBG_IKE
, "integrity check failed");
1076 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1080 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1081 exchange_type_names
, msg
->get_exchange_type(msg
),
1082 is_request ?
"request" : "response",
1083 msg
->get_message_id(msg
));
1085 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
, status
);
1087 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1088 { /* invalid initiation attempt, close SA */
1096 METHOD(task_manager_t
, process_message
, status_t
,
1097 private_task_manager_t
*this, message_t
*msg
)
1102 bool schedule_delete_job
= FALSE
;
1104 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1105 status
= parse_message(this, msg
);
1106 if (status
!= SUCCESS
)
1111 me
= msg
->get_destination(msg
);
1112 other
= msg
->get_source(msg
);
1114 /* if this IKE_SA is virgin, we check for a config */
1115 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1119 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1121 if (ike_cfg
== NULL
)
1123 /* no config found for these hosts, destroy */
1124 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1125 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1126 send_notify_response(this, msg
,
1127 NO_PROPOSAL_CHOSEN
, chunk_empty
);
1130 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1131 ike_cfg
->destroy(ike_cfg
);
1132 /* add a timeout if peer does not establish it completely */
1133 schedule_delete_job
= TRUE
;
1135 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1136 time_monotonic(NULL
));
1138 mid
= msg
->get_message_id(msg
);
1139 if (msg
->get_request(msg
))
1141 if (mid
== this->responding
.mid
)
1143 /* reject initial messages once established */
1144 if (msg
->get_exchange_type(msg
) == IKE_SA_INIT
||
1145 msg
->get_exchange_type(msg
) == IKE_AUTH
)
1147 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CREATED
&&
1148 this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
1150 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1151 exchange_type_names
, msg
->get_exchange_type(msg
));
1155 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1156 { /* with MOBIKE, we do no implicit updates */
1157 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, mid
== 1);
1159 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1160 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1161 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1164 if (process_request(this, msg
) != SUCCESS
)
1169 this->responding
.mid
++;
1171 else if ((mid
== this->responding
.mid
- 1) && this->responding
.packet
)
1176 DBG1(DBG_IKE
, "received retransmit of request with ID %d, "
1177 "retransmitting response", mid
);
1178 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1179 clone
= this->responding
.packet
->clone(this->responding
.packet
);
1180 host
= msg
->get_destination(msg
);
1181 clone
->set_source(clone
, host
->clone(host
));
1182 host
= msg
->get_source(msg
);
1183 clone
->set_destination(clone
, host
->clone(host
));
1184 charon
->sender
->send(charon
->sender
, clone
);
1188 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1189 mid
, this->responding
.mid
);
1190 if (msg
->get_exchange_type(msg
) == IKE_SA_INIT
)
1191 { /* clean up IKE_SA state if IKE_SA_INIT has invalid msg ID */
1198 if (mid
== this->initiating
.mid
)
1200 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1201 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1202 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1203 { /* only do updates based on verified messages (or initial ones) */
1204 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1205 { /* with MOBIKE, we do no implicit updates. we force an
1206 * update of the local address on IKE_SA_INIT, but never
1207 * for the remote address */
1208 this->ike_sa
->update_hosts(this->ike_sa
, me
, NULL
, mid
== 0);
1209 this->ike_sa
->update_hosts(this->ike_sa
, NULL
, other
, FALSE
);
1212 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1213 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1214 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1217 if (process_response(this, msg
) != SUCCESS
)
1225 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1226 mid
, this->initiating
.mid
);
1231 if (schedule_delete_job
)
1233 ike_sa_id_t
*ike_sa_id
;
1236 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1237 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1238 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1239 lib
->settings
->get_int(lib
->settings
,
1240 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1246 METHOD(task_manager_t
, queue_task
, void,
1247 private_task_manager_t
*this, task_t
*task
)
1249 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
1250 { /* there is no need to queue more than one mobike task */
1251 enumerator_t
*enumerator
;
1254 enumerator
= array_create_enumerator(this->queued_tasks
);
1255 while (enumerator
->enumerate(enumerator
, ¤t
))
1257 if (current
->get_type(current
) == TASK_IKE_MOBIKE
)
1259 enumerator
->destroy(enumerator
);
1260 task
->destroy(task
);
1264 enumerator
->destroy(enumerator
);
1266 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1267 array_insert(this->queued_tasks
, ARRAY_TAIL
, task
);
1271 * Check if a given task has been queued already
1273 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1275 enumerator_t
*enumerator
;
1279 enumerator
= array_create_enumerator(this->queued_tasks
);
1280 while (enumerator
->enumerate(enumerator
, &task
))
1282 if (task
->get_type(task
) == type
)
1288 enumerator
->destroy(enumerator
);
1292 METHOD(task_manager_t
, queue_ike
, void,
1293 private_task_manager_t
*this)
1295 if (!has_queued(this, TASK_IKE_VENDOR
))
1297 queue_task(this, (task_t
*)ike_vendor_create(this->ike_sa
, TRUE
));
1299 if (!has_queued(this, TASK_IKE_INIT
))
1301 queue_task(this, (task_t
*)ike_init_create(this->ike_sa
, TRUE
, NULL
));
1303 if (!has_queued(this, TASK_IKE_NATD
))
1305 queue_task(this, (task_t
*)ike_natd_create(this->ike_sa
, TRUE
));
1307 if (!has_queued(this, TASK_IKE_CERT_PRE
))
1309 queue_task(this, (task_t
*)ike_cert_pre_create(this->ike_sa
, TRUE
));
1311 if (!has_queued(this, TASK_IKE_AUTH
))
1313 queue_task(this, (task_t
*)ike_auth_create(this->ike_sa
, TRUE
));
1315 if (!has_queued(this, TASK_IKE_CERT_POST
))
1317 queue_task(this, (task_t
*)ike_cert_post_create(this->ike_sa
, TRUE
));
1319 if (!has_queued(this, TASK_IKE_CONFIG
))
1321 queue_task(this, (task_t
*)ike_config_create(this->ike_sa
, TRUE
));
1323 if (!has_queued(this, TASK_IKE_AUTH_LIFETIME
))
1325 queue_task(this, (task_t
*)ike_auth_lifetime_create(this->ike_sa
, TRUE
));
1327 if (!has_queued(this, TASK_IKE_MOBIKE
))
1329 peer_cfg_t
*peer_cfg
;
1331 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1332 if (peer_cfg
->use_mobike(peer_cfg
))
1334 queue_task(this, (task_t
*)ike_mobike_create(this->ike_sa
, TRUE
));
1338 if (!has_queued(this, TASK_IKE_ME
))
1340 queue_task(this, (task_t
*)ike_me_create(this->ike_sa
, TRUE
));
1345 METHOD(task_manager_t
, queue_ike_rekey
, void,
1346 private_task_manager_t
*this)
1348 queue_task(this, (task_t
*)ike_rekey_create(this->ike_sa
, TRUE
));
1351 METHOD(task_manager_t
, queue_ike_reauth
, void,
1352 private_task_manager_t
*this)
1354 queue_task(this, (task_t
*)ike_reauth_create(this->ike_sa
));
1357 METHOD(task_manager_t
, queue_ike_delete
, void,
1358 private_task_manager_t
*this)
1360 queue_task(this, (task_t
*)ike_delete_create(this->ike_sa
, TRUE
));
1363 METHOD(task_manager_t
, queue_mobike
, void,
1364 private_task_manager_t
*this, bool roam
, bool address
)
1366 ike_mobike_t
*mobike
;
1368 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1371 mobike
->roam(mobike
, address
);
1375 mobike
->addresses(mobike
);
1377 queue_task(this, &mobike
->task
);
1380 METHOD(task_manager_t
, queue_child
, void,
1381 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1382 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1384 child_create_t
*task
;
1386 task
= child_create_create(this->ike_sa
, cfg
, FALSE
, tsi
, tsr
);
1389 task
->use_reqid(task
, reqid
);
1391 queue_task(this, &task
->task
);
1394 METHOD(task_manager_t
, queue_child_rekey
, void,
1395 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1397 queue_task(this, (task_t
*)child_rekey_create(this->ike_sa
, protocol
, spi
));
1400 METHOD(task_manager_t
, queue_child_delete
, void,
1401 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
,
1404 queue_task(this, (task_t
*)child_delete_create(this->ike_sa
,
1405 protocol
, spi
, expired
));
1408 METHOD(task_manager_t
, queue_dpd
, void,
1409 private_task_manager_t
*this)
1411 ike_mobike_t
*mobike
;
1413 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
) &&
1414 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_HERE
))
1416 /* use mobike enabled DPD to detect NAT mapping changes */
1417 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1418 mobike
->dpd(mobike
);
1419 queue_task(this, &mobike
->task
);
1423 queue_task(this, (task_t
*)ike_dpd_create(TRUE
));
1427 METHOD(task_manager_t
, adopt_tasks
, void,
1428 private_task_manager_t
*this, task_manager_t
*other_public
)
1430 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1433 /* move queued tasks from other to this */
1434 while (array_remove(other
->queued_tasks
, ARRAY_TAIL
, &task
))
1436 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1437 task
->migrate(task
, this->ike_sa
);
1438 array_insert(this->queued_tasks
, ARRAY_HEAD
, task
);
1443 * Migrates child-creating tasks from src to dst
1445 static void migrate_child_tasks(private_task_manager_t
*this,
1446 array_t
*src
, array_t
*dst
)
1448 enumerator_t
*enumerator
;
1451 enumerator
= array_create_enumerator(src
);
1452 while (enumerator
->enumerate(enumerator
, &task
))
1454 if (task
->get_type(task
) == TASK_CHILD_CREATE
)
1456 array_remove_at(src
, enumerator
);
1457 task
->migrate(task
, this->ike_sa
);
1458 array_insert(dst
, ARRAY_TAIL
, task
);
1461 enumerator
->destroy(enumerator
);
1464 METHOD(task_manager_t
, adopt_child_tasks
, void,
1465 private_task_manager_t
*this, task_manager_t
*other_public
)
1467 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1469 /* move active child tasks from other to this */
1470 migrate_child_tasks(this, other
->active_tasks
, this->queued_tasks
);
1471 /* do the same for queued tasks */
1472 migrate_child_tasks(this, other
->queued_tasks
, this->queued_tasks
);
1475 METHOD(task_manager_t
, busy
, bool,
1476 private_task_manager_t
*this)
1478 return array_count(this->active_tasks
) > 0;
1481 METHOD(task_manager_t
, reset
, void,
1482 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1484 enumerator_t
*enumerator
;
1487 /* reset message counters and retransmit packets */
1488 DESTROY_IF(this->responding
.packet
);
1489 DESTROY_IF(this->initiating
.packet
);
1490 this->responding
.packet
= NULL
;
1491 this->initiating
.packet
= NULL
;
1492 if (initiate
!= UINT_MAX
)
1494 this->initiating
.mid
= initiate
;
1496 if (respond
!= UINT_MAX
)
1498 this->responding
.mid
= respond
;
1500 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1502 /* reset queued tasks */
1503 enumerator
= array_create_enumerator(this->queued_tasks
);
1504 while (enumerator
->enumerate(enumerator
, &task
))
1506 task
->migrate(task
, this->ike_sa
);
1508 enumerator
->destroy(enumerator
);
1510 /* reset active tasks */
1511 while (array_remove(this->active_tasks
, ARRAY_TAIL
, &task
))
1513 task
->migrate(task
, this->ike_sa
);
1514 array_insert(this->queued_tasks
, ARRAY_HEAD
, task
);
1520 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1521 private_task_manager_t
*this, task_queue_t queue
)
1525 case TASK_QUEUE_ACTIVE
:
1526 return array_create_enumerator(this->active_tasks
);
1527 case TASK_QUEUE_PASSIVE
:
1528 return array_create_enumerator(this->passive_tasks
);
1529 case TASK_QUEUE_QUEUED
:
1530 return array_create_enumerator(this->queued_tasks
);
1532 return enumerator_create_empty();
1536 METHOD(task_manager_t
, destroy
, void,
1537 private_task_manager_t
*this)
1541 array_destroy(this->active_tasks
);
1542 array_destroy(this->queued_tasks
);
1543 array_destroy(this->passive_tasks
);
1545 DESTROY_IF(this->responding
.packet
);
1546 DESTROY_IF(this->initiating
.packet
);
1553 task_manager_v2_t
*task_manager_v2_create(ike_sa_t
*ike_sa
)
1555 private_task_manager_t
*this;
1560 .process_message
= _process_message
,
1561 .queue_task
= _queue_task
,
1562 .queue_ike
= _queue_ike
,
1563 .queue_ike_rekey
= _queue_ike_rekey
,
1564 .queue_ike_reauth
= _queue_ike_reauth
,
1565 .queue_ike_delete
= _queue_ike_delete
,
1566 .queue_mobike
= _queue_mobike
,
1567 .queue_child
= _queue_child
,
1568 .queue_child_rekey
= _queue_child_rekey
,
1569 .queue_child_delete
= _queue_child_delete
,
1570 .queue_dpd
= _queue_dpd
,
1571 .initiate
= _initiate
,
1572 .retransmit
= _retransmit
,
1573 .incr_mid
= _incr_mid
,
1575 .adopt_tasks
= _adopt_tasks
,
1576 .adopt_child_tasks
= _adopt_child_tasks
,
1578 .create_task_enumerator
= _create_task_enumerator
,
1580 .flush_queue
= _flush_queue
,
1581 .destroy
= _destroy
,
1585 .initiating
.type
= EXCHANGE_TYPE_UNDEFINED
,
1586 .queued_tasks
= array_create(0, 0),
1587 .active_tasks
= array_create(0, 0),
1588 .passive_tasks
= array_create(0, 0),
1589 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
1590 "%s.retransmit_tries", RETRANSMIT_TRIES
, lib
->ns
),
1591 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
1592 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, lib
->ns
),
1593 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
1594 "%s.retransmit_base", RETRANSMIT_BASE
, lib
->ns
),
1597 return &this->public;