2 * Copyright (C) 2007-2019 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 * Jitter to apply to calculated retransmit timeout (in percent)
215 u_int retransmit_jitter
;
218 * Limit retransmit timeout to this value
220 uint32_t retransmit_limit
;
223 * Sequence number for sending DPD requests
228 * Sequence number for received DPD requests
234 * Reset retransmission packet list
236 static void clear_packets(array_t
*array
)
240 while (array_remove(array
, ARRAY_TAIL
, &packet
))
242 packet
->destroy(packet
);
246 METHOD(task_manager_t
, flush_queue
, void,
247 private_task_manager_t
*this, task_queue_t queue
)
254 this->queued
->destroy(this->queued
);
259 case TASK_QUEUE_ACTIVE
:
260 list
= this->active_tasks
;
261 /* cancel pending retransmits */
262 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
263 clear_packets(this->initiating
.packets
);
265 case TASK_QUEUE_PASSIVE
:
266 list
= this->passive_tasks
;
268 case TASK_QUEUE_QUEUED
:
269 list
= this->queued_tasks
;
274 while (list
->remove_last(list
, (void**)&task
) == SUCCESS
)
280 METHOD(task_manager_t
, flush
, void,
281 private_task_manager_t
*this)
283 flush_queue(this, TASK_QUEUE_QUEUED
);
284 flush_queue(this, TASK_QUEUE_PASSIVE
);
285 flush_queue(this, TASK_QUEUE_ACTIVE
);
289 * move a task of a specific type from the queue to the active list
291 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
293 enumerator_t
*enumerator
;
297 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
298 while (enumerator
->enumerate(enumerator
, (void**)&task
))
300 if (task
->get_type(task
) == type
)
302 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
303 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
304 this->active_tasks
->insert_last(this->active_tasks
, task
);
309 enumerator
->destroy(enumerator
);
314 * Send packets in the given array (they get cloned)
316 static void send_packets(private_task_manager_t
*this, array_t
*packets
)
318 enumerator_t
*enumerator
;
321 enumerator
= array_create_enumerator(packets
);
322 while (enumerator
->enumerate(enumerator
, &packet
))
324 charon
->sender
->send(charon
->sender
, packet
->clone(packet
));
326 enumerator
->destroy(enumerator
);
330 * Generates the given message and stores packet(s) in the given array
332 static bool generate_message(private_task_manager_t
*this, message_t
*message
,
335 enumerator_t
*fragments
;
338 if (this->ike_sa
->generate_message_fragmented(this->ike_sa
, message
,
339 &fragments
) != SUCCESS
)
343 while (fragments
->enumerate(fragments
, &fragment
))
345 array_insert_create(packets
, ARRAY_TAIL
, fragment
);
347 fragments
->destroy(fragments
);
352 * Retransmit a packet (or its fragments)
354 static status_t
retransmit_packet(private_task_manager_t
*this, uint32_t seqnr
,
355 u_int mid
, u_int retransmitted
, array_t
*packets
)
358 uint32_t t
, max_jitter
;
360 array_get(packets
, 0, &packet
);
361 if (retransmitted
> this->retransmit_tries
)
363 DBG1(DBG_IKE
, "giving up after %u retransmits", retransmitted
- 1);
364 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
, packet
);
367 t
= (uint32_t)(this->retransmit_timeout
* 1000.0 *
368 pow(this->retransmit_base
, retransmitted
));
369 if (this->retransmit_limit
)
371 t
= min(t
, this->retransmit_limit
);
373 if (this->retransmit_jitter
)
375 max_jitter
= (t
/ 100.0) * this->retransmit_jitter
;
376 t
-= max_jitter
* (random() / (RAND_MAX
+ 1.0));
380 DBG1(DBG_IKE
, "sending retransmit %u of %s message ID %u, seq %u",
381 retransmitted
, seqnr
< RESPONDING_SEQ
? "request" : "response",
382 mid
, seqnr
< RESPONDING_SEQ
? seqnr
: seqnr
- RESPONDING_SEQ
);
383 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
, packet
,
386 send_packets(this, packets
);
387 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
388 retransmit_job_create(seqnr
, this->ike_sa
->get_id(this->ike_sa
)), t
);
392 METHOD(task_manager_t
, retransmit
, status_t
,
393 private_task_manager_t
*this, uint32_t seqnr
)
395 status_t status
= SUCCESS
;
397 if (seqnr
== this->initiating
.seqnr
&&
398 array_count(this->initiating
.packets
))
400 status
= retransmit_packet(this, seqnr
, this->initiating
.mid
,
401 this->initiating
.retransmitted
, this->initiating
.packets
);
402 if (status
== SUCCESS
)
404 this->initiating
.retransmitted
++;
407 if (seqnr
== this->responding
.seqnr
&&
408 array_count(this->responding
.packets
))
410 status
= retransmit_packet(this, seqnr
, this->responding
.mid
,
411 this->responding
.retransmitted
, this->responding
.packets
);
412 if (status
== SUCCESS
)
414 this->responding
.retransmitted
++;
421 * Check if we have to wait for a mode config before starting a quick mode
423 static bool mode_config_expected(private_task_manager_t
*this)
425 enumerator_t
*enumerator
;
426 peer_cfg_t
*peer_cfg
;
431 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
434 if (peer_cfg
->use_pull_mode(peer_cfg
))
436 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
437 if (!enumerator
->enumerate(enumerator
, &pool
))
438 { /* no pool configured */
439 enumerator
->destroy(enumerator
);
442 enumerator
->destroy(enumerator
);
448 enumerator
= peer_cfg
->create_virtual_ip_enumerator(peer_cfg
);
449 if (!enumerator
->enumerate(enumerator
, &host
))
450 { /* not requesting a vip */
451 enumerator
->destroy(enumerator
);
454 enumerator
->destroy(enumerator
);
458 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
,
460 if (!enumerator
->enumerate(enumerator
, &host
))
461 { /* expecting a VIP exchange, but no VIP assigned yet */
462 enumerator
->destroy(enumerator
);
465 enumerator
->destroy(enumerator
);
470 METHOD(task_manager_t
, initiate
, status_t
,
471 private_task_manager_t
*this)
473 enumerator_t
*enumerator
;
477 exchange_type_t exchange
= EXCHANGE_TYPE_UNDEFINED
;
478 bool new_mid
= FALSE
, expect_response
= FALSE
, cancelled
= FALSE
, keep
= FALSE
;
480 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
&&
481 this->initiating
.type
!= INFORMATIONAL_V1
)
483 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
484 exchange_type_names
, this->initiating
.type
);
485 /* do not initiate if we already have a message in the air */
489 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
491 DBG2(DBG_IKE
, "activating new tasks");
492 switch (this->ike_sa
->get_state(this->ike_sa
))
495 activate_task(this, TASK_ISAKMP_VENDOR
);
496 activate_task(this, TASK_ISAKMP_CERT_PRE
);
497 if (activate_task(this, TASK_MAIN_MODE
))
501 else if (activate_task(this, TASK_AGGRESSIVE_MODE
))
503 exchange
= AGGRESSIVE
;
505 activate_task(this, TASK_ISAKMP_CERT_POST
);
506 activate_task(this, TASK_ISAKMP_NATD
);
509 if (activate_task(this, TASK_ISAKMP_DELETE
))
511 exchange
= INFORMATIONAL_V1
;
515 if (activate_task(this, TASK_XAUTH
))
517 exchange
= TRANSACTION
;
521 if (activate_task(this, TASK_INFORMATIONAL
))
523 exchange
= INFORMATIONAL_V1
;
528 case IKE_ESTABLISHED
:
529 if (activate_task(this, TASK_MODE_CONFIG
))
531 exchange
= TRANSACTION
;
535 if (activate_task(this, TASK_QUICK_DELETE
))
537 exchange
= INFORMATIONAL_V1
;
541 if (activate_task(this, TASK_ISAKMP_DELETE
))
543 exchange
= INFORMATIONAL_V1
;
547 if (activate_task(this, TASK_ISAKMP_DPD
))
549 exchange
= INFORMATIONAL_V1
;
553 if (!mode_config_expected(this) &&
554 activate_task(this, TASK_QUICK_MODE
))
556 exchange
= QUICK_MODE
;
560 if (activate_task(this, TASK_INFORMATIONAL
))
562 exchange
= INFORMATIONAL_V1
;
568 if (activate_task(this, TASK_ISAKMP_DELETE
))
570 exchange
= INFORMATIONAL_V1
;
574 if (activate_task(this, TASK_ISAKMP_DPD
))
576 exchange
= INFORMATIONAL_V1
;
587 DBG2(DBG_IKE
, "reinitiating already active tasks");
588 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
589 while (enumerator
->enumerate(enumerator
, (void**)&task
))
591 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
592 switch (task
->get_type(task
))
597 case TASK_AGGRESSIVE_MODE
:
598 exchange
= AGGRESSIVE
;
600 case TASK_QUICK_MODE
:
601 exchange
= QUICK_MODE
;
604 exchange
= TRANSACTION
;
612 enumerator
->destroy(enumerator
);
615 if (exchange
== EXCHANGE_TYPE_UNDEFINED
)
617 DBG2(DBG_IKE
, "nothing to initiate");
618 /* nothing to do yet... */
622 me
= this->ike_sa
->get_my_host(this->ike_sa
);
623 other
= this->ike_sa
->get_other_host(this->ike_sa
);
627 if (!this->rng
->get_bytes(this->rng
, sizeof(this->initiating
.mid
),
628 (void*)&this->initiating
.mid
))
630 DBG1(DBG_IKE
, "failed to allocate message ID, destroying IKE_SA");
635 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
636 message
->set_message_id(message
, this->initiating
.mid
);
637 message
->set_source(message
, me
->clone(me
));
638 message
->set_destination(message
, other
->clone(other
));
639 message
->set_exchange_type(message
, exchange
);
640 this->initiating
.type
= exchange
;
641 this->initiating
.retransmitted
= 0;
643 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
644 while (enumerator
->enumerate(enumerator
, (void*)&task
))
646 switch (task
->build(task
, message
))
649 /* task completed, remove it */
650 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
651 if (task
->get_type(task
) == TASK_AGGRESSIVE_MODE
||
652 task
->get_type(task
) == TASK_QUICK_MODE
)
653 { /* last message of three message exchange */
659 expect_response
= TRUE
;
660 /* processed, but task needs another exchange */
667 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
669 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
673 /* critical failure, destroy IKE_SA */
674 enumerator
->destroy(enumerator
);
675 message
->destroy(message
);
681 enumerator
->destroy(enumerator
);
683 if (this->active_tasks
->get_count(this->active_tasks
) == 0 &&
684 (exchange
== QUICK_MODE
|| exchange
== AGGRESSIVE
))
685 { /* tasks completed, no exchange active anymore */
686 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
690 message
->destroy(message
);
691 return initiate(this);
694 clear_packets(this->initiating
.packets
);
695 if (!generate_message(this, message
, &this->initiating
.packets
))
697 /* message generation failed. There is nothing more to do than to
699 message
->destroy(message
);
701 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
705 this->initiating
.seqnr
++;
708 message
->destroy(message
);
709 return retransmit(this, this->initiating
.seqnr
);
711 send_packets(this, this->initiating
.packets
);
714 clear_packets(this->initiating
.packets
);
716 message
->destroy(message
);
718 if (exchange
== INFORMATIONAL_V1
)
720 switch (this->ike_sa
->get_state(this->ike_sa
))
723 /* close after sending an INFORMATIONAL when unestablished */
724 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
727 /* close after sending a DELETE */
733 return initiate(this);
737 * build a response depending on the "passive" task list
739 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
741 enumerator_t
*enumerator
;
745 bool delete = FALSE
, cancelled
= FALSE
, expect_request
= FALSE
;
747 me
= request
->get_destination(request
);
748 other
= request
->get_source(request
);
750 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
751 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
752 /* send response along the path the request came in */
753 message
->set_source(message
, me
->clone(me
));
754 message
->set_destination(message
, other
->clone(other
));
755 message
->set_message_id(message
, request
->get_message_id(request
));
756 message
->set_request(message
, FALSE
);
758 this->responding
.mid
= request
->get_message_id(request
);
759 this->responding
.retransmitted
= 0;
760 this->responding
.seqnr
++;
762 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
763 while (enumerator
->enumerate(enumerator
, (void*)&task
))
765 switch (task
->build(task
, message
))
768 /* task completed, remove it */
769 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
773 /* processed, but task needs another exchange */
774 if (task
->get_type(task
) == TASK_QUICK_MODE
||
775 task
->get_type(task
) == TASK_AGGRESSIVE_MODE
)
776 { /* we rely on initiator retransmission, except for
777 * three-message exchanges */
778 expect_request
= TRUE
;
785 if (task
->get_type(task
) == TASK_QUICK_MODE
)
786 { /* not responsible for this exchange */
792 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
795 /* destroy IKE_SA, but SEND response first */
801 enumerator
->destroy(enumerator
);
803 clear_packets(this->responding
.packets
);
806 message
->destroy(message
);
807 return initiate(this);
809 if (!generate_message(this, message
, &this->responding
.packets
))
811 message
->destroy(message
);
812 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
815 message
->destroy(message
);
817 if (expect_request
&& !delete)
819 return retransmit(this, this->responding
.seqnr
);
821 send_packets(this, this->responding
.packets
);
830 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
831 * The notify protocol_id is set to ISAKMP
833 static void send_notify(private_task_manager_t
*this, message_t
*request
,
837 array_t
*packets
= NULL
;
841 if (request
->get_exchange_type(request
) == INFORMATIONAL_V1
)
842 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
843 DBG1(DBG_IKE
, "ignore malformed INFORMATIONAL request");
846 if (!this->rng
->get_bytes(this->rng
, sizeof(mid
), (void*)&mid
))
848 DBG1(DBG_IKE
, "failed to allocate message ID");
851 response
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
852 response
->set_exchange_type(response
, INFORMATIONAL_V1
);
853 response
->set_request(response
, TRUE
);
854 response
->set_message_id(response
, mid
);
855 response
->add_payload(response
, (payload_t
*)
856 notify_payload_create_from_protocol_and_type(PLV1_NOTIFY
,
859 me
= this->ike_sa
->get_my_host(this->ike_sa
);
860 if (me
->is_anyaddr(me
))
862 me
= request
->get_destination(request
);
863 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
865 other
= this->ike_sa
->get_other_host(this->ike_sa
);
866 if (other
->is_anyaddr(other
))
868 other
= request
->get_source(request
);
869 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
871 response
->set_source(response
, me
->clone(me
));
872 response
->set_destination(response
, other
->clone(other
));
873 if (generate_message(this, response
, &packets
))
875 send_packets(this, packets
);
877 clear_packets(packets
);
878 array_destroy(packets
);
879 response
->destroy(response
);
883 * Process a DPD request/response
885 static bool process_dpd(private_task_manager_t
*this, message_t
*message
)
887 notify_payload_t
*notify
;
892 type
= DPD_R_U_THERE
;
893 notify
= message
->get_notify(message
, type
);
896 type
= DPD_R_U_THERE_ACK
;
897 notify
= message
->get_notify(message
, type
);
903 data
= notify
->get_notification_data(notify
);
908 seq
= untoh32(data
.ptr
);
910 if (type
== DPD_R_U_THERE
)
912 if (this->dpd_recv
== 0 || seq
== this->dpd_recv
)
913 { /* check sequence validity */
914 this->dpd_recv
= seq
+ 1;
915 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
916 time_monotonic(NULL
));
918 /* but respond anyway */
919 this->ike_sa
->queue_task(this->ike_sa
,
920 &isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE_ACK
, seq
)->task
);
922 else /* DPD_R_U_THERE_ACK */
924 if (seq
== this->dpd_send
)
927 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
928 time_monotonic(NULL
));
932 DBG1(DBG_IKE
, "received invalid DPD sequence number %u "
933 "(expected %u), ignored", seq
, this->dpd_send
);
940 * Check if we already have a quick mode task queued for the exchange with the
943 static bool have_quick_mode_task(private_task_manager_t
*this, uint32_t mid
)
945 enumerator_t
*enumerator
;
950 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
951 while (enumerator
->enumerate(enumerator
, &task
))
953 if (task
->get_type(task
) == TASK_QUICK_MODE
)
955 qm
= (quick_mode_t
*)task
;
956 if (qm
->get_mid(qm
) == mid
)
963 enumerator
->destroy(enumerator
);
968 * Check if we still have a specific task queued
970 static bool have_task_queued(private_task_manager_t
*this, task_type_t type
)
972 enumerator_t
*enumerator
;
976 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
977 while (enumerator
->enumerate(enumerator
, &task
))
979 if (task
->get_type(task
) == type
)
985 enumerator
->destroy(enumerator
);
990 * handle an incoming request message
992 static status_t
process_request(private_task_manager_t
*this,
995 enumerator_t
*enumerator
;
997 bool send_response
= FALSE
, dpd
= FALSE
;
999 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
1000 message
->get_exchange_type(message
) == QUICK_MODE
||
1001 this->passive_tasks
->get_count(this->passive_tasks
) == 0)
1002 { /* create tasks depending on request type, if not already some queued */
1003 switch (message
->get_exchange_type(message
))
1006 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
1007 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1008 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
1009 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1010 task
= (task_t
*)main_mode_create(this->ike_sa
, FALSE
);
1011 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1012 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
1013 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1014 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1015 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1018 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
1019 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1020 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
1021 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1022 task
= (task_t
*)aggressive_mode_create(this->ike_sa
, FALSE
);
1023 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1024 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
1025 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1026 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1027 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1030 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1032 DBG1(DBG_IKE
, "received quick mode request for "
1033 "unestablished IKE_SA, ignored");
1036 if (have_quick_mode_task(this, message
->get_message_id(message
)))
1040 task
= (task_t
*)quick_mode_create(this->ike_sa
, NULL
,
1042 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1044 case INFORMATIONAL_V1
:
1045 if (process_dpd(this, message
))
1051 task
= (task_t
*)informational_create(this->ike_sa
, NULL
);
1052 this->passive_tasks
->insert_first(this->passive_tasks
, task
);
1056 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
1058 task
= (task_t
*)mode_config_create(this->ike_sa
,
1063 task
= (task_t
*)xauth_create(this->ike_sa
, FALSE
);
1065 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1073 return initiate(this);
1075 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
, time_monotonic(NULL
));
1077 /* let the tasks process the message */
1078 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
1079 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1081 switch (task
->process(task
, message
))
1084 /* task completed, remove it */
1085 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1086 task
->destroy(task
);
1089 /* processed, but task needs at least another call to build() */
1090 send_response
= TRUE
;
1093 send_response
= FALSE
;
1096 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1097 { /* not responsible for this exchange */
1103 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1106 /* critical failure, destroy IKE_SA */
1107 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1108 enumerator
->destroy(enumerator
);
1109 task
->destroy(task
);
1114 enumerator
->destroy(enumerator
);
1118 if (build_response(this, message
) != SUCCESS
)
1125 if (this->responding
.retransmitted
> 1)
1127 packet_t
*packet
= NULL
;
1128 array_get(this->responding
.packets
, 0, &packet
);
1129 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_CLEARED
,
1132 /* We don't send a response, so don't retransmit one if we get
1133 * the same message again. */
1134 clear_packets(this->responding
.packets
);
1137 this->queued
->get_exchange_type(this->queued
) == INFORMATIONAL_V1
)
1142 queued
= this->queued
;
1143 this->queued
= NULL
;
1144 status
= this->public.task_manager
.process_message(
1145 &this->public.task_manager
, queued
);
1146 queued
->destroy(queued
);
1147 if (status
== DESTROY_ME
)
1152 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0 &&
1153 this->queued_tasks
->get_count(this->queued_tasks
) > 0)
1155 /* passive tasks completed, check if an active task has been queued,
1156 * such as XAUTH or modeconfig push */
1157 return initiate(this);
1163 * handle an incoming response message
1165 static status_t
process_response(private_task_manager_t
*this,
1168 enumerator_t
*enumerator
;
1173 if (message
->get_exchange_type(message
) != this->initiating
.type
)
1175 /* Windows server sends a fourth quick mode message having an initial
1176 * contact notify. Ignore this message for compatibility. */
1177 if (this->initiating
.type
== EXCHANGE_TYPE_UNDEFINED
&&
1178 message
->get_exchange_type(message
) == QUICK_MODE
&&
1179 message
->get_notify(message
, INITIAL_CONTACT
))
1181 DBG1(DBG_IKE
, "ignoring fourth Quick Mode message");
1184 DBG1(DBG_IKE
, "received %N response, but expected %N",
1185 exchange_type_names
, message
->get_exchange_type(message
),
1186 exchange_type_names
, this->initiating
.type
);
1187 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1191 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
1192 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1194 switch (task
->process(task
, message
))
1197 /* task completed, remove it */
1198 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1199 task
->destroy(task
);
1202 /* processed, but task needs another exchange */
1208 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1211 /* critical failure, destroy IKE_SA */
1212 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1213 enumerator
->destroy(enumerator
);
1214 task
->destroy(task
);
1219 enumerator
->destroy(enumerator
);
1221 if (this->initiating
.retransmitted
> 1)
1223 packet_t
*packet
= NULL
;
1224 array_get(this->initiating
.packets
, 0, &packet
);
1225 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_CLEARED
, packet
);
1227 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1228 clear_packets(this->initiating
.packets
);
1230 if (this->queued
&& !this->active_tasks
->get_count(this->active_tasks
) &&
1231 this->queued
->get_exchange_type(this->queued
) == TRANSACTION
)
1233 queued
= this->queued
;
1234 this->queued
= NULL
;
1235 status
= this->public.task_manager
.process_message(
1236 &this->public.task_manager
, queued
);
1237 queued
->destroy(queued
);
1238 if (status
== DESTROY_ME
)
1244 return initiate(this);
1247 static status_t
handle_fragment(private_task_manager_t
*this, message_t
*msg
)
1253 this->defrag
= message_create_defrag(msg
);
1259 status
= this->defrag
->add_fragment(this->defrag
, msg
);
1260 if (status
== SUCCESS
)
1262 lib
->processor
->queue_job(lib
->processor
,
1263 (job_t
*)process_message_job_create(this->defrag
));
1264 this->defrag
= NULL
;
1265 /* do not process the last fragment */
1272 * Parse the given message and verify that it is valid.
1274 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
1278 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
1280 if (status
!= SUCCESS
)
1285 DBG1(DBG_IKE
, "unsupported exchange type");
1286 send_notify(this, msg
, INVALID_EXCHANGE_TYPE
);
1289 DBG1(DBG_IKE
, "message parsing failed");
1290 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1293 DBG1(DBG_IKE
, "message verification failed");
1294 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1297 DBG1(DBG_IKE
, "integrity check failed");
1298 send_notify(this, msg
, INVALID_HASH_INFORMATION
);
1301 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1302 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1306 DBG1(DBG_IKE
, "%N %s with message ID %u processing failed",
1307 exchange_type_names
, msg
->get_exchange_type(msg
),
1308 msg
->get_request(msg
) ? "request" : "response",
1309 msg
->get_message_id(msg
));
1311 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
, status
);
1313 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1314 { /* invalid initiation attempt, close SA */
1319 if (msg
->get_first_payload_type(msg
) == PLV1_FRAGMENT
)
1321 return handle_fragment(this, msg
);
1327 * Queue the given message if possible
1329 static status_t
queue_message(private_task_manager_t
*this, message_t
*msg
)
1333 DBG1(DBG_IKE
, "ignoring %N request, queue full",
1334 exchange_type_names
, msg
->get_exchange_type(msg
));
1337 this->queued
= message_create_from_packet(msg
->get_packet(msg
));
1338 if (this->queued
->parse_header(this->queued
) != SUCCESS
)
1340 this->queued
->destroy(this->queued
);
1341 this->queued
= NULL
;
1344 DBG1(DBG_IKE
, "queueing %N request as tasks still active",
1345 exchange_type_names
, msg
->get_exchange_type(msg
));
1349 METHOD(task_manager_t
, process_message
, status_t
,
1350 private_task_manager_t
*this, message_t
*msg
)
1352 uint32_t hash
, mid
, i
;
1356 /* TODO-IKEv1: update hosts more selectively */
1357 me
= msg
->get_destination(msg
);
1358 other
= msg
->get_source(msg
);
1359 mid
= msg
->get_message_id(msg
);
1360 hash
= chunk_hash(msg
->get_packet_data(msg
));
1361 for (i
= 0; i
< MAX_OLD_HASHES
; i
++)
1363 if (this->initiating
.old_hashes
[i
] == hash
)
1365 if (array_count(this->initiating
.packets
) &&
1366 i
== (this->initiating
.old_hash_pos
% MAX_OLD_HASHES
) &&
1367 (msg
->get_exchange_type(msg
) == QUICK_MODE
||
1368 msg
->get_exchange_type(msg
) == AGGRESSIVE
))
1370 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1371 "resending last request", mid
);
1372 send_packets(this, this->initiating
.packets
);
1375 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1376 "but next request already sent", mid
);
1381 if ((mid
&& mid
== this->initiating
.mid
) ||
1382 (this->initiating
.mid
== 0 &&
1383 msg
->get_exchange_type(msg
) == this->initiating
.type
&&
1384 this->active_tasks
->get_count(this->active_tasks
)))
1386 msg
->set_request(msg
, FALSE
);
1387 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1388 status
= parse_message(this, msg
);
1389 if (status
== NEED_MORE
)
1393 if (status
!= SUCCESS
)
1397 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1398 time_monotonic(NULL
));
1399 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1400 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1401 if (process_response(this, msg
) != SUCCESS
)
1406 this->initiating
.old_hashes
[(++this->initiating
.old_hash_pos
) %
1407 MAX_OLD_HASHES
] = hash
;
1411 if (hash
== this->responding
.hash
)
1413 if (array_count(this->responding
.packets
))
1415 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1416 "retransmitting response", mid
);
1417 send_packets(this, this->responding
.packets
);
1419 else if (array_count(this->initiating
.packets
) &&
1420 this->initiating
.type
== INFORMATIONAL_V1
)
1422 DBG1(DBG_IKE
, "received retransmit of DPD request, "
1423 "retransmitting response");
1424 send_packets(this, this->initiating
.packets
);
1428 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1429 "but no response to retransmit", mid
);
1431 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1435 /* reject Main/Aggressive Modes once established */
1436 if (msg
->get_exchange_type(msg
) == ID_PROT
||
1437 msg
->get_exchange_type(msg
) == AGGRESSIVE
)
1439 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CREATED
&&
1440 this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
&&
1441 msg
->get_first_payload_type(msg
) != PLV1_FRAGMENT
)
1443 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1444 exchange_type_names
, msg
->get_exchange_type(msg
));
1449 /* drop XAuth/Mode Config/Quick Mode messages until we received the last
1450 * Aggressive Mode message. since Informational messages are not
1451 * retransmitted we queue them. */
1452 if (have_task_queued(this, TASK_AGGRESSIVE_MODE
))
1454 if (msg
->get_exchange_type(msg
) == INFORMATIONAL_V1
)
1456 return queue_message(this, msg
);
1458 else if (msg
->get_exchange_type(msg
) != AGGRESSIVE
)
1460 DBG1(DBG_IKE
, "ignoring %N request while phase 1 is incomplete",
1461 exchange_type_names
, msg
->get_exchange_type(msg
));
1466 /* queue XAuth/Mode Config messages unless the Main Mode exchange we
1467 * initiated is complete */
1468 if (msg
->get_exchange_type(msg
) == TRANSACTION
&&
1469 this->active_tasks
->get_count(this->active_tasks
))
1471 return queue_message(this, msg
);
1474 /* some peers send INITIAL_CONTACT notifies during XAuth, cache it */
1475 if (have_task_queued(this, TASK_XAUTH
) &&
1476 msg
->get_exchange_type(msg
) == INFORMATIONAL_V1
)
1478 return queue_message(this, msg
);
1481 msg
->set_request(msg
, TRUE
);
1482 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1483 status
= parse_message(this, msg
);
1484 if (status
== NEED_MORE
)
1488 if (status
!= SUCCESS
)
1492 /* if this IKE_SA is virgin, we check for a config */
1493 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1495 ike_sa_id_t
*ike_sa_id
;
1499 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1501 if (ike_cfg
== NULL
)
1503 /* no config found for these hosts, destroy */
1504 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1505 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1506 send_notify(this, msg
, NO_PROPOSAL_CHOSEN
);
1509 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1510 ike_cfg
->destroy(ike_cfg
);
1511 /* add a timeout if peer does not establish it completely */
1512 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1513 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1514 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1515 lib
->settings
->get_int(lib
->settings
,
1516 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1519 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1520 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1521 if (process_request(this, msg
) != SUCCESS
)
1526 this->responding
.hash
= hash
;
1532 * Check if a given task has been queued already
1534 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1536 enumerator_t
*enumerator
;
1540 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1541 while (enumerator
->enumerate(enumerator
, &task
))
1543 if (task
->get_type(task
) == type
)
1549 enumerator
->destroy(enumerator
);
1553 METHOD(task_manager_t
, queue_task_delayed
, void,
1554 private_task_manager_t
*this, task_t
*task
, uint32_t delay
)
1556 task_type_t type
= task
->get_type(task
);
1560 case TASK_MODE_CONFIG
:
1562 if (has_queued(this, type
))
1564 task
->destroy(task
);
1571 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1572 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1575 METHOD(task_manager_t
, queue_task
, void,
1576 private_task_manager_t
*this, task_t
*task
)
1578 queue_task_delayed(this, task
, 0);
1581 METHOD(task_manager_t
, queue_ike
, void,
1582 private_task_manager_t
*this)
1584 peer_cfg_t
*peer_cfg
;
1586 if (!has_queued(this, TASK_ISAKMP_VENDOR
))
1588 queue_task(this, (task_t
*)isakmp_vendor_create(this->ike_sa
, TRUE
));
1590 if (!has_queued(this, TASK_ISAKMP_CERT_PRE
))
1592 queue_task(this, (task_t
*)isakmp_cert_pre_create(this->ike_sa
, TRUE
));
1594 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1595 if (peer_cfg
->use_aggressive(peer_cfg
))
1597 if (!has_queued(this, TASK_AGGRESSIVE_MODE
))
1599 queue_task(this, (task_t
*)aggressive_mode_create(this->ike_sa
, TRUE
));
1604 if (!has_queued(this, TASK_MAIN_MODE
))
1606 queue_task(this, (task_t
*)main_mode_create(this->ike_sa
, TRUE
));
1609 if (!has_queued(this, TASK_ISAKMP_CERT_POST
))
1611 queue_task(this, (task_t
*)isakmp_cert_post_create(this->ike_sa
, TRUE
));
1613 if (!has_queued(this, TASK_ISAKMP_NATD
))
1615 queue_task(this, (task_t
*)isakmp_natd_create(this->ike_sa
, TRUE
));
1619 METHOD(task_manager_t
, queue_ike_reauth
, void,
1620 private_task_manager_t
*this)
1622 enumerator_t
*enumerator
;
1623 child_sa_t
*child_sa
;
1627 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1628 this->ike_sa
->get_version(this->ike_sa
), TRUE
);
1630 { /* shouldn't happen */
1634 new->set_peer_cfg(new, this->ike_sa
->get_peer_cfg(this->ike_sa
));
1635 host
= this->ike_sa
->get_other_host(this->ike_sa
);
1636 new->set_other_host(new, host
->clone(host
));
1637 host
= this->ike_sa
->get_my_host(this->ike_sa
);
1638 new->set_my_host(new, host
->clone(host
));
1639 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
, TRUE
);
1640 while (enumerator
->enumerate(enumerator
, &host
))
1642 new->add_virtual_ip(new, TRUE
, host
);
1644 enumerator
->destroy(enumerator
);
1646 charon
->bus
->children_migrate(charon
->bus
, new->get_id(new),
1647 new->get_unique_id(new));
1648 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1649 while (enumerator
->enumerate(enumerator
, &child_sa
))
1651 this->ike_sa
->remove_child_sa(this->ike_sa
, enumerator
);
1652 new->add_child_sa(new, child_sa
);
1654 enumerator
->destroy(enumerator
);
1655 charon
->bus
->set_sa(charon
->bus
, new);
1656 charon
->bus
->children_migrate(charon
->bus
, NULL
, 0);
1657 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1659 if (!new->get_child_count(new))
1660 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1663 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1664 while (enumerator
->enumerate(enumerator
, &task
))
1666 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1668 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
1669 task
->migrate(task
, new);
1670 new->queue_task(new, task
);
1673 enumerator
->destroy(enumerator
);
1676 if (new->initiate(new, NULL
, 0, NULL
, NULL
) != DESTROY_ME
)
1678 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1679 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
1683 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1684 DBG1(DBG_IKE
, "reauthenticating IKE_SA failed");
1686 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1689 METHOD(task_manager_t
, queue_ike_rekey
, void,
1690 private_task_manager_t
*this)
1692 queue_ike_reauth(this);
1695 METHOD(task_manager_t
, queue_ike_delete
, void,
1696 private_task_manager_t
*this)
1698 enumerator_t
*enumerator
;
1699 child_sa_t
*child_sa
;
1701 /* cancel any currently active task to get the DELETE done quickly */
1702 flush_queue(this, TASK_QUEUE_ACTIVE
);
1704 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1705 while (enumerator
->enumerate(enumerator
, &child_sa
))
1707 queue_task(this, (task_t
*)
1708 quick_delete_create(this->ike_sa
, child_sa
->get_protocol(child_sa
),
1709 child_sa
->get_spi(child_sa
, TRUE
), FALSE
, FALSE
));
1711 enumerator
->destroy(enumerator
);
1713 queue_task(this, (task_t
*)isakmp_delete_create(this->ike_sa
, TRUE
));
1716 METHOD(task_manager_t
, queue_mobike
, void,
1717 private_task_manager_t
*this, bool roam
, bool address
)
1719 /* Not supported in IKEv1 */
1722 METHOD(task_manager_t
, queue_child
, void,
1723 private_task_manager_t
*this, child_cfg_t
*cfg
, uint32_t reqid
,
1724 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1728 task
= quick_mode_create(this->ike_sa
, cfg
, tsi
, tsr
);
1729 task
->use_reqid(task
, reqid
);
1731 queue_task(this, &task
->task
);
1735 * Check if two CHILD_SAs have the same traffic selector
1737 static bool have_equal_ts(child_sa_t
*child1
, child_sa_t
*child2
, bool local
)
1739 enumerator_t
*e1
, *e2
;
1740 traffic_selector_t
*ts1
, *ts2
;
1743 e1
= child1
->create_ts_enumerator(child1
, local
);
1744 e2
= child2
->create_ts_enumerator(child2
, local
);
1745 if (e1
->enumerate(e1
, &ts1
) && e2
->enumerate(e2
, &ts2
))
1747 equal
= ts1
->equals(ts1
, ts2
);
1756 * Described in header
1758 bool ikev1_child_sa_is_redundant(ike_sa_t
*ike_sa
, child_sa_t
*child_sa
,
1759 bool (*cmp
)(child_sa_t
*,child_sa_t
*))
1761 enumerator_t
*enumerator
;
1762 child_sa_t
*current
;
1763 bool redundant
= FALSE
;
1765 enumerator
= ike_sa
->create_child_sa_enumerator(ike_sa
);
1766 while (enumerator
->enumerate(enumerator
, ¤t
))
1768 if (current
!= child_sa
&&
1769 current
->get_state(current
) == CHILD_INSTALLED
&&
1770 streq(current
->get_name(current
), child_sa
->get_name(child_sa
)) &&
1771 have_equal_ts(current
, child_sa
, TRUE
) &&
1772 have_equal_ts(current
, child_sa
, FALSE
) &&
1773 (!cmp
|| cmp(child_sa
, current
)))
1775 DBG1(DBG_IKE
, "detected redundant CHILD_SA %s{%d}",
1776 child_sa
->get_name(child_sa
),
1777 child_sa
->get_unique_id(child_sa
));
1782 enumerator
->destroy(enumerator
);
1788 * Compare the rekey times of two CHILD_SAs, a CHILD_SA is redundant if it is
1789 * rekeyed sooner than another.
1791 static bool is_rekeyed_sooner(child_sa_t
*is_redundant
, child_sa_t
*other
)
1793 return other
->get_lifetime(other
, FALSE
) >
1794 is_redundant
->get_lifetime(is_redundant
, FALSE
);
1798 * Get the first traffic selector of a CHILD_SA, local or remote
1800 static traffic_selector_t
* get_first_ts(child_sa_t
*child_sa
, bool local
)
1802 traffic_selector_t
*ts
= NULL
;
1803 enumerator_t
*enumerator
;
1805 enumerator
= child_sa
->create_ts_enumerator(child_sa
, local
);
1806 enumerator
->enumerate(enumerator
, &ts
);
1807 enumerator
->destroy(enumerator
);
1812 METHOD(task_manager_t
, queue_child_rekey
, void,
1813 private_task_manager_t
*this, protocol_id_t protocol
, uint32_t spi
)
1815 child_sa_t
*child_sa
;
1819 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, TRUE
);
1822 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, FALSE
);
1824 if (child_sa
&& child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
1826 if (ikev1_child_sa_is_redundant(this->ike_sa
, child_sa
,
1829 child_sa
->set_state(child_sa
, CHILD_REKEYED
);
1830 if (lib
->settings
->get_bool(lib
->settings
, "%s.delete_rekeyed",
1833 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
,
1834 protocol
, spi
, FALSE
, FALSE
));
1839 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
1840 cfg
= child_sa
->get_config(child_sa
);
1841 task
= quick_mode_create(this->ike_sa
, cfg
->get_ref(cfg
),
1842 get_first_ts(child_sa
, TRUE
), get_first_ts(child_sa
, FALSE
));
1843 task
->use_reqid(task
, child_sa
->get_reqid(child_sa
));
1844 task
->use_marks(task
, child_sa
->get_mark(child_sa
, TRUE
).value
,
1845 child_sa
->get_mark(child_sa
, FALSE
).value
);
1846 task
->rekey(task
, child_sa
->get_spi(child_sa
, TRUE
));
1848 queue_task(this, &task
->task
);
1853 METHOD(task_manager_t
, queue_child_delete
, void,
1854 private_task_manager_t
*this, protocol_id_t protocol
, uint32_t spi
,
1857 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
, protocol
,
1858 spi
, FALSE
, expired
));
1861 METHOD(task_manager_t
, queue_dpd
, void,
1862 private_task_manager_t
*this)
1864 peer_cfg_t
*peer_cfg
;
1865 uint32_t t
, retransmit
;
1867 queue_task(this, (task_t
*)isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE
,
1869 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1871 /* compute timeout in milliseconds */
1872 t
= 1000 * peer_cfg
->get_dpd_timeout(peer_cfg
);
1875 /* use the same timeout as a retransmitting IKE message would have */
1876 for (retransmit
= 0; retransmit
<= this->retransmit_tries
; retransmit
++)
1878 t
+= (uint32_t)(this->retransmit_timeout
* 1000.0 *
1879 pow(this->retransmit_base
, retransmit
));
1882 /* compensate for the already elapsed dpd delay */
1883 t
-= 1000 * peer_cfg
->get_dpd(peer_cfg
);
1885 /* schedule DPD timeout job */
1886 lib
->scheduler
->schedule_job_ms(lib
->scheduler
,
1887 (job_t
*)dpd_timeout_job_create(this->ike_sa
->get_id(this->ike_sa
)), t
);
1890 METHOD(task_manager_t
, adopt_tasks
, void,
1891 private_task_manager_t
*this, task_manager_t
*other_public
)
1893 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1896 /* move queued tasks from other to this */
1897 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1898 (void**)&task
) == SUCCESS
)
1900 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1901 task
->migrate(task
, this->ike_sa
);
1902 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1906 METHOD(task_manager_t
, busy
, bool,
1907 private_task_manager_t
*this)
1909 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1912 METHOD(task_manager_t
, incr_mid
, void,
1913 private_task_manager_t
*this, bool initiate
)
1917 METHOD(task_manager_t
, get_mid
, uint32_t,
1918 private_task_manager_t
*this, bool initiate
)
1920 return initiate
? this->initiating
.mid
: this->responding
.mid
;
1923 METHOD(task_manager_t
, reset
, void,
1924 private_task_manager_t
*this, uint32_t initiate
, uint32_t respond
)
1926 enumerator_t
*enumerator
;
1929 /* reset message counters and retransmit packets */
1930 clear_packets(this->responding
.packets
);
1931 clear_packets(this->initiating
.packets
);
1932 this->responding
.seqnr
= RESPONDING_SEQ
;
1933 this->responding
.retransmitted
= 0;
1934 this->initiating
.mid
= 0;
1935 this->initiating
.seqnr
= 0;
1936 this->initiating
.retransmitted
= 0;
1937 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1938 DESTROY_IF(this->defrag
);
1939 this->defrag
= NULL
;
1940 if (initiate
!= UINT_MAX
)
1942 this->dpd_send
= initiate
;
1944 if (respond
!= UINT_MAX
)
1946 this->dpd_recv
= respond
;
1949 /* reset queued tasks */
1950 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1951 while (enumerator
->enumerate(enumerator
, &task
))
1953 task
->migrate(task
, this->ike_sa
);
1955 enumerator
->destroy(enumerator
);
1957 /* reset active tasks */
1958 while (this->active_tasks
->remove_last(this->active_tasks
,
1959 (void**)&task
) == SUCCESS
)
1961 task
->migrate(task
, this->ike_sa
);
1962 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1967 * Data for a task queue enumerator
1970 enumerator_t
public;
1972 enumerator_t
*inner
;
1973 } task_enumerator_t
;
1975 METHOD(enumerator_t
, task_enumerator_destroy
, void,
1976 task_enumerator_t
*this)
1978 this->inner
->destroy(this->inner
);
1982 METHOD(enumerator_t
, task_enumerator_enumerate
, bool,
1983 task_enumerator_t
*this, va_list args
)
1987 VA_ARGS_VGET(args
, task
);
1988 return this->inner
->enumerate(this->inner
, task
);
1991 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1992 private_task_manager_t
*this, task_queue_t queue
)
1994 task_enumerator_t
*enumerator
;
1998 .enumerate
= enumerator_enumerate_default
,
1999 .venumerate
= _task_enumerator_enumerate
,
2000 .destroy
= _task_enumerator_destroy
,
2006 case TASK_QUEUE_ACTIVE
:
2007 enumerator
->inner
= this->active_tasks
->create_enumerator(
2008 this->active_tasks
);
2010 case TASK_QUEUE_PASSIVE
:
2011 enumerator
->inner
= this->passive_tasks
->create_enumerator(
2012 this->passive_tasks
);
2014 case TASK_QUEUE_QUEUED
:
2015 enumerator
->inner
= this->queued_tasks
->create_enumerator(
2016 this->queued_tasks
);
2019 enumerator
->inner
= enumerator_create_empty();
2022 return &enumerator
->public;
2025 METHOD(task_manager_t
, remove_task
, void,
2026 private_task_manager_t
*this, enumerator_t
*enumerator_public
)
2028 task_enumerator_t
*enumerator
= (task_enumerator_t
*)enumerator_public
;
2030 switch (enumerator
->queue
)
2032 case TASK_QUEUE_ACTIVE
:
2033 this->active_tasks
->remove_at(this->active_tasks
,
2036 case TASK_QUEUE_PASSIVE
:
2037 this->passive_tasks
->remove_at(this->passive_tasks
,
2040 case TASK_QUEUE_QUEUED
:
2041 this->queued_tasks
->remove_at(this->queued_tasks
,
2049 METHOD(task_manager_t
, destroy
, void,
2050 private_task_manager_t
*this)
2054 this->active_tasks
->destroy(this->active_tasks
);
2055 this->queued_tasks
->destroy(this->queued_tasks
);
2056 this->passive_tasks
->destroy(this->passive_tasks
);
2057 DESTROY_IF(this->defrag
);
2059 DESTROY_IF(this->queued
);
2060 clear_packets(this->responding
.packets
);
2061 array_destroy(this->responding
.packets
);
2062 clear_packets(this->initiating
.packets
);
2063 array_destroy(this->initiating
.packets
);
2064 DESTROY_IF(this->rng
);
2071 task_manager_v1_t
*task_manager_v1_create(ike_sa_t
*ike_sa
)
2073 private_task_manager_t
*this;
2078 .process_message
= _process_message
,
2079 .queue_task
= _queue_task
,
2080 .queue_task_delayed
= _queue_task_delayed
,
2081 .queue_ike
= _queue_ike
,
2082 .queue_ike_rekey
= _queue_ike_rekey
,
2083 .queue_ike_reauth
= _queue_ike_reauth
,
2084 .queue_ike_delete
= _queue_ike_delete
,
2085 .queue_mobike
= _queue_mobike
,
2086 .queue_child
= _queue_child
,
2087 .queue_child_rekey
= _queue_child_rekey
,
2088 .queue_child_delete
= _queue_child_delete
,
2089 .queue_dpd
= _queue_dpd
,
2090 .initiate
= _initiate
,
2091 .retransmit
= _retransmit
,
2092 .incr_mid
= _incr_mid
,
2093 .get_mid
= _get_mid
,
2095 .adopt_tasks
= _adopt_tasks
,
2097 .create_task_enumerator
= _create_task_enumerator
,
2098 .remove_task
= _remove_task
,
2100 .flush_queue
= _flush_queue
,
2101 .destroy
= _destroy
,
2105 .type
= EXCHANGE_TYPE_UNDEFINED
,
2108 .seqnr
= RESPONDING_SEQ
,
2111 .rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
),
2112 .queued_tasks
= linked_list_create(),
2113 .active_tasks
= linked_list_create(),
2114 .passive_tasks
= linked_list_create(),
2115 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
2116 "%s.retransmit_tries", RETRANSMIT_TRIES
, lib
->ns
),
2117 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
2118 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, lib
->ns
),
2119 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
2120 "%s.retransmit_base", RETRANSMIT_BASE
, lib
->ns
),
2121 .retransmit_jitter
= min(lib
->settings
->get_int(lib
->settings
,
2122 "%s.retransmit_jitter", 0, lib
->ns
), RETRANSMIT_JITTER_MAX
),
2123 .retransmit_limit
= lib
->settings
->get_int(lib
->settings
,
2124 "%s.retransmit_limit", 0, lib
->ns
) * 1000,
2129 DBG1(DBG_IKE
, "no RNG found, unable to create IKE_SA");
2133 if (!this->rng
->get_bytes(this->rng
, sizeof(this->dpd_send
),
2134 (void*)&this->dpd_send
))
2136 DBG1(DBG_IKE
, "failed to allocate message ID, unable to create IKE_SA");
2140 this->dpd_send
&= 0x7FFFFFFF;
2142 return &this->public;