2 * Copyright (C) 2007-2016 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * HSR 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>
39 #include <processing/jobs/process_message_job.h>
41 #include <collections/array.h>
44 * Number of old messages hashes we keep for retransmission.
46 * In Main Mode, we must ignore messages from a previous message pair if
47 * we already continued to the next. Otherwise a late retransmission
48 * could be considered as a reply to the newer request.
50 #define MAX_OLD_HASHES 2
53 * First sequence number of responding packets.
55 * To distinguish retransmission jobs for initiating and responding packets,
56 * we split up the sequence counter and use the upper half for responding.
58 #define RESPONDING_SEQ INT_MAX
60 typedef struct exchange_t exchange_t
;
63 * An exchange in the air, used do detect and handle retransmission
68 * Message ID used for this transaction
73 * generated packet for retransmission
78 typedef struct private_task_manager_t private_task_manager_t
;
81 * private data of the task manager
83 struct private_task_manager_t
{
88 task_manager_v1_t
public;
91 * associated IKE_SA we are serving
96 * RNG to create message IDs
101 * Exchange we are currently handling as responder
105 * Message ID of the last response
110 * Hash of a previously received message
115 * packet(s) for retransmission
120 * Sequence number of the last sent message
125 * how many times we have retransmitted so far
132 * Exchange we are currently handling as initiator
136 * Message ID of the exchange
141 * Hashes of old responses we can ignore
143 uint32_t old_hashes
[MAX_OLD_HASHES
];
146 * Position in old hash array
151 * Sequence number of the last sent message
156 * how many times we have retransmitted so far
161 * packet(s) for retransmission
166 * type of the initiated exchange
168 exchange_type_t type
;
173 * Message we are currently defragmenting, if any (only one at a time)
178 * List of queued tasks not yet in action
180 linked_list_t
*queued_tasks
;
183 * List of active tasks, initiated by ourselves
185 linked_list_t
*active_tasks
;
188 * List of tasks initiated by peer
190 linked_list_t
*passive_tasks
;
193 * Queued messages not yet ready to process
198 * Number of times we retransmit messages before giving up
200 u_int retransmit_tries
;
203 * Retransmission timeout
205 double retransmit_timeout
;
208 * Base to calculate retransmission timeout
210 double retransmit_base
;
213 * Sequence number for sending DPD requests
218 * Sequence number for received DPD requests
224 * Reset retransmission packet list
226 static void clear_packets(array_t
*array
)
230 while (array_remove(array
, ARRAY_TAIL
, &packet
))
232 packet
->destroy(packet
);
236 METHOD(task_manager_t
, flush_queue
, void,
237 private_task_manager_t
*this, task_queue_t queue
)
244 this->queued
->destroy(this->queued
);
249 case TASK_QUEUE_ACTIVE
:
250 list
= this->active_tasks
;
251 /* cancel pending retransmits */
252 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
253 clear_packets(this->initiating
.packets
);
255 case TASK_QUEUE_PASSIVE
:
256 list
= this->passive_tasks
;
258 case TASK_QUEUE_QUEUED
:
259 list
= this->queued_tasks
;
264 while (list
->remove_last(list
, (void**)&task
) == SUCCESS
)
270 METHOD(task_manager_t
, flush
, void,
271 private_task_manager_t
*this)
273 flush_queue(this, TASK_QUEUE_QUEUED
);
274 flush_queue(this, TASK_QUEUE_PASSIVE
);
275 flush_queue(this, TASK_QUEUE_ACTIVE
);
279 * move a task of a specific type from the queue to the active list
281 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
283 enumerator_t
*enumerator
;
287 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
288 while (enumerator
->enumerate(enumerator
, (void**)&task
))
290 if (task
->get_type(task
) == type
)
292 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
293 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
294 this->active_tasks
->insert_last(this->active_tasks
, task
);
299 enumerator
->destroy(enumerator
);
304 * Send packets in the given array (they get cloned)
306 static void send_packets(private_task_manager_t
*this, array_t
*packets
)
308 enumerator_t
*enumerator
;
311 enumerator
= array_create_enumerator(packets
);
312 while (enumerator
->enumerate(enumerator
, &packet
))
314 charon
->sender
->send(charon
->sender
, packet
->clone(packet
));
316 enumerator
->destroy(enumerator
);
320 * Generates the given message and stores packet(s) in the given array
322 static bool generate_message(private_task_manager_t
*this, message_t
*message
,
325 enumerator_t
*fragments
;
328 if (this->ike_sa
->generate_message_fragmented(this->ike_sa
, message
,
329 &fragments
) != SUCCESS
)
333 while (fragments
->enumerate(fragments
, &fragment
))
335 array_insert_create(packets
, ARRAY_TAIL
, fragment
);
337 fragments
->destroy(fragments
);
342 * Retransmit a packet (or its fragments)
344 static status_t
retransmit_packet(private_task_manager_t
*this, uint32_t seqnr
,
345 u_int mid
, u_int retransmitted
, array_t
*packets
)
350 array_get(packets
, 0, &packet
);
351 if (retransmitted
> this->retransmit_tries
)
353 DBG1(DBG_IKE
, "giving up after %u retransmits", retransmitted
- 1);
354 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
, packet
);
357 t
= (uint32_t)(this->retransmit_timeout
* 1000.0 *
358 pow(this->retransmit_base
, retransmitted
));
361 DBG1(DBG_IKE
, "sending retransmit %u of %s message ID %u, seq %u",
362 retransmitted
, seqnr
< RESPONDING_SEQ
? "request" : "response",
363 mid
, seqnr
< RESPONDING_SEQ
? seqnr
: seqnr
- RESPONDING_SEQ
);
364 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
, packet
,
367 send_packets(this, packets
);
368 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
369 retransmit_job_create(seqnr
, this->ike_sa
->get_id(this->ike_sa
)), t
);
373 METHOD(task_manager_t
, retransmit
, status_t
,
374 private_task_manager_t
*this, uint32_t seqnr
)
376 status_t status
= SUCCESS
;
378 if (seqnr
== this->initiating
.seqnr
&&
379 array_count(this->initiating
.packets
))
381 status
= retransmit_packet(this, seqnr
, this->initiating
.mid
,
382 this->initiating
.retransmitted
, this->initiating
.packets
);
383 if (status
== SUCCESS
)
385 this->initiating
.retransmitted
++;
388 if (seqnr
== this->responding
.seqnr
&&
389 array_count(this->responding
.packets
))
391 status
= retransmit_packet(this, seqnr
, this->responding
.mid
,
392 this->responding
.retransmitted
, this->responding
.packets
);
393 if (status
== SUCCESS
)
395 this->responding
.retransmitted
++;
402 * Check if we have to wait for a mode config before starting a quick mode
404 static bool mode_config_expected(private_task_manager_t
*this)
406 enumerator_t
*enumerator
;
407 peer_cfg_t
*peer_cfg
;
412 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
415 if (peer_cfg
->use_pull_mode(peer_cfg
))
417 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
418 if (!enumerator
->enumerate(enumerator
, &pool
))
419 { /* no pool configured */
420 enumerator
->destroy(enumerator
);
423 enumerator
->destroy(enumerator
);
429 enumerator
= peer_cfg
->create_virtual_ip_enumerator(peer_cfg
);
430 if (!enumerator
->enumerate(enumerator
, &host
))
431 { /* not requesting a vip */
432 enumerator
->destroy(enumerator
);
435 enumerator
->destroy(enumerator
);
439 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
,
441 if (!enumerator
->enumerate(enumerator
, &host
))
442 { /* expecting a VIP exchange, but no VIP assigned yet */
443 enumerator
->destroy(enumerator
);
446 enumerator
->destroy(enumerator
);
451 METHOD(task_manager_t
, initiate
, status_t
,
452 private_task_manager_t
*this)
454 enumerator_t
*enumerator
;
458 exchange_type_t exchange
= EXCHANGE_TYPE_UNDEFINED
;
459 bool new_mid
= FALSE
, expect_response
= FALSE
, cancelled
= FALSE
, keep
= FALSE
;
461 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
&&
462 this->initiating
.type
!= INFORMATIONAL_V1
)
464 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
465 exchange_type_names
, this->initiating
.type
);
466 /* do not initiate if we already have a message in the air */
470 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
472 DBG2(DBG_IKE
, "activating new tasks");
473 switch (this->ike_sa
->get_state(this->ike_sa
))
476 activate_task(this, TASK_ISAKMP_VENDOR
);
477 activate_task(this, TASK_ISAKMP_CERT_PRE
);
478 if (activate_task(this, TASK_MAIN_MODE
))
482 else if (activate_task(this, TASK_AGGRESSIVE_MODE
))
484 exchange
= AGGRESSIVE
;
486 activate_task(this, TASK_ISAKMP_CERT_POST
);
487 activate_task(this, TASK_ISAKMP_NATD
);
490 if (activate_task(this, TASK_ISAKMP_DELETE
))
492 exchange
= INFORMATIONAL_V1
;
496 if (activate_task(this, TASK_XAUTH
))
498 exchange
= TRANSACTION
;
502 if (activate_task(this, TASK_INFORMATIONAL
))
504 exchange
= INFORMATIONAL_V1
;
509 case IKE_ESTABLISHED
:
510 if (activate_task(this, TASK_MODE_CONFIG
))
512 exchange
= TRANSACTION
;
516 if (activate_task(this, TASK_QUICK_DELETE
))
518 exchange
= INFORMATIONAL_V1
;
522 if (activate_task(this, TASK_ISAKMP_DELETE
))
524 exchange
= INFORMATIONAL_V1
;
528 if (!mode_config_expected(this) &&
529 activate_task(this, TASK_QUICK_MODE
))
531 exchange
= QUICK_MODE
;
535 if (activate_task(this, TASK_INFORMATIONAL
))
537 exchange
= INFORMATIONAL_V1
;
541 if (activate_task(this, TASK_ISAKMP_DPD
))
543 exchange
= INFORMATIONAL_V1
;
549 if (activate_task(this, TASK_ISAKMP_DELETE
))
551 exchange
= INFORMATIONAL_V1
;
562 DBG2(DBG_IKE
, "reinitiating already active tasks");
563 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
564 while (enumerator
->enumerate(enumerator
, (void**)&task
))
566 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
567 switch (task
->get_type(task
))
572 case TASK_AGGRESSIVE_MODE
:
573 exchange
= AGGRESSIVE
;
575 case TASK_QUICK_MODE
:
576 exchange
= QUICK_MODE
;
579 exchange
= TRANSACTION
;
587 enumerator
->destroy(enumerator
);
590 if (exchange
== EXCHANGE_TYPE_UNDEFINED
)
592 DBG2(DBG_IKE
, "nothing to initiate");
593 /* nothing to do yet... */
597 me
= this->ike_sa
->get_my_host(this->ike_sa
);
598 other
= this->ike_sa
->get_other_host(this->ike_sa
);
602 if (!this->rng
->get_bytes(this->rng
, sizeof(this->initiating
.mid
),
603 (void*)&this->initiating
.mid
))
605 DBG1(DBG_IKE
, "failed to allocate message ID, destroying IKE_SA");
610 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
611 message
->set_message_id(message
, this->initiating
.mid
);
612 message
->set_source(message
, me
->clone(me
));
613 message
->set_destination(message
, other
->clone(other
));
614 message
->set_exchange_type(message
, exchange
);
615 this->initiating
.type
= exchange
;
616 this->initiating
.retransmitted
= 0;
618 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
619 while (enumerator
->enumerate(enumerator
, (void*)&task
))
621 switch (task
->build(task
, message
))
624 /* task completed, remove it */
625 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
626 if (task
->get_type(task
) == TASK_AGGRESSIVE_MODE
||
627 task
->get_type(task
) == TASK_QUICK_MODE
)
628 { /* last message of three message exchange */
634 expect_response
= TRUE
;
635 /* processed, but task needs another exchange */
642 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
644 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
648 /* critical failure, destroy IKE_SA */
649 enumerator
->destroy(enumerator
);
650 message
->destroy(message
);
656 enumerator
->destroy(enumerator
);
658 if (this->active_tasks
->get_count(this->active_tasks
) == 0 &&
659 (exchange
== QUICK_MODE
|| exchange
== AGGRESSIVE
))
660 { /* tasks completed, no exchange active anymore */
661 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
665 message
->destroy(message
);
666 return initiate(this);
669 clear_packets(this->initiating
.packets
);
670 if (!generate_message(this, message
, &this->initiating
.packets
))
672 /* message generation failed. There is nothing more to do than to
674 message
->destroy(message
);
676 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
680 this->initiating
.seqnr
++;
683 message
->destroy(message
);
684 return retransmit(this, this->initiating
.seqnr
);
686 send_packets(this, this->initiating
.packets
);
689 clear_packets(this->initiating
.packets
);
691 message
->destroy(message
);
693 if (exchange
== INFORMATIONAL_V1
)
695 switch (this->ike_sa
->get_state(this->ike_sa
))
698 /* close after sending an INFORMATIONAL when unestablished */
701 /* close after sending a DELETE */
707 return initiate(this);
711 * build a response depending on the "passive" task list
713 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
715 enumerator_t
*enumerator
;
719 bool delete = FALSE
, cancelled
= FALSE
, expect_request
= FALSE
;
721 me
= request
->get_destination(request
);
722 other
= request
->get_source(request
);
724 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
725 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
726 /* send response along the path the request came in */
727 message
->set_source(message
, me
->clone(me
));
728 message
->set_destination(message
, other
->clone(other
));
729 message
->set_message_id(message
, request
->get_message_id(request
));
730 message
->set_request(message
, FALSE
);
732 this->responding
.mid
= request
->get_message_id(request
);
733 this->responding
.retransmitted
= 0;
734 this->responding
.seqnr
++;
736 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
737 while (enumerator
->enumerate(enumerator
, (void*)&task
))
739 switch (task
->build(task
, message
))
742 /* task completed, remove it */
743 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
747 /* processed, but task needs another exchange */
748 if (task
->get_type(task
) == TASK_QUICK_MODE
||
749 task
->get_type(task
) == TASK_AGGRESSIVE_MODE
)
750 { /* we rely on initiator retransmission, except for
751 * three-message exchanges */
752 expect_request
= TRUE
;
759 if (task
->get_type(task
) == TASK_QUICK_MODE
)
760 { /* not responsible for this exchange */
766 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
769 /* destroy IKE_SA, but SEND response first */
775 enumerator
->destroy(enumerator
);
777 clear_packets(this->responding
.packets
);
780 message
->destroy(message
);
781 return initiate(this);
783 if (!generate_message(this, message
, &this->responding
.packets
))
785 message
->destroy(message
);
786 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
789 message
->destroy(message
);
791 if (expect_request
&& !delete)
793 return retransmit(this, this->responding
.seqnr
);
795 send_packets(this, this->responding
.packets
);
804 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
805 * The notify protocol_id is set to ISAKMP
807 static void send_notify(private_task_manager_t
*this, message_t
*request
,
811 array_t
*packets
= NULL
;
815 if (request
->get_exchange_type(request
) == INFORMATIONAL_V1
)
816 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
817 DBG1(DBG_IKE
, "ignore malformed INFORMATIONAL request");
820 if (!this->rng
->get_bytes(this->rng
, sizeof(mid
), (void*)&mid
))
822 DBG1(DBG_IKE
, "failed to allocate message ID");
825 response
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
826 response
->set_exchange_type(response
, INFORMATIONAL_V1
);
827 response
->set_request(response
, TRUE
);
828 response
->set_message_id(response
, mid
);
829 response
->add_payload(response
, (payload_t
*)
830 notify_payload_create_from_protocol_and_type(PLV1_NOTIFY
,
833 me
= this->ike_sa
->get_my_host(this->ike_sa
);
834 if (me
->is_anyaddr(me
))
836 me
= request
->get_destination(request
);
837 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
839 other
= this->ike_sa
->get_other_host(this->ike_sa
);
840 if (other
->is_anyaddr(other
))
842 other
= request
->get_source(request
);
843 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
845 response
->set_source(response
, me
->clone(me
));
846 response
->set_destination(response
, other
->clone(other
));
847 if (generate_message(this, response
, &packets
))
849 send_packets(this, packets
);
851 clear_packets(packets
);
852 array_destroy(packets
);
853 response
->destroy(response
);
857 * Process a DPD request/response
859 static bool process_dpd(private_task_manager_t
*this, message_t
*message
)
861 notify_payload_t
*notify
;
866 type
= DPD_R_U_THERE
;
867 notify
= message
->get_notify(message
, type
);
870 type
= DPD_R_U_THERE_ACK
;
871 notify
= message
->get_notify(message
, type
);
877 data
= notify
->get_notification_data(notify
);
882 seq
= untoh32(data
.ptr
);
884 if (type
== DPD_R_U_THERE
)
886 if (this->dpd_recv
== 0 || seq
== this->dpd_recv
)
887 { /* check sequence validity */
888 this->dpd_recv
= seq
+ 1;
889 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
890 time_monotonic(NULL
));
892 /* but respond anyway */
893 this->ike_sa
->queue_task(this->ike_sa
,
894 &isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE_ACK
, seq
)->task
);
896 else /* DPD_R_U_THERE_ACK */
898 if (seq
== this->dpd_send
- 1)
900 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
901 time_monotonic(NULL
));
905 DBG1(DBG_IKE
, "received invalid DPD sequence number %u "
906 "(expected %u), ignored", seq
, this->dpd_send
- 1);
913 * Check if we already have a quick mode task queued for the exchange with the
916 static bool have_quick_mode_task(private_task_manager_t
*this, uint32_t mid
)
918 enumerator_t
*enumerator
;
923 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
924 while (enumerator
->enumerate(enumerator
, &task
))
926 if (task
->get_type(task
) == TASK_QUICK_MODE
)
928 qm
= (quick_mode_t
*)task
;
929 if (qm
->get_mid(qm
) == mid
)
936 enumerator
->destroy(enumerator
);
941 * Check if we still have a specific task queued
943 static bool have_task_queued(private_task_manager_t
*this, task_type_t type
)
945 enumerator_t
*enumerator
;
949 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
950 while (enumerator
->enumerate(enumerator
, &task
))
952 if (task
->get_type(task
) == type
)
958 enumerator
->destroy(enumerator
);
963 * handle an incoming request message
965 static status_t
process_request(private_task_manager_t
*this,
968 enumerator_t
*enumerator
;
970 bool send_response
= FALSE
, dpd
= FALSE
;
972 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
973 message
->get_exchange_type(message
) == QUICK_MODE
||
974 this->passive_tasks
->get_count(this->passive_tasks
) == 0)
975 { /* create tasks depending on request type, if not already some queued */
976 switch (message
->get_exchange_type(message
))
979 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
980 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
981 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
982 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
983 task
= (task_t
*)main_mode_create(this->ike_sa
, FALSE
);
984 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
985 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
986 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
987 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
988 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
991 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
992 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
993 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
994 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
995 task
= (task_t
*)aggressive_mode_create(this->ike_sa
, FALSE
);
996 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
997 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
998 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
999 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1000 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1003 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1005 DBG1(DBG_IKE
, "received quick mode request for "
1006 "unestablished IKE_SA, ignored");
1009 if (have_quick_mode_task(this, message
->get_message_id(message
)))
1013 task
= (task_t
*)quick_mode_create(this->ike_sa
, NULL
,
1015 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1017 case INFORMATIONAL_V1
:
1018 if (process_dpd(this, message
))
1024 task
= (task_t
*)informational_create(this->ike_sa
, NULL
);
1025 this->passive_tasks
->insert_first(this->passive_tasks
, task
);
1029 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
1031 task
= (task_t
*)mode_config_create(this->ike_sa
,
1036 task
= (task_t
*)xauth_create(this->ike_sa
, FALSE
);
1038 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1046 return initiate(this);
1048 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
, time_monotonic(NULL
));
1050 /* let the tasks process the message */
1051 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
1052 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1054 switch (task
->process(task
, message
))
1057 /* task completed, remove it */
1058 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1059 task
->destroy(task
);
1062 /* processed, but task needs at least another call to build() */
1063 send_response
= TRUE
;
1066 send_response
= FALSE
;
1069 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1070 { /* not responsible for this exchange */
1076 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1079 /* critical failure, destroy IKE_SA */
1080 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1081 enumerator
->destroy(enumerator
);
1082 task
->destroy(task
);
1087 enumerator
->destroy(enumerator
);
1091 if (build_response(this, message
) != SUCCESS
)
1097 { /* We don't send a response, so don't retransmit one if we get
1098 * the same message again. */
1099 clear_packets(this->responding
.packets
);
1102 this->queued
->get_exchange_type(this->queued
) == INFORMATIONAL_V1
)
1107 queued
= this->queued
;
1108 this->queued
= NULL
;
1109 status
= this->public.task_manager
.process_message(
1110 &this->public.task_manager
, queued
);
1111 queued
->destroy(queued
);
1112 if (status
== DESTROY_ME
)
1117 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0 &&
1118 this->queued_tasks
->get_count(this->queued_tasks
) > 0)
1120 /* passive tasks completed, check if an active task has been queued,
1121 * such as XAUTH or modeconfig push */
1122 return initiate(this);
1128 * handle an incoming response message
1130 static status_t
process_response(private_task_manager_t
*this,
1133 enumerator_t
*enumerator
;
1138 if (message
->get_exchange_type(message
) != this->initiating
.type
)
1140 /* Windows server sends a fourth quick mode message having an initial
1141 * contact notify. Ignore this message for compatibility. */
1142 if (this->initiating
.type
== EXCHANGE_TYPE_UNDEFINED
&&
1143 message
->get_exchange_type(message
) == QUICK_MODE
&&
1144 message
->get_notify(message
, INITIAL_CONTACT
))
1146 DBG1(DBG_IKE
, "ignoring fourth Quick Mode message");
1149 DBG1(DBG_IKE
, "received %N response, but expected %N",
1150 exchange_type_names
, message
->get_exchange_type(message
),
1151 exchange_type_names
, this->initiating
.type
);
1152 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1156 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
1157 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1159 switch (task
->process(task
, message
))
1162 /* task completed, remove it */
1163 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1164 task
->destroy(task
);
1167 /* processed, but task needs another exchange */
1173 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1176 /* critical failure, destroy IKE_SA */
1177 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1178 enumerator
->destroy(enumerator
);
1179 task
->destroy(task
);
1184 enumerator
->destroy(enumerator
);
1186 if (this->initiating
.retransmitted
> 1)
1188 packet_t
*packet
= NULL
;
1189 array_get(this->initiating
.packets
, 0, &packet
);
1190 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_CLEARED
, packet
);
1192 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1193 clear_packets(this->initiating
.packets
);
1195 if (this->queued
&& !this->active_tasks
->get_count(this->active_tasks
) &&
1196 this->queued
->get_exchange_type(this->queued
) == TRANSACTION
)
1198 queued
= this->queued
;
1199 this->queued
= NULL
;
1200 status
= this->public.task_manager
.process_message(
1201 &this->public.task_manager
, queued
);
1202 queued
->destroy(queued
);
1203 if (status
== DESTROY_ME
)
1209 return initiate(this);
1212 static status_t
handle_fragment(private_task_manager_t
*this, message_t
*msg
)
1218 this->defrag
= message_create_defrag(msg
);
1224 status
= this->defrag
->add_fragment(this->defrag
, msg
);
1225 if (status
== SUCCESS
)
1227 lib
->processor
->queue_job(lib
->processor
,
1228 (job_t
*)process_message_job_create(this->defrag
));
1229 this->defrag
= NULL
;
1230 /* do not process the last fragment */
1237 * Parse the given message and verify that it is valid.
1239 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
1243 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
1245 if (status
!= SUCCESS
)
1250 DBG1(DBG_IKE
, "unsupported exchange type");
1251 send_notify(this, msg
, INVALID_EXCHANGE_TYPE
);
1254 DBG1(DBG_IKE
, "message parsing failed");
1255 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1258 DBG1(DBG_IKE
, "message verification failed");
1259 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1262 DBG1(DBG_IKE
, "integrity check failed");
1263 send_notify(this, msg
, INVALID_HASH_INFORMATION
);
1266 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1267 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1271 DBG1(DBG_IKE
, "%N %s with message ID %u processing failed",
1272 exchange_type_names
, msg
->get_exchange_type(msg
),
1273 msg
->get_request(msg
) ? "request" : "response",
1274 msg
->get_message_id(msg
));
1276 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
, status
);
1278 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1279 { /* invalid initiation attempt, close SA */
1284 if (msg
->get_first_payload_type(msg
) == PLV1_FRAGMENT
)
1286 return handle_fragment(this, msg
);
1292 * Queue the given message if possible
1294 static status_t
queue_message(private_task_manager_t
*this, message_t
*msg
)
1298 DBG1(DBG_IKE
, "ignoring %N request, queue full",
1299 exchange_type_names
, msg
->get_exchange_type(msg
));
1302 this->queued
= message_create_from_packet(msg
->get_packet(msg
));
1303 if (this->queued
->parse_header(this->queued
) != SUCCESS
)
1305 this->queued
->destroy(this->queued
);
1306 this->queued
= NULL
;
1309 DBG1(DBG_IKE
, "queueing %N request as tasks still active",
1310 exchange_type_names
, msg
->get_exchange_type(msg
));
1314 METHOD(task_manager_t
, process_message
, status_t
,
1315 private_task_manager_t
*this, message_t
*msg
)
1317 uint32_t hash
, mid
, i
;
1321 /* TODO-IKEv1: update hosts more selectively */
1322 me
= msg
->get_destination(msg
);
1323 other
= msg
->get_source(msg
);
1324 mid
= msg
->get_message_id(msg
);
1325 hash
= chunk_hash(msg
->get_packet_data(msg
));
1326 for (i
= 0; i
< MAX_OLD_HASHES
; i
++)
1328 if (this->initiating
.old_hashes
[i
] == hash
)
1330 if (array_count(this->initiating
.packets
) &&
1331 i
== (this->initiating
.old_hash_pos
% MAX_OLD_HASHES
) &&
1332 (msg
->get_exchange_type(msg
) == QUICK_MODE
||
1333 msg
->get_exchange_type(msg
) == AGGRESSIVE
))
1335 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1336 "resending last request", mid
);
1337 send_packets(this, this->initiating
.packets
);
1340 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1341 "but next request already sent", mid
);
1346 if ((mid
&& mid
== this->initiating
.mid
) ||
1347 (this->initiating
.mid
== 0 &&
1348 msg
->get_exchange_type(msg
) == this->initiating
.type
&&
1349 this->active_tasks
->get_count(this->active_tasks
)))
1351 msg
->set_request(msg
, FALSE
);
1352 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1353 status
= parse_message(this, msg
);
1354 if (status
== NEED_MORE
)
1358 if (status
!= SUCCESS
)
1362 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1363 time_monotonic(NULL
));
1364 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1365 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1366 if (process_response(this, msg
) != SUCCESS
)
1371 this->initiating
.old_hashes
[(++this->initiating
.old_hash_pos
) %
1372 MAX_OLD_HASHES
] = hash
;
1376 if (hash
== this->responding
.hash
)
1378 if (array_count(this->responding
.packets
))
1380 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1381 "retransmitting response", mid
);
1382 send_packets(this, this->responding
.packets
);
1384 else if (array_count(this->initiating
.packets
) &&
1385 this->initiating
.type
== INFORMATIONAL_V1
)
1387 DBG1(DBG_IKE
, "received retransmit of DPD request, "
1388 "retransmitting response");
1389 send_packets(this, this->initiating
.packets
);
1393 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1394 "but no response to retransmit", mid
);
1396 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1400 /* reject Main/Aggressive Modes once established */
1401 if (msg
->get_exchange_type(msg
) == ID_PROT
||
1402 msg
->get_exchange_type(msg
) == AGGRESSIVE
)
1404 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CREATED
&&
1405 this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
&&
1406 msg
->get_first_payload_type(msg
) != PLV1_FRAGMENT
)
1408 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1409 exchange_type_names
, msg
->get_exchange_type(msg
));
1414 /* drop XAuth/Mode Config/Quick Mode messages until we received the last
1415 * Aggressive Mode message. since Informational messages are not
1416 * retransmitted we queue them. */
1417 if (have_task_queued(this, TASK_AGGRESSIVE_MODE
))
1419 if (msg
->get_exchange_type(msg
) == INFORMATIONAL_V1
)
1421 return queue_message(this, msg
);
1423 else if (msg
->get_exchange_type(msg
) != AGGRESSIVE
)
1425 DBG1(DBG_IKE
, "ignoring %N request while phase 1 is incomplete",
1426 exchange_type_names
, msg
->get_exchange_type(msg
));
1431 /* queue XAuth/Mode Config messages unless the Main Mode exchange we
1432 * initiated is complete */
1433 if (msg
->get_exchange_type(msg
) == TRANSACTION
&&
1434 this->active_tasks
->get_count(this->active_tasks
))
1436 return queue_message(this, msg
);
1439 /* some peers send INITIAL_CONTACT notifies during XAuth, cache it */
1440 if (have_task_queued(this, TASK_XAUTH
) &&
1441 msg
->get_exchange_type(msg
) == INFORMATIONAL_V1
)
1443 return queue_message(this, msg
);
1446 msg
->set_request(msg
, TRUE
);
1447 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1448 status
= parse_message(this, msg
);
1449 if (status
== NEED_MORE
)
1453 if (status
!= SUCCESS
)
1457 /* if this IKE_SA is virgin, we check for a config */
1458 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1460 ike_sa_id_t
*ike_sa_id
;
1464 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1466 if (ike_cfg
== NULL
)
1468 /* no config found for these hosts, destroy */
1469 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1470 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1471 send_notify(this, msg
, NO_PROPOSAL_CHOSEN
);
1474 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1475 ike_cfg
->destroy(ike_cfg
);
1476 /* add a timeout if peer does not establish it completely */
1477 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1478 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1479 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1480 lib
->settings
->get_int(lib
->settings
,
1481 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1484 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1485 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1486 if (process_request(this, msg
) != SUCCESS
)
1491 this->responding
.hash
= hash
;
1497 * Check if a given task has been queued already
1499 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1501 enumerator_t
*enumerator
;
1505 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1506 while (enumerator
->enumerate(enumerator
, &task
))
1508 if (task
->get_type(task
) == type
)
1514 enumerator
->destroy(enumerator
);
1518 METHOD(task_manager_t
, queue_task_delayed
, void,
1519 private_task_manager_t
*this, task_t
*task
, uint32_t delay
)
1521 task_type_t type
= task
->get_type(task
);
1525 case TASK_MODE_CONFIG
:
1527 if (has_queued(this, type
))
1529 task
->destroy(task
);
1536 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1537 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1540 METHOD(task_manager_t
, queue_task
, void,
1541 private_task_manager_t
*this, task_t
*task
)
1543 queue_task_delayed(this, task
, 0);
1546 METHOD(task_manager_t
, queue_ike
, void,
1547 private_task_manager_t
*this)
1549 peer_cfg_t
*peer_cfg
;
1551 if (!has_queued(this, TASK_ISAKMP_VENDOR
))
1553 queue_task(this, (task_t
*)isakmp_vendor_create(this->ike_sa
, TRUE
));
1555 if (!has_queued(this, TASK_ISAKMP_CERT_PRE
))
1557 queue_task(this, (task_t
*)isakmp_cert_pre_create(this->ike_sa
, TRUE
));
1559 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1560 if (peer_cfg
->use_aggressive(peer_cfg
))
1562 if (!has_queued(this, TASK_AGGRESSIVE_MODE
))
1564 queue_task(this, (task_t
*)aggressive_mode_create(this->ike_sa
, TRUE
));
1569 if (!has_queued(this, TASK_MAIN_MODE
))
1571 queue_task(this, (task_t
*)main_mode_create(this->ike_sa
, TRUE
));
1574 if (!has_queued(this, TASK_ISAKMP_CERT_POST
))
1576 queue_task(this, (task_t
*)isakmp_cert_post_create(this->ike_sa
, TRUE
));
1578 if (!has_queued(this, TASK_ISAKMP_NATD
))
1580 queue_task(this, (task_t
*)isakmp_natd_create(this->ike_sa
, TRUE
));
1584 METHOD(task_manager_t
, queue_ike_reauth
, void,
1585 private_task_manager_t
*this)
1587 enumerator_t
*enumerator
;
1588 child_sa_t
*child_sa
;
1592 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1593 this->ike_sa
->get_version(this->ike_sa
), TRUE
);
1595 { /* shouldn't happen */
1599 new->set_peer_cfg(new, this->ike_sa
->get_peer_cfg(this->ike_sa
));
1600 host
= this->ike_sa
->get_other_host(this->ike_sa
);
1601 new->set_other_host(new, host
->clone(host
));
1602 host
= this->ike_sa
->get_my_host(this->ike_sa
);
1603 new->set_my_host(new, host
->clone(host
));
1604 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
, TRUE
);
1605 while (enumerator
->enumerate(enumerator
, &host
))
1607 new->add_virtual_ip(new, TRUE
, host
);
1609 enumerator
->destroy(enumerator
);
1611 charon
->bus
->children_migrate(charon
->bus
, new->get_id(new),
1612 new->get_unique_id(new));
1613 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1614 while (enumerator
->enumerate(enumerator
, &child_sa
))
1616 this->ike_sa
->remove_child_sa(this->ike_sa
, enumerator
);
1617 new->add_child_sa(new, child_sa
);
1619 enumerator
->destroy(enumerator
);
1620 charon
->bus
->set_sa(charon
->bus
, new);
1621 charon
->bus
->children_migrate(charon
->bus
, NULL
, 0);
1622 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1624 if (!new->get_child_count(new))
1625 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1628 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1629 while (enumerator
->enumerate(enumerator
, &task
))
1631 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1633 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
1634 task
->migrate(task
, new);
1635 new->queue_task(new, task
);
1638 enumerator
->destroy(enumerator
);
1641 if (new->initiate(new, NULL
, 0, NULL
, NULL
) != DESTROY_ME
)
1643 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1644 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
1648 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1649 DBG1(DBG_IKE
, "reauthenticating IKE_SA failed");
1651 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1654 METHOD(task_manager_t
, queue_ike_rekey
, void,
1655 private_task_manager_t
*this)
1657 queue_ike_reauth(this);
1660 METHOD(task_manager_t
, queue_ike_delete
, void,
1661 private_task_manager_t
*this)
1663 enumerator_t
*enumerator
;
1664 child_sa_t
*child_sa
;
1666 /* cancel any currently active task to get the DELETE done quickly */
1667 flush_queue(this, TASK_QUEUE_ACTIVE
);
1669 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1670 while (enumerator
->enumerate(enumerator
, &child_sa
))
1672 queue_task(this, (task_t
*)
1673 quick_delete_create(this->ike_sa
, child_sa
->get_protocol(child_sa
),
1674 child_sa
->get_spi(child_sa
, TRUE
), FALSE
, FALSE
));
1676 enumerator
->destroy(enumerator
);
1678 queue_task(this, (task_t
*)isakmp_delete_create(this->ike_sa
, TRUE
));
1681 METHOD(task_manager_t
, queue_mobike
, void,
1682 private_task_manager_t
*this, bool roam
, bool address
)
1684 /* Not supported in IKEv1 */
1687 METHOD(task_manager_t
, queue_child
, void,
1688 private_task_manager_t
*this, child_cfg_t
*cfg
, uint32_t reqid
,
1689 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1693 task
= quick_mode_create(this->ike_sa
, cfg
, tsi
, tsr
);
1694 task
->use_reqid(task
, reqid
);
1696 queue_task(this, &task
->task
);
1700 * Check if two CHILD_SAs have the same traffic selector
1702 static bool have_equal_ts(child_sa_t
*child1
, child_sa_t
*child2
, bool local
)
1704 enumerator_t
*e1
, *e2
;
1705 traffic_selector_t
*ts1
, *ts2
;
1708 e1
= child1
->create_ts_enumerator(child1
, local
);
1709 e2
= child2
->create_ts_enumerator(child2
, local
);
1710 if (e1
->enumerate(e1
, &ts1
) && e2
->enumerate(e2
, &ts2
))
1712 equal
= ts1
->equals(ts1
, ts2
);
1721 * Check if a CHILD_SA is redundant and we should delete instead of rekey
1723 static bool is_redundant(private_task_manager_t
*this, child_sa_t
*child_sa
)
1725 enumerator_t
*enumerator
;
1726 child_sa_t
*current
;
1727 bool redundant
= FALSE
;
1729 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1730 while (enumerator
->enumerate(enumerator
, ¤t
))
1732 if (current
->get_state(current
) == CHILD_INSTALLED
&&
1733 streq(current
->get_name(current
), child_sa
->get_name(child_sa
)) &&
1734 have_equal_ts(current
, child_sa
, TRUE
) &&
1735 have_equal_ts(current
, child_sa
, FALSE
) &&
1736 current
->get_lifetime(current
, FALSE
) >
1737 child_sa
->get_lifetime(child_sa
, FALSE
))
1739 DBG1(DBG_IKE
, "deleting redundant CHILD_SA %s{%d}",
1740 child_sa
->get_name(child_sa
),
1741 child_sa
->get_unique_id(child_sa
));
1746 enumerator
->destroy(enumerator
);
1752 * Get the first traffic selector of a CHILD_SA, local or remote
1754 static traffic_selector_t
* get_first_ts(child_sa_t
*child_sa
, bool local
)
1756 traffic_selector_t
*ts
= NULL
;
1757 enumerator_t
*enumerator
;
1759 enumerator
= child_sa
->create_ts_enumerator(child_sa
, local
);
1760 enumerator
->enumerate(enumerator
, &ts
);
1761 enumerator
->destroy(enumerator
);
1766 METHOD(task_manager_t
, queue_child_rekey
, void,
1767 private_task_manager_t
*this, protocol_id_t protocol
, uint32_t spi
)
1769 child_sa_t
*child_sa
;
1773 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, TRUE
);
1776 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, FALSE
);
1778 if (child_sa
&& child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
1780 if (is_redundant(this, child_sa
))
1782 child_sa
->set_state(child_sa
, CHILD_REKEYED
);
1783 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
,
1784 protocol
, spi
, FALSE
, FALSE
));
1788 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
1789 cfg
= child_sa
->get_config(child_sa
);
1790 task
= quick_mode_create(this->ike_sa
, cfg
->get_ref(cfg
),
1791 get_first_ts(child_sa
, TRUE
), get_first_ts(child_sa
, FALSE
));
1792 task
->use_reqid(task
, child_sa
->get_reqid(child_sa
));
1793 task
->use_marks(task
, child_sa
->get_mark(child_sa
, TRUE
).value
,
1794 child_sa
->get_mark(child_sa
, FALSE
).value
);
1795 task
->rekey(task
, child_sa
->get_spi(child_sa
, TRUE
));
1797 queue_task(this, &task
->task
);
1802 METHOD(task_manager_t
, queue_child_delete
, void,
1803 private_task_manager_t
*this, protocol_id_t protocol
, uint32_t spi
,
1806 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
, protocol
,
1807 spi
, FALSE
, expired
));
1810 METHOD(task_manager_t
, queue_dpd
, void,
1811 private_task_manager_t
*this)
1813 peer_cfg_t
*peer_cfg
;
1814 uint32_t t
, retransmit
;
1816 queue_task(this, (task_t
*)isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE
,
1818 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1820 /* compute timeout in milliseconds */
1821 t
= 1000 * peer_cfg
->get_dpd_timeout(peer_cfg
);
1824 /* use the same timeout as a retransmitting IKE message would have */
1825 for (retransmit
= 0; retransmit
<= this->retransmit_tries
; retransmit
++)
1827 t
+= (uint32_t)(this->retransmit_timeout
* 1000.0 *
1828 pow(this->retransmit_base
, retransmit
));
1831 /* compensate for the already elapsed dpd delay */
1832 t
-= 1000 * peer_cfg
->get_dpd(peer_cfg
);
1834 /* schedule DPD timeout job */
1835 lib
->scheduler
->schedule_job_ms(lib
->scheduler
,
1836 (job_t
*)dpd_timeout_job_create(this->ike_sa
->get_id(this->ike_sa
)), t
);
1839 METHOD(task_manager_t
, adopt_tasks
, void,
1840 private_task_manager_t
*this, task_manager_t
*other_public
)
1842 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1845 /* move queued tasks from other to this */
1846 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1847 (void**)&task
) == SUCCESS
)
1849 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1850 task
->migrate(task
, this->ike_sa
);
1851 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1856 * Migrates child-creating tasks from src to dst
1858 static void migrate_child_tasks(private_task_manager_t
*this,
1859 linked_list_t
*src
, linked_list_t
*dst
)
1861 enumerator_t
*enumerator
;
1864 enumerator
= src
->create_enumerator(src
);
1865 while (enumerator
->enumerate(enumerator
, &task
))
1867 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1869 src
->remove_at(src
, enumerator
);
1870 task
->migrate(task
, this->ike_sa
);
1871 dst
->insert_last(dst
, task
);
1874 enumerator
->destroy(enumerator
);
1877 METHOD(task_manager_t
, adopt_child_tasks
, void,
1878 private_task_manager_t
*this, task_manager_t
*other_public
)
1880 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1882 /* move active child tasks from other to this */
1883 migrate_child_tasks(this, other
->active_tasks
, this->queued_tasks
);
1884 /* do the same for queued tasks */
1885 migrate_child_tasks(this, other
->queued_tasks
, this->queued_tasks
);
1888 METHOD(task_manager_t
, busy
, bool,
1889 private_task_manager_t
*this)
1891 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1894 METHOD(task_manager_t
, incr_mid
, void,
1895 private_task_manager_t
*this, bool initiate
)
1899 METHOD(task_manager_t
, reset
, void,
1900 private_task_manager_t
*this, uint32_t initiate
, uint32_t respond
)
1902 enumerator_t
*enumerator
;
1905 /* reset message counters and retransmit packets */
1906 clear_packets(this->responding
.packets
);
1907 clear_packets(this->initiating
.packets
);
1908 this->responding
.seqnr
= RESPONDING_SEQ
;
1909 this->responding
.retransmitted
= 0;
1910 this->initiating
.mid
= 0;
1911 this->initiating
.seqnr
= 0;
1912 this->initiating
.retransmitted
= 0;
1913 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1914 DESTROY_IF(this->defrag
);
1915 this->defrag
= NULL
;
1916 if (initiate
!= UINT_MAX
)
1918 this->dpd_send
= initiate
;
1920 if (respond
!= UINT_MAX
)
1922 this->dpd_recv
= respond
;
1925 /* reset queued tasks */
1926 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1927 while (enumerator
->enumerate(enumerator
, &task
))
1929 task
->migrate(task
, this->ike_sa
);
1931 enumerator
->destroy(enumerator
);
1933 /* reset active tasks */
1934 while (this->active_tasks
->remove_last(this->active_tasks
,
1935 (void**)&task
) == SUCCESS
)
1937 task
->migrate(task
, this->ike_sa
);
1938 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1942 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1943 private_task_manager_t
*this, task_queue_t queue
)
1947 case TASK_QUEUE_ACTIVE
:
1948 return this->active_tasks
->create_enumerator(this->active_tasks
);
1949 case TASK_QUEUE_PASSIVE
:
1950 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
1951 case TASK_QUEUE_QUEUED
:
1952 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
1954 return enumerator_create_empty();
1958 METHOD(task_manager_t
, destroy
, void,
1959 private_task_manager_t
*this)
1963 this->active_tasks
->destroy(this->active_tasks
);
1964 this->queued_tasks
->destroy(this->queued_tasks
);
1965 this->passive_tasks
->destroy(this->passive_tasks
);
1966 DESTROY_IF(this->defrag
);
1968 DESTROY_IF(this->queued
);
1969 clear_packets(this->responding
.packets
);
1970 array_destroy(this->responding
.packets
);
1971 clear_packets(this->initiating
.packets
);
1972 array_destroy(this->initiating
.packets
);
1973 DESTROY_IF(this->rng
);
1980 task_manager_v1_t
*task_manager_v1_create(ike_sa_t
*ike_sa
)
1982 private_task_manager_t
*this;
1987 .process_message
= _process_message
,
1988 .queue_task
= _queue_task
,
1989 .queue_task_delayed
= _queue_task_delayed
,
1990 .queue_ike
= _queue_ike
,
1991 .queue_ike_rekey
= _queue_ike_rekey
,
1992 .queue_ike_reauth
= _queue_ike_reauth
,
1993 .queue_ike_delete
= _queue_ike_delete
,
1994 .queue_mobike
= _queue_mobike
,
1995 .queue_child
= _queue_child
,
1996 .queue_child_rekey
= _queue_child_rekey
,
1997 .queue_child_delete
= _queue_child_delete
,
1998 .queue_dpd
= _queue_dpd
,
1999 .initiate
= _initiate
,
2000 .retransmit
= _retransmit
,
2001 .incr_mid
= _incr_mid
,
2003 .adopt_tasks
= _adopt_tasks
,
2004 .adopt_child_tasks
= _adopt_child_tasks
,
2006 .create_task_enumerator
= _create_task_enumerator
,
2008 .flush_queue
= _flush_queue
,
2009 .destroy
= _destroy
,
2013 .type
= EXCHANGE_TYPE_UNDEFINED
,
2016 .seqnr
= RESPONDING_SEQ
,
2019 .rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
),
2020 .queued_tasks
= linked_list_create(),
2021 .active_tasks
= linked_list_create(),
2022 .passive_tasks
= linked_list_create(),
2023 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
2024 "%s.retransmit_tries", RETRANSMIT_TRIES
, lib
->ns
),
2025 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
2026 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, lib
->ns
),
2027 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
2028 "%s.retransmit_base", RETRANSMIT_BASE
, lib
->ns
),
2033 DBG1(DBG_IKE
, "no RNG found, unable to create IKE_SA");
2037 if (!this->rng
->get_bytes(this->rng
, sizeof(this->dpd_send
),
2038 (void*)&this->dpd_send
))
2040 DBG1(DBG_IKE
, "failed to allocate message ID, unable to create IKE_SA");
2044 this->dpd_send
&= 0x7FFFFFFF;
2046 return &this->public;