2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2011 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_v1.h"
22 #include <sa/ikev1/tasks/main_mode.h>
23 #include <sa/ikev1/tasks/aggressive_mode.h>
24 #include <sa/ikev1/tasks/quick_mode.h>
25 #include <sa/ikev1/tasks/quick_delete.h>
26 #include <sa/ikev1/tasks/xauth.h>
27 #include <sa/ikev1/tasks/mode_config.h>
28 #include <sa/ikev1/tasks/informational.h>
29 #include <sa/ikev1/tasks/isakmp_natd.h>
30 #include <sa/ikev1/tasks/isakmp_vendor.h>
31 #include <sa/ikev1/tasks/isakmp_cert_pre.h>
32 #include <sa/ikev1/tasks/isakmp_cert_post.h>
33 #include <sa/ikev1/tasks/isakmp_delete.h>
34 #include <sa/ikev1/tasks/isakmp_dpd.h>
36 #include <processing/jobs/retransmit_job.h>
37 #include <processing/jobs/delete_ike_sa_job.h>
38 #include <processing/jobs/dpd_timeout_job.h>
41 * Number of old messages hashes we keep for retransmission.
43 * In Main Mode, we must ignore messages from a previous message pair if
44 * we already continued to the next. Otherwise a late retransmission
45 * could be considered as a reply to the newer request.
47 #define MAX_OLD_HASHES 2
50 * First sequence number of responding packets.
52 * To distinguish retransmission jobs for initiating and responding packets,
53 * we split up the sequence counter and use the upper half for responding.
55 #define RESPONDING_SEQ INT_MAX
57 typedef struct exchange_t exchange_t
;
60 * An exchange in the air, used do detect and handle retransmission
65 * Message ID used for this transaction
70 * generated packet for retransmission
75 typedef struct private_task_manager_t private_task_manager_t
;
78 * private data of the task manager
80 struct private_task_manager_t
{
85 task_manager_v1_t
public;
88 * associated IKE_SA we are serving
93 * RNG to create message IDs
98 * Exchange we are currently handling as responder
102 * Message ID of the last response
107 * Hash of a previously received message
112 * packet for retransmission
117 * Sequence number of the last sent message
122 * how many times we have retransmitted so far
129 * Exchange we are currently handling as initiator
133 * Message ID of the exchange
138 * Hashes of old responses we can ignore
140 u_int32_t old_hashes
[MAX_OLD_HASHES
];
143 * Position in old hash array
148 * Sequence number of the last sent message
153 * how many times we have retransmitted so far
158 * packet for retransmission
163 * type of the initated exchange
165 exchange_type_t type
;
170 * List of queued tasks not yet in action
172 linked_list_t
*queued_tasks
;
175 * List of active tasks, initiated by ourselve
177 linked_list_t
*active_tasks
;
180 * List of tasks initiated by peer
182 linked_list_t
*passive_tasks
;
185 * Queued messages not yet ready to process
190 * Number of times we retransmit messages before giving up
192 u_int retransmit_tries
;
195 * Retransmission timeout
197 double retransmit_timeout
;
200 * Base to calculate retransmission timeout
202 double retransmit_base
;
205 * Sequence number for sending DPD requests
210 * Sequence number for received DPD requests
216 * Flush a single task queue
218 static void flush_queue(private_task_manager_t
*this, linked_list_t
*list
)
224 this->queued
->destroy(this->queued
);
227 while (list
->remove_last(list
, (void**)&task
) == SUCCESS
)
234 * flush all tasks in the task manager
236 static void flush(private_task_manager_t
*this)
238 flush_queue(this, this->queued_tasks
);
239 flush_queue(this, this->passive_tasks
);
240 flush_queue(this, this->active_tasks
);
244 * move a task of a specific type from the queue to the active list
246 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
248 enumerator_t
*enumerator
;
252 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
253 while (enumerator
->enumerate(enumerator
, (void**)&task
))
255 if (task
->get_type(task
) == type
)
257 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
258 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
259 this->active_tasks
->insert_last(this->active_tasks
, task
);
264 enumerator
->destroy(enumerator
);
269 * Retransmit a packet, either as initiator or as responder
271 static status_t
retransmit_packet(private_task_manager_t
*this, u_int32_t seqnr
,
272 u_int mid
, u_int retransmitted
, packet_t
*packet
)
276 if (retransmitted
> this->retransmit_tries
)
278 DBG1(DBG_IKE
, "giving up after %u retransmits", retransmitted
- 1);
279 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
281 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
285 t
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
286 pow(this->retransmit_base
, retransmitted
));
289 DBG1(DBG_IKE
, "sending retransmit %u of %s message ID %u, seq %u",
290 retransmitted
, seqnr
< RESPONDING_SEQ ?
"request" : "response",
291 mid
, seqnr
< RESPONDING_SEQ ? seqnr
: seqnr
- RESPONDING_SEQ
);
293 charon
->sender
->send(charon
->sender
, packet
->clone(packet
));
294 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
295 retransmit_job_create(seqnr
, this->ike_sa
->get_id(this->ike_sa
)), t
);
299 METHOD(task_manager_t
, retransmit
, status_t
,
300 private_task_manager_t
*this, u_int32_t seqnr
)
302 status_t status
= SUCCESS
;
304 if (seqnr
== this->initiating
.seqnr
&& this->initiating
.packet
)
306 status
= retransmit_packet(this, seqnr
, this->initiating
.mid
,
307 this->initiating
.retransmitted
, this->initiating
.packet
);
308 if (status
== NEED_MORE
)
310 this->initiating
.retransmitted
++;
314 if (seqnr
== this->responding
.seqnr
&& this->responding
.packet
)
316 status
= retransmit_packet(this, seqnr
, this->responding
.mid
,
317 this->responding
.retransmitted
, this->responding
.packet
);
318 if (status
== NEED_MORE
)
320 this->responding
.retransmitted
++;
327 METHOD(task_manager_t
, initiate
, status_t
,
328 private_task_manager_t
*this)
330 enumerator_t
*enumerator
;
335 exchange_type_t exchange
= EXCHANGE_TYPE_UNDEFINED
;
336 bool new_mid
= FALSE
, expect_response
= FALSE
, flushed
= FALSE
, keep
= FALSE
;
338 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
&&
339 this->initiating
.type
!= INFORMATIONAL_V1
)
341 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
342 exchange_type_names
, this->initiating
.type
);
343 /* do not initiate if we already have a message in the air */
347 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
349 DBG2(DBG_IKE
, "activating new tasks");
350 switch (this->ike_sa
->get_state(this->ike_sa
))
353 activate_task(this, TASK_ISAKMP_VENDOR
);
354 activate_task(this, TASK_ISAKMP_CERT_PRE
);
355 if (activate_task(this, TASK_MAIN_MODE
))
359 else if (activate_task(this, TASK_AGGRESSIVE_MODE
))
361 exchange
= AGGRESSIVE
;
363 activate_task(this, TASK_ISAKMP_CERT_POST
);
364 activate_task(this, TASK_ISAKMP_NATD
);
367 if (activate_task(this, TASK_ISAKMP_DELETE
))
369 exchange
= INFORMATIONAL_V1
;
373 if (activate_task(this, TASK_XAUTH
))
375 exchange
= TRANSACTION
;
379 if (activate_task(this, TASK_INFORMATIONAL
))
381 exchange
= INFORMATIONAL_V1
;
386 case IKE_ESTABLISHED
:
387 if (activate_task(this, TASK_MODE_CONFIG
))
389 exchange
= TRANSACTION
;
393 if (activate_task(this, TASK_QUICK_MODE
))
395 exchange
= QUICK_MODE
;
399 if (activate_task(this, TASK_INFORMATIONAL
))
401 exchange
= INFORMATIONAL_V1
;
405 if (activate_task(this, TASK_QUICK_DELETE
))
407 exchange
= INFORMATIONAL_V1
;
411 if (activate_task(this, TASK_ISAKMP_DELETE
))
413 exchange
= INFORMATIONAL_V1
;
417 if (activate_task(this, TASK_ISAKMP_DPD
))
419 exchange
= INFORMATIONAL_V1
;
430 DBG2(DBG_IKE
, "reinitiating already active tasks");
431 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
432 while (enumerator
->enumerate(enumerator
, (void**)&task
))
434 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
435 switch (task
->get_type(task
))
440 case TASK_AGGRESSIVE_MODE
:
441 exchange
= AGGRESSIVE
;
443 case TASK_QUICK_MODE
:
444 exchange
= QUICK_MODE
;
447 exchange
= TRANSACTION
;
455 enumerator
->destroy(enumerator
);
458 if (exchange
== EXCHANGE_TYPE_UNDEFINED
)
460 DBG2(DBG_IKE
, "nothing to initiate");
461 /* nothing to do yet... */
465 me
= this->ike_sa
->get_my_host(this->ike_sa
);
466 other
= this->ike_sa
->get_other_host(this->ike_sa
);
468 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
471 this->rng
->get_bytes(this->rng
, sizeof(this->initiating
.mid
),
472 (void*)&this->initiating
.mid
);
474 message
->set_message_id(message
, this->initiating
.mid
);
475 message
->set_source(message
, me
->clone(me
));
476 message
->set_destination(message
, other
->clone(other
));
477 message
->set_exchange_type(message
, exchange
);
478 this->initiating
.type
= exchange
;
479 this->initiating
.retransmitted
= 0;
481 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
482 while (enumerator
->enumerate(enumerator
, (void*)&task
))
484 switch (task
->build(task
, message
))
487 /* task completed, remove it */
488 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
489 if (task
->get_type(task
) == TASK_AGGRESSIVE_MODE
||
490 task
->get_type(task
) == TASK_QUICK_MODE
)
491 { /* last message of three message exchange */
497 expect_response
= TRUE
;
498 /* processed, but task needs another exchange */
501 flush_queue(this, this->active_tasks
);
506 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
508 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
512 /* critical failure, destroy IKE_SA */
513 enumerator
->destroy(enumerator
);
514 message
->destroy(message
);
520 enumerator
->destroy(enumerator
);
522 if (this->active_tasks
->get_count(this->active_tasks
) == 0 &&
523 (exchange
== QUICK_MODE
|| exchange
== AGGRESSIVE
))
524 { /* tasks completed, no exchange active anymore */
525 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
529 message
->destroy(message
);
530 return initiate(this);
533 DESTROY_IF(this->initiating
.packet
);
534 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
535 &this->initiating
.packet
);
536 if (status
!= SUCCESS
)
538 /* message generation failed. There is nothing more to do than to
540 message
->destroy(message
);
542 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
546 this->initiating
.seqnr
++;
547 if (expect_response
)
549 message
->destroy(message
);
550 return retransmit(this, this->initiating
.seqnr
);
553 { /* keep the packet for retransmission, the responder might request it */
554 charon
->sender
->send(charon
->sender
,
555 this->initiating
.packet
->clone(this->initiating
.packet
));
559 charon
->sender
->send(charon
->sender
, this->initiating
.packet
);
560 this->initiating
.packet
= NULL
;
562 message
->destroy(message
);
564 if (exchange
== INFORMATIONAL_V1
)
566 switch (this->ike_sa
->get_state(this->ike_sa
))
569 /* close after sending an INFORMATIONAL when unestablished */
572 /* close after sending a DELETE */
578 return initiate(this);
582 * build a response depending on the "passive" task list
584 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
586 enumerator_t
*enumerator
;
590 bool delete = FALSE
, flushed
= FALSE
, expect_request
= FALSE
;
593 me
= request
->get_destination(request
);
594 other
= request
->get_source(request
);
596 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
597 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
598 /* send response along the path the request came in */
599 message
->set_source(message
, me
->clone(me
));
600 message
->set_destination(message
, other
->clone(other
));
601 message
->set_message_id(message
, request
->get_message_id(request
));
602 message
->set_request(message
, FALSE
);
604 this->responding
.mid
= request
->get_message_id(request
);
605 this->responding
.retransmitted
= 0;
606 this->responding
.seqnr
++;
608 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
609 while (enumerator
->enumerate(enumerator
, (void*)&task
))
611 switch (task
->build(task
, message
))
614 /* task completed, remove it */
615 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
619 /* processed, but task needs another exchange */
620 if (task
->get_type(task
) == TASK_QUICK_MODE
||
621 task
->get_type(task
) == TASK_AGGRESSIVE_MODE
)
622 { /* we rely on initiator retransmission, except for
623 * three-message exchanges */
624 expect_request
= TRUE
;
628 flush_queue(this, this->passive_tasks
);
633 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
636 /* destroy IKE_SA, but SEND response first */
642 enumerator
->destroy(enumerator
);
644 DESTROY_IF(this->responding
.packet
);
645 this->responding
.packet
= NULL
;
648 message
->destroy(message
);
649 return initiate(this);
651 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
652 &this->responding
.packet
);
653 message
->destroy(message
);
654 if (status
!= SUCCESS
)
656 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
660 if (expect_request
&& !delete)
662 return retransmit(this, this->responding
.seqnr
);
664 charon
->sender
->send(charon
->sender
,
665 this->responding
.packet
->clone(this->responding
.packet
));
674 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
675 * The notify protocol_id is set to ISAKMP
677 static void send_notify(private_task_manager_t
*this, message_t
*request
,
685 if (request
&& request
->get_exchange_type(request
) == INFORMATIONAL_V1
)
686 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
687 DBG1(DBG_IKE
, "ignore malformed INFORMATIONAL request");
691 response
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
692 response
->set_exchange_type(response
, INFORMATIONAL_V1
);
693 response
->set_request(response
, TRUE
);
694 this->rng
->get_bytes(this->rng
, sizeof(mid
), (void*)&mid
);
695 response
->set_message_id(response
, mid
);
696 response
->add_payload(response
, (payload_t
*)
697 notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
700 me
= this->ike_sa
->get_my_host(this->ike_sa
);
701 if (me
->is_anyaddr(me
))
703 me
= request
->get_destination(request
);
704 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
706 other
= this->ike_sa
->get_other_host(this->ike_sa
);
707 if (other
->is_anyaddr(other
))
709 other
= request
->get_source(request
);
710 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
712 response
->set_source(response
, me
->clone(me
));
713 response
->set_destination(response
, other
->clone(other
));
714 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
717 charon
->sender
->send(charon
->sender
, packet
);
719 response
->destroy(response
);
723 * handle an incoming request message
725 static status_t
process_request(private_task_manager_t
*this,
728 enumerator_t
*enumerator
;
730 bool send_response
= FALSE
, dpd
= FALSE
;
731 notify_payload_t
*notify
;
734 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
735 this->passive_tasks
->get_count(this->passive_tasks
) == 0)
736 { /* create tasks depending on request type, if not already some queued */
737 switch (message
->get_exchange_type(message
))
740 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
741 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
742 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
743 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
744 task
= (task_t
*)main_mode_create(this->ike_sa
, FALSE
);
745 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
746 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
747 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
748 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
749 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
752 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
753 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
754 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
755 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
756 task
= (task_t
*)aggressive_mode_create(this->ike_sa
, FALSE
);
757 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
758 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
759 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
760 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
761 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
764 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
766 DBG1(DBG_IKE
, "received quick mode request for "
767 "unestablished IKE_SA, ignored");
770 task
= (task_t
*)quick_mode_create(this->ike_sa
, NULL
,
772 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
774 case INFORMATIONAL_V1
:
775 notify
= message
->get_notify(message
, DPD_R_U_THERE
);
778 data
= notify
->get_notification_data(notify
);
779 if (this->dpd_recv
== 0 && data
.len
== 4)
780 { /* first DPD request, initialize counter */
781 this->dpd_recv
= untoh32(data
.ptr
);
783 task
= (task_t
*)isakmp_dpd_create(this->ike_sa
, FALSE
,
787 else if ((notify
= message
->get_notify(message
,
790 data
= notify
->get_notification_data(notify
);
791 if (data
.len
== 4 && untoh32(data
.ptr
) == this->dpd_send
)
795 task
= (task_t
*)isakmp_dpd_create(this->ike_sa
, TRUE
,
801 task
= (task_t
*)informational_create(this->ike_sa
, NULL
);
803 this->passive_tasks
->insert_first(this->passive_tasks
, task
);
806 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_ESTABLISHED
)
808 task
= (task_t
*)mode_config_create(this->ike_sa
, FALSE
);
812 task
= (task_t
*)xauth_create(this->ike_sa
, FALSE
);
814 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
822 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
823 time_monotonic(NULL
));
825 /* let the tasks process the message */
826 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
827 while (enumerator
->enumerate(enumerator
, (void*)&task
))
829 switch (task
->process(task
, message
))
832 /* task completed, remove it */
833 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
837 /* processed, but task needs at least another call to build() */
838 send_response
= TRUE
;
841 send_response
= FALSE
;
842 flush_queue(this, this->passive_tasks
);
846 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
849 /* critical failure, destroy IKE_SA */
850 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
851 enumerator
->destroy(enumerator
);
857 enumerator
->destroy(enumerator
);
859 if (dpd
&& this->initiating
.type
== INFORMATIONAL_V1
)
860 { /* got a DPD reply, cancel any retransmission */
861 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
862 DESTROY_IF(this->initiating
.packet
);
863 this->initiating
.packet
= NULL
;
867 if (build_response(this, message
) != SUCCESS
)
873 { /* We don't send a response, so don't retransmit one if we get
874 * the same message again. */
875 DESTROY_IF(this->responding
.packet
);
876 this->responding
.packet
= NULL
;
878 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0 &&
879 this->queued_tasks
->get_count(this->queued_tasks
) > 0)
881 /* passive tasks completed, check if an active task has been queued,
882 * such as XAUTH or modeconfig push */
883 return initiate(this);
889 * handle an incoming response message
891 static status_t
process_response(private_task_manager_t
*this,
894 enumerator_t
*enumerator
;
898 if (message
->get_exchange_type(message
) != this->initiating
.type
)
900 DBG1(DBG_IKE
, "received %N response, but expected %N",
901 exchange_type_names
, message
->get_exchange_type(message
),
902 exchange_type_names
, this->initiating
.type
);
903 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
907 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
908 while (enumerator
->enumerate(enumerator
, (void*)&task
))
910 switch (task
->process(task
, message
))
913 /* task completed, remove it */
914 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
918 /* processed, but task needs another exchange */
921 flush_queue(this, this->active_tasks
);
925 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
928 /* critical failure, destroy IKE_SA */
929 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
930 enumerator
->destroy(enumerator
);
936 enumerator
->destroy(enumerator
);
938 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
939 this->initiating
.packet
->destroy(this->initiating
.packet
);
940 this->initiating
.packet
= NULL
;
942 if (this->queued
&& this->active_tasks
->get_count(this->active_tasks
) == 0)
944 status
= this->public.task_manager
.process_message(
945 &this->public.task_manager
, this->queued
);
946 this->queued
->destroy(this->queued
);
948 if (status
== DESTROY_ME
)
954 return initiate(this);
958 * Parse the given message and verify that it is valid.
960 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
964 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
966 if (status
!= SUCCESS
)
971 DBG1(DBG_IKE
, "unsupported exchange type");
972 send_notify(this, msg
, INVALID_EXCHANGE_TYPE
);
975 DBG1(DBG_IKE
, "message parsing failed");
976 send_notify(this, msg
, PAYLOAD_MALFORMED
);
979 DBG1(DBG_IKE
, "message verification failed");
980 send_notify(this, msg
, PAYLOAD_MALFORMED
);
983 DBG1(DBG_IKE
, "integrity check failed");
984 send_notify(this, msg
, INVALID_HASH_INFORMATION
);
987 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
988 send_notify(this, msg
, PAYLOAD_MALFORMED
);
992 DBG1(DBG_IKE
, "%N %s with message ID %u processing failed",
993 exchange_type_names
, msg
->get_exchange_type(msg
),
994 msg
->get_request(msg
) ?
"request" : "response",
995 msg
->get_message_id(msg
));
997 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
998 { /* invalid initiation attempt, close SA */
1005 METHOD(task_manager_t
, process_message
, status_t
,
1006 private_task_manager_t
*this, message_t
*msg
)
1008 u_int32_t hash
, mid
, i
;
1012 /* TODO-IKEv1: update hosts more selectively */
1013 me
= msg
->get_destination(msg
);
1014 other
= msg
->get_source(msg
);
1015 mid
= msg
->get_message_id(msg
);
1016 hash
= chunk_hash(msg
->get_packet_data(msg
));
1017 for (i
= 0; i
< MAX_OLD_HASHES
; i
++)
1019 if (this->initiating
.old_hashes
[i
] == hash
)
1021 if (this->initiating
.packet
&&
1022 i
== (this->initiating
.old_hash_pos
% MAX_OLD_HASHES
) &&
1023 (msg
->get_exchange_type(msg
) == QUICK_MODE
||
1024 msg
->get_exchange_type(msg
) == AGGRESSIVE
))
1026 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1027 "resending last request", mid
);
1028 charon
->sender
->send(charon
->sender
,
1029 this->initiating
.packet
->clone(this->initiating
.packet
));
1032 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1033 "but next request already sent", mid
);
1038 if ((mid
&& mid
== this->initiating
.mid
) ||
1039 (this->initiating
.mid
== 0 &&
1040 msg
->get_exchange_type(msg
) == this->initiating
.type
&&
1041 this->active_tasks
->get_count(this->active_tasks
)))
1043 msg
->set_request(msg
, FALSE
);
1044 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1045 status
= parse_message(this, msg
);
1046 if (status
!= SUCCESS
)
1050 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1051 time_monotonic(NULL
));
1052 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1053 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1054 if (process_response(this, msg
) != SUCCESS
)
1059 this->initiating
.old_hashes
[(++this->initiating
.old_hash_pos
) %
1060 MAX_OLD_HASHES
] = hash
;
1064 if (hash
== this->responding
.hash
)
1066 if (this->responding
.packet
)
1068 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1069 "retransmitting response", mid
);
1070 charon
->sender
->send(charon
->sender
,
1071 this->responding
.packet
->clone(this->responding
.packet
));
1073 else if (this->initiating
.packet
&&
1074 this->initiating
.type
== INFORMATIONAL_V1
)
1076 DBG1(DBG_IKE
, "received retransmit of DPD request, "
1077 "retransmitting response");
1078 charon
->sender
->send(charon
->sender
,
1079 this->initiating
.packet
->clone(this->initiating
.packet
));
1083 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1084 "but no response to retransmit", mid
);
1088 if (msg
->get_exchange_type(msg
) == TRANSACTION
&&
1089 this->active_tasks
->get_count(this->active_tasks
))
1090 { /* main mode not yet complete, queue XAuth/Mode config tasks */
1093 DBG1(DBG_IKE
, "ignoring additional %N request, queue full",
1094 exchange_type_names
, TRANSACTION
);
1097 this->queued
= message_create_from_packet(msg
->get_packet(msg
));
1098 if (this->queued
->parse_header(this->queued
) != SUCCESS
)
1100 this->queued
->destroy(this->queued
);
1101 this->queued
= NULL
;
1104 DBG1(DBG_IKE
, "queueing %N request as tasks still active",
1105 exchange_type_names
, TRANSACTION
);
1109 msg
->set_request(msg
, TRUE
);
1110 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1111 status
= parse_message(this, msg
);
1112 if (status
!= SUCCESS
)
1116 /* if this IKE_SA is virgin, we check for a config */
1117 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1119 ike_sa_id_t
*ike_sa_id
;
1123 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
, me
, other
);
1124 if (ike_cfg
== NULL
)
1126 /* no config found for these hosts, destroy */
1127 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1128 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1129 send_notify(this, msg
, NO_PROPOSAL_CHOSEN
);
1132 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1133 ike_cfg
->destroy(ike_cfg
);
1134 /* add a timeout if peer does not establish it completely */
1135 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1136 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1137 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1138 lib
->settings
->get_int(lib
->settings
,
1139 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1142 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1143 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1144 if (process_request(this, msg
) != SUCCESS
)
1149 this->responding
.hash
= hash
;
1154 METHOD(task_manager_t
, queue_task
, void,
1155 private_task_manager_t
*this, task_t
*task
)
1157 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1158 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1162 * Check if a given task has been queued already
1164 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1166 enumerator_t
*enumerator
;
1170 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1171 while (enumerator
->enumerate(enumerator
, &task
))
1173 if (task
->get_type(task
) == type
)
1179 enumerator
->destroy(enumerator
);
1183 METHOD(task_manager_t
, queue_ike
, void,
1184 private_task_manager_t
*this)
1186 peer_cfg_t
*peer_cfg
;
1188 if (!has_queued(this, TASK_ISAKMP_VENDOR
))
1190 queue_task(this, (task_t
*)isakmp_vendor_create(this->ike_sa
, TRUE
));
1192 if (!has_queued(this, TASK_ISAKMP_CERT_PRE
))
1194 queue_task(this, (task_t
*)isakmp_cert_pre_create(this->ike_sa
, TRUE
));
1196 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1197 if (peer_cfg
->use_aggressive(peer_cfg
))
1199 if (!has_queued(this, TASK_AGGRESSIVE_MODE
))
1201 queue_task(this, (task_t
*)aggressive_mode_create(this->ike_sa
, TRUE
));
1206 if (!has_queued(this, TASK_MAIN_MODE
))
1208 queue_task(this, (task_t
*)main_mode_create(this->ike_sa
, TRUE
));
1211 if (!has_queued(this, TASK_ISAKMP_CERT_POST
))
1213 queue_task(this, (task_t
*)isakmp_cert_post_create(this->ike_sa
, TRUE
));
1215 if (!has_queued(this, TASK_ISAKMP_NATD
))
1217 queue_task(this, (task_t
*)isakmp_natd_create(this->ike_sa
, TRUE
));
1221 METHOD(task_manager_t
, queue_ike_reauth
, void,
1222 private_task_manager_t
*this)
1224 enumerator_t
*enumerator
;
1225 child_sa_t
*child_sa
;
1229 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1230 this->ike_sa
->get_version(this->ike_sa
), TRUE
);
1232 { /* shouldn't happen */
1236 new->set_peer_cfg(new, this->ike_sa
->get_peer_cfg(this->ike_sa
));
1237 host
= this->ike_sa
->get_other_host(this->ike_sa
);
1238 new->set_other_host(new, host
->clone(host
));
1239 host
= this->ike_sa
->get_my_host(this->ike_sa
);
1240 new->set_my_host(new, host
->clone(host
));
1241 host
= this->ike_sa
->get_virtual_ip(this->ike_sa
, TRUE
);
1244 new->set_virtual_ip(new, TRUE
, host
);
1247 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1248 while (enumerator
->enumerate(enumerator
, &child_sa
))
1250 this->ike_sa
->remove_child_sa(this->ike_sa
, enumerator
);
1251 new->add_child_sa(new, child_sa
);
1253 enumerator
->destroy(enumerator
);
1255 if (!new->get_child_count(new))
1256 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1259 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1260 while (enumerator
->enumerate(enumerator
, &task
))
1262 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1264 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
1265 task
->migrate(task
, new);
1266 new->queue_task(new, task
);
1269 enumerator
->destroy(enumerator
);
1272 if (new->initiate(new, NULL
, 0, NULL
, NULL
) != DESTROY_ME
)
1274 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1275 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
1279 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1280 DBG1(DBG_IKE
, "reauthenticating IKE_SA failed");
1282 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1285 METHOD(task_manager_t
, queue_ike_rekey
, void,
1286 private_task_manager_t
*this)
1288 queue_ike_reauth(this);
1291 METHOD(task_manager_t
, queue_ike_delete
, void,
1292 private_task_manager_t
*this)
1294 enumerator_t
*enumerator
;
1295 child_sa_t
*child_sa
;
1297 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1298 while (enumerator
->enumerate(enumerator
, &child_sa
))
1300 queue_task(this, (task_t
*)
1301 quick_delete_create(this->ike_sa
, child_sa
->get_protocol(child_sa
),
1302 child_sa
->get_spi(child_sa
, TRUE
), FALSE
, FALSE
));
1304 enumerator
->destroy(enumerator
);
1306 queue_task(this, (task_t
*)isakmp_delete_create(this->ike_sa
, TRUE
));
1309 METHOD(task_manager_t
, queue_mobike
, void,
1310 private_task_manager_t
*this, bool roam
, bool address
)
1312 /* Not supported in IKEv1 */
1315 METHOD(task_manager_t
, queue_child
, void,
1316 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1317 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1321 task
= quick_mode_create(this->ike_sa
, cfg
, tsi
, tsr
);
1322 task
->use_reqid(task
, reqid
);
1324 queue_task(this, &task
->task
);
1327 METHOD(task_manager_t
, queue_child_rekey
, void,
1328 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1330 child_sa_t
*child_sa
;
1334 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, TRUE
);
1337 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, FALSE
);
1339 if (child_sa
&& child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
1341 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
1342 cfg
= child_sa
->get_config(child_sa
);
1343 task
= quick_mode_create(this->ike_sa
, cfg
->get_ref(cfg
), NULL
, NULL
);
1344 task
->use_reqid(task
, child_sa
->get_reqid(child_sa
));
1345 task
->rekey(task
, child_sa
->get_spi(child_sa
, TRUE
));
1347 queue_task(this, &task
->task
);
1351 METHOD(task_manager_t
, queue_child_delete
, void,
1352 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
,
1355 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
, protocol
,
1356 spi
, FALSE
, expired
));
1359 METHOD(task_manager_t
, queue_dpd
, void,
1360 private_task_manager_t
*this)
1362 u_int32_t t
= 0, retransmit
;
1364 queue_task(this, (task_t
*)isakmp_dpd_create(this->ike_sa
, TRUE
,
1367 /* schedule DPD timeout job using the same timeout as a retransmitting
1368 * IKE message would have. */
1369 for (retransmit
= 0; retransmit
<= this->retransmit_tries
; retransmit
++)
1371 t
+= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
1372 pow(this->retransmit_base
, retransmit
));
1374 lib
->scheduler
->schedule_job_ms(lib
->scheduler
,
1375 (job_t
*)dpd_timeout_job_create(this->ike_sa
->get_id(this->ike_sa
)), t
);
1378 METHOD(task_manager_t
, adopt_tasks
, void,
1379 private_task_manager_t
*this, task_manager_t
*other_public
)
1381 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1384 /* move queued tasks from other to this */
1385 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1386 (void**)&task
) == SUCCESS
)
1388 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1389 task
->migrate(task
, this->ike_sa
);
1390 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1394 METHOD(task_manager_t
, busy
, bool,
1395 private_task_manager_t
*this)
1397 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1400 METHOD(task_manager_t
, incr_mid
, void,
1401 private_task_manager_t
*this, bool initiate
)
1405 METHOD(task_manager_t
, reset
, void,
1406 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1408 enumerator_t
*enumerator
;
1411 /* reset message counters and retransmit packets */
1412 DESTROY_IF(this->responding
.packet
);
1413 DESTROY_IF(this->initiating
.packet
);
1414 this->responding
.packet
= NULL
;
1415 this->responding
.seqnr
= RESPONDING_SEQ
;
1416 this->responding
.retransmitted
= 0;
1417 this->initiating
.packet
= NULL
;
1418 this->initiating
.mid
= 0;
1419 this->initiating
.seqnr
= 0;
1420 this->initiating
.retransmitted
= 0;
1421 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1422 if (initiate
!= UINT_MAX
)
1424 this->dpd_send
= initiate
;
1426 if (respond
!= UINT_MAX
)
1428 this->dpd_recv
= respond
;
1431 /* reset queued tasks */
1432 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1433 while (enumerator
->enumerate(enumerator
, &task
))
1435 task
->migrate(task
, this->ike_sa
);
1437 enumerator
->destroy(enumerator
);
1439 /* reset active tasks */
1440 while (this->active_tasks
->remove_last(this->active_tasks
,
1441 (void**)&task
) == SUCCESS
)
1443 task
->migrate(task
, this->ike_sa
);
1444 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1448 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1449 private_task_manager_t
*this, task_queue_t queue
)
1453 case TASK_QUEUE_ACTIVE
:
1454 return this->active_tasks
->create_enumerator(this->active_tasks
);
1455 case TASK_QUEUE_PASSIVE
:
1456 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
1457 case TASK_QUEUE_QUEUED
:
1458 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
1460 return enumerator_create_empty();
1464 METHOD(task_manager_t
, destroy
, void,
1465 private_task_manager_t
*this)
1469 this->active_tasks
->destroy(this->active_tasks
);
1470 this->queued_tasks
->destroy(this->queued_tasks
);
1471 this->passive_tasks
->destroy(this->passive_tasks
);
1473 DESTROY_IF(this->queued
);
1474 DESTROY_IF(this->responding
.packet
);
1475 DESTROY_IF(this->initiating
.packet
);
1476 DESTROY_IF(this->rng
);
1483 task_manager_v1_t
*task_manager_v1_create(ike_sa_t
*ike_sa
)
1485 private_task_manager_t
*this;
1490 .process_message
= _process_message
,
1491 .queue_task
= _queue_task
,
1492 .queue_ike
= _queue_ike
,
1493 .queue_ike_rekey
= _queue_ike_rekey
,
1494 .queue_ike_reauth
= _queue_ike_reauth
,
1495 .queue_ike_delete
= _queue_ike_delete
,
1496 .queue_mobike
= _queue_mobike
,
1497 .queue_child
= _queue_child
,
1498 .queue_child_rekey
= _queue_child_rekey
,
1499 .queue_child_delete
= _queue_child_delete
,
1500 .queue_dpd
= _queue_dpd
,
1501 .initiate
= _initiate
,
1502 .retransmit
= _retransmit
,
1503 .incr_mid
= _incr_mid
,
1505 .adopt_tasks
= _adopt_tasks
,
1507 .create_task_enumerator
= _create_task_enumerator
,
1508 .destroy
= _destroy
,
1512 .type
= EXCHANGE_TYPE_UNDEFINED
,
1515 .seqnr
= RESPONDING_SEQ
,
1518 .rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
),
1519 .queued_tasks
= linked_list_create(),
1520 .active_tasks
= linked_list_create(),
1521 .passive_tasks
= linked_list_create(),
1522 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
1523 "%s.retransmit_tries", RETRANSMIT_TRIES
, charon
->name
),
1524 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
1525 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, charon
->name
),
1526 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
1527 "%s.retransmit_base", RETRANSMIT_BASE
, charon
->name
),
1532 DBG1(DBG_IKE
, "no RNG found, unable to create IKE_SA");
1537 this->rng
->get_bytes(this->rng
, sizeof(this->dpd_send
),
1538 (void*)&this->dpd_send
);
1539 this->dpd_send
&= 0x7FFFFFFF;
1541 return &this->public;