2 * Copyright (C) 2007-2013 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "task_manager_v1.h"
22 #include <sa/ikev1/tasks/main_mode.h>
23 #include <sa/ikev1/tasks/aggressive_mode.h>
24 #include <sa/ikev1/tasks/quick_mode.h>
25 #include <sa/ikev1/tasks/quick_delete.h>
26 #include <sa/ikev1/tasks/xauth.h>
27 #include <sa/ikev1/tasks/mode_config.h>
28 #include <sa/ikev1/tasks/informational.h>
29 #include <sa/ikev1/tasks/isakmp_natd.h>
30 #include <sa/ikev1/tasks/isakmp_vendor.h>
31 #include <sa/ikev1/tasks/isakmp_cert_pre.h>
32 #include <sa/ikev1/tasks/isakmp_cert_post.h>
33 #include <sa/ikev1/tasks/isakmp_delete.h>
34 #include <sa/ikev1/tasks/isakmp_dpd.h>
36 #include <processing/jobs/retransmit_job.h>
37 #include <processing/jobs/delete_ike_sa_job.h>
38 #include <processing/jobs/dpd_timeout_job.h>
39 #include <processing/jobs/process_message_job.h>
41 #include <encoding/payloads/fragment_payload.h>
42 #include <bio/bio_writer.h>
45 * Number of old messages hashes we keep for retransmission.
47 * In Main Mode, we must ignore messages from a previous message pair if
48 * we already continued to the next. Otherwise a late retransmission
49 * could be considered as a reply to the newer request.
51 #define MAX_OLD_HASHES 2
54 * Maximum packet size for fragmented packets (same as in sockets)
56 #define MAX_PACKET 10000
59 * Maximum size of fragment data when sending packets (currently the same is
60 * used for IPv4 and IPv6, even though the latter has a higher minimum datagram
61 * size). 576 (= min. IPv4) - 20 (= IP header) - 8 (= UDP header) -
62 * - 28 (= IKE header) - 8 (= fragment header) = 512
63 * This is reduced by 4 in case of NAT-T (due to the non-ESP marker).
65 #define MAX_FRAGMENT_SIZE 512
68 * First sequence number of responding packets.
70 * To distinguish retransmission jobs for initiating and responding packets,
71 * we split up the sequence counter and use the upper half for responding.
73 #define RESPONDING_SEQ INT_MAX
75 typedef struct exchange_t exchange_t
;
78 * An exchange in the air, used do detect and handle retransmission
83 * Message ID used for this transaction
88 * generated packet for retransmission
93 typedef struct private_task_manager_t private_task_manager_t
;
96 * private data of the task manager
98 struct private_task_manager_t
{
103 task_manager_v1_t
public;
106 * associated IKE_SA we are serving
111 * RNG to create message IDs
116 * Exchange we are currently handling as responder
120 * Message ID of the last response
125 * Hash of a previously received message
130 * packet for retransmission
135 * Sequence number of the last sent message
140 * how many times we have retransmitted so far
147 * Exchange we are currently handling as initiator
151 * Message ID of the exchange
156 * Hashes of old responses we can ignore
158 u_int32_t old_hashes
[MAX_OLD_HASHES
];
161 * Position in old hash array
166 * Sequence number of the last sent message
171 * how many times we have retransmitted so far
176 * packet for retransmission
181 * type of the initiated exchange
183 exchange_type_t type
;
188 * Data used to reassemble a fragmented message
193 * Fragment ID (currently only one is supported at a time)
198 * The number of the last fragment (in case we receive the fragments out
199 * of order), since the first starts with 1 this defines the number of
200 * fragments we expect
205 * List of fragments (fragment_t*)
210 * Length of all currently received fragments
215 * Maximum length of a fragmented packet
220 * Maximum length of a single fragment (when sending)
225 * The exchange type we use for fragments. Always the initial type even
226 * for fragmented quick mode or transaction messages (i.e. either
227 * ID_PROT or AGGRESSIVE)
229 exchange_type_t exchange
;
234 * List of queued tasks not yet in action
236 linked_list_t
*queued_tasks
;
239 * List of active tasks, initiated by ourselves
241 linked_list_t
*active_tasks
;
244 * List of tasks initiated by peer
246 linked_list_t
*passive_tasks
;
249 * Queued messages not yet ready to process
254 * Number of times we retransmit messages before giving up
256 u_int retransmit_tries
;
259 * Retransmission timeout
261 double retransmit_timeout
;
264 * Base to calculate retransmission timeout
266 double retransmit_base
;
269 * Sequence number for sending DPD requests
274 * Sequence number for received DPD requests
280 * A single fragment within a fragmented message
284 /** fragment number */
292 static void fragment_destroy(fragment_t
*this)
294 chunk_free(&this->data
);
298 static void clear_fragments(private_task_manager_t
*this, u_int16_t id
)
300 DESTROY_FUNCTION_IF(this->frag
.list
, (void*)fragment_destroy
);
301 this->frag
.list
= NULL
;
307 METHOD(task_manager_t
, flush_queue
, void,
308 private_task_manager_t
*this, task_queue_t queue
)
315 this->queued
->destroy(this->queued
);
320 case TASK_QUEUE_ACTIVE
:
321 list
= this->active_tasks
;
322 /* cancel pending retransmits */
323 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
324 DESTROY_IF(this->initiating
.packet
);
325 this->initiating
.packet
= NULL
;
327 case TASK_QUEUE_PASSIVE
:
328 list
= this->passive_tasks
;
330 case TASK_QUEUE_QUEUED
:
331 list
= this->queued_tasks
;
336 while (list
->remove_last(list
, (void**)&task
) == SUCCESS
)
343 * flush all tasks in the task manager
345 static void flush(private_task_manager_t
*this)
347 flush_queue(this, TASK_QUEUE_QUEUED
);
348 flush_queue(this, TASK_QUEUE_PASSIVE
);
349 flush_queue(this, TASK_QUEUE_ACTIVE
);
353 * move a task of a specific type from the queue to the active list
355 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
357 enumerator_t
*enumerator
;
361 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
362 while (enumerator
->enumerate(enumerator
, (void**)&task
))
364 if (task
->get_type(task
) == type
)
366 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
367 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
368 this->active_tasks
->insert_last(this->active_tasks
, task
);
373 enumerator
->destroy(enumerator
);
378 * Send a single fragment with the given data
380 static bool send_fragment(private_task_manager_t
*this, bool request
,
381 host_t
*src
, host_t
*dst
, fragment_payload_t
*fragment
)
387 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
388 /* other implementations seem to just use 0 as message ID, so here we go */
389 message
->set_message_id(message
, 0);
390 message
->set_request(message
, request
);
391 message
->set_source(message
, src
->clone(src
));
392 message
->set_destination(message
, dst
->clone(dst
));
393 message
->set_exchange_type(message
, this->frag
.exchange
);
394 message
->add_payload(message
, (payload_t
*)fragment
);
396 status
= this->ike_sa
->generate_message(this->ike_sa
, message
, &packet
);
397 if (status
!= SUCCESS
)
399 DBG1(DBG_IKE
, "failed to generate IKE fragment");
400 message
->destroy(message
);
403 charon
->sender
->send(charon
->sender
, packet
);
404 message
->destroy(message
);
409 * Send a packet, if supported and required do so in fragments
411 static bool send_packet(private_task_manager_t
*this, bool request
,
414 bool use_frags
= FALSE
;
418 ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
421 switch (ike_cfg
->fragmentation(ike_cfg
))
423 case FRAGMENTATION_FORCE
:
426 case FRAGMENTATION_YES
:
427 use_frags
= this->ike_sa
->supports_extension(this->ike_sa
,
428 EXT_IKE_FRAGMENTATION
);
434 data
= packet
->get_data(packet
);
435 if (data
.len
> this->frag
.size
&& use_frags
)
437 fragment_payload_t
*fragment
;
439 size_t len
, frag_size
;
442 src
= packet
->get_source(packet
);
443 dst
= packet
->get_destination(packet
);
445 frag_size
= this->frag
.size
;
446 if (dst
->get_port(dst
) != IKEV2_UDP_PORT
&&
447 src
->get_port(src
) != IKEV2_UDP_PORT
)
448 { /* reduce size due to non-ESP marker */
451 count
= data
.len
/ frag_size
+ (data
.len
% frag_size
? 1 : 0);
453 DBG1(DBG_IKE
, "sending IKE message with length of %zu bytes in "
454 "%hhu fragments", data
.len
, count
);
455 for (num
= 1; num
<= count
; num
++)
457 len
= min(data
.len
, frag_size
);
458 fragment
= fragment_payload_create_from_data(num
, num
== count
,
459 chunk_create(data
.ptr
, len
));
460 if (!send_fragment(this, request
, src
, dst
, fragment
))
462 packet
->destroy(packet
);
465 data
= chunk_skip(data
, len
);
467 packet
->destroy(packet
);
470 charon
->sender
->send(charon
->sender
, packet
);
475 * Retransmit a packet, either as initiator or as responder
477 static status_t
retransmit_packet(private_task_manager_t
*this, bool request
,
478 u_int32_t seqnr
, u_int mid
, u_int retransmitted
, packet_t
*packet
)
482 if (retransmitted
> this->retransmit_tries
)
484 DBG1(DBG_IKE
, "giving up after %u retransmits", retransmitted
- 1);
485 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
, packet
);
488 t
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
489 pow(this->retransmit_base
, retransmitted
));
492 DBG1(DBG_IKE
, "sending retransmit %u of %s message ID %u, seq %u",
493 retransmitted
, seqnr
< RESPONDING_SEQ
? "request" : "response",
494 mid
, seqnr
< RESPONDING_SEQ
? seqnr
: seqnr
- RESPONDING_SEQ
);
495 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
, packet
);
497 if (!send_packet(this, request
, packet
->clone(packet
)))
501 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
502 retransmit_job_create(seqnr
, this->ike_sa
->get_id(this->ike_sa
)), t
);
506 METHOD(task_manager_t
, retransmit
, status_t
,
507 private_task_manager_t
*this, u_int32_t seqnr
)
509 status_t status
= SUCCESS
;
511 if (seqnr
== this->initiating
.seqnr
&& this->initiating
.packet
)
513 status
= retransmit_packet(this, TRUE
, seqnr
, this->initiating
.mid
,
514 this->initiating
.retransmitted
, this->initiating
.packet
);
515 if (status
== NEED_MORE
)
517 this->initiating
.retransmitted
++;
521 if (seqnr
== this->responding
.seqnr
&& this->responding
.packet
)
523 status
= retransmit_packet(this, FALSE
, seqnr
, this->responding
.mid
,
524 this->responding
.retransmitted
, this->responding
.packet
);
525 if (status
== NEED_MORE
)
527 this->responding
.retransmitted
++;
535 * Check if we have to wait for a mode config before starting a quick mode
537 static bool mode_config_expected(private_task_manager_t
*this)
539 enumerator_t
*enumerator
;
540 peer_cfg_t
*peer_cfg
;
545 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
548 if (peer_cfg
->use_pull_mode(peer_cfg
))
550 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
551 if (!enumerator
->enumerate(enumerator
, &pool
))
552 { /* no pool configured */
553 enumerator
->destroy(enumerator
);
556 enumerator
->destroy(enumerator
);
562 enumerator
= peer_cfg
->create_virtual_ip_enumerator(peer_cfg
);
563 if (!enumerator
->enumerate(enumerator
, &host
))
564 { /* not requesting a vip */
565 enumerator
->destroy(enumerator
);
568 enumerator
->destroy(enumerator
);
572 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
,
574 if (!enumerator
->enumerate(enumerator
, &host
))
575 { /* expecting a VIP exchange, but no VIP assigned yet */
576 enumerator
->destroy(enumerator
);
579 enumerator
->destroy(enumerator
);
584 METHOD(task_manager_t
, initiate
, status_t
,
585 private_task_manager_t
*this)
587 enumerator_t
*enumerator
;
592 exchange_type_t exchange
= EXCHANGE_TYPE_UNDEFINED
;
593 bool new_mid
= FALSE
, expect_response
= FALSE
, cancelled
= FALSE
, keep
= FALSE
;
595 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
&&
596 this->initiating
.type
!= INFORMATIONAL_V1
)
598 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
599 exchange_type_names
, this->initiating
.type
);
600 /* do not initiate if we already have a message in the air */
604 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
606 DBG2(DBG_IKE
, "activating new tasks");
607 switch (this->ike_sa
->get_state(this->ike_sa
))
610 activate_task(this, TASK_ISAKMP_VENDOR
);
611 activate_task(this, TASK_ISAKMP_CERT_PRE
);
612 if (activate_task(this, TASK_MAIN_MODE
))
616 else if (activate_task(this, TASK_AGGRESSIVE_MODE
))
618 exchange
= AGGRESSIVE
;
620 activate_task(this, TASK_ISAKMP_CERT_POST
);
621 activate_task(this, TASK_ISAKMP_NATD
);
624 if (activate_task(this, TASK_ISAKMP_DELETE
))
626 exchange
= INFORMATIONAL_V1
;
630 if (activate_task(this, TASK_XAUTH
))
632 exchange
= TRANSACTION
;
636 if (activate_task(this, TASK_INFORMATIONAL
))
638 exchange
= INFORMATIONAL_V1
;
643 case IKE_ESTABLISHED
:
644 if (activate_task(this, TASK_MODE_CONFIG
))
646 exchange
= TRANSACTION
;
650 if (!mode_config_expected(this) &&
651 activate_task(this, TASK_QUICK_MODE
))
653 exchange
= QUICK_MODE
;
657 if (activate_task(this, TASK_INFORMATIONAL
))
659 exchange
= INFORMATIONAL_V1
;
663 if (activate_task(this, TASK_QUICK_DELETE
))
665 exchange
= INFORMATIONAL_V1
;
669 if (activate_task(this, TASK_ISAKMP_DELETE
))
671 exchange
= INFORMATIONAL_V1
;
675 if (activate_task(this, TASK_ISAKMP_DPD
))
677 exchange
= INFORMATIONAL_V1
;
688 DBG2(DBG_IKE
, "reinitiating already active tasks");
689 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
690 while (enumerator
->enumerate(enumerator
, (void**)&task
))
692 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
693 switch (task
->get_type(task
))
698 case TASK_AGGRESSIVE_MODE
:
699 exchange
= AGGRESSIVE
;
701 case TASK_QUICK_MODE
:
702 exchange
= QUICK_MODE
;
705 exchange
= TRANSACTION
;
713 enumerator
->destroy(enumerator
);
716 if (exchange
== EXCHANGE_TYPE_UNDEFINED
)
718 DBG2(DBG_IKE
, "nothing to initiate");
719 /* nothing to do yet... */
723 me
= this->ike_sa
->get_my_host(this->ike_sa
);
724 other
= this->ike_sa
->get_other_host(this->ike_sa
);
728 if (!this->rng
->get_bytes(this->rng
, sizeof(this->initiating
.mid
),
729 (void*)&this->initiating
.mid
))
731 DBG1(DBG_IKE
, "failed to allocate message ID, destroying IKE_SA");
736 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
737 message
->set_message_id(message
, this->initiating
.mid
);
738 message
->set_source(message
, me
->clone(me
));
739 message
->set_destination(message
, other
->clone(other
));
740 message
->set_exchange_type(message
, exchange
);
741 this->initiating
.type
= exchange
;
742 this->initiating
.retransmitted
= 0;
744 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
745 while (enumerator
->enumerate(enumerator
, (void*)&task
))
747 switch (task
->build(task
, message
))
750 /* task completed, remove it */
751 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
752 if (task
->get_type(task
) == TASK_AGGRESSIVE_MODE
||
753 task
->get_type(task
) == TASK_QUICK_MODE
)
754 { /* last message of three message exchange */
760 expect_response
= TRUE
;
761 /* processed, but task needs another exchange */
768 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
770 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
774 /* critical failure, destroy IKE_SA */
775 enumerator
->destroy(enumerator
);
776 message
->destroy(message
);
782 enumerator
->destroy(enumerator
);
784 if (this->active_tasks
->get_count(this->active_tasks
) == 0 &&
785 (exchange
== QUICK_MODE
|| exchange
== AGGRESSIVE
))
786 { /* tasks completed, no exchange active anymore */
787 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
791 message
->destroy(message
);
792 return initiate(this);
795 DESTROY_IF(this->initiating
.packet
);
796 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
797 &this->initiating
.packet
);
798 if (status
!= SUCCESS
)
800 /* message generation failed. There is nothing more to do than to
802 message
->destroy(message
);
804 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
808 this->initiating
.seqnr
++;
811 message
->destroy(message
);
812 return retransmit(this, this->initiating
.seqnr
);
815 { /* keep the packet for retransmission, the responder might request it */
816 send_packet(this, TRUE
,
817 this->initiating
.packet
->clone(this->initiating
.packet
));
821 send_packet(this, TRUE
, this->initiating
.packet
);
822 this->initiating
.packet
= NULL
;
824 message
->destroy(message
);
826 if (exchange
== INFORMATIONAL_V1
)
828 switch (this->ike_sa
->get_state(this->ike_sa
))
831 /* close after sending an INFORMATIONAL when unestablished */
834 /* close after sending a DELETE */
840 return initiate(this);
844 * build a response depending on the "passive" task list
846 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
848 enumerator_t
*enumerator
;
852 bool delete = FALSE
, cancelled
= FALSE
, expect_request
= FALSE
;
855 me
= request
->get_destination(request
);
856 other
= request
->get_source(request
);
858 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
859 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
860 /* send response along the path the request came in */
861 message
->set_source(message
, me
->clone(me
));
862 message
->set_destination(message
, other
->clone(other
));
863 message
->set_message_id(message
, request
->get_message_id(request
));
864 message
->set_request(message
, FALSE
);
866 this->responding
.mid
= request
->get_message_id(request
);
867 this->responding
.retransmitted
= 0;
868 this->responding
.seqnr
++;
870 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
871 while (enumerator
->enumerate(enumerator
, (void*)&task
))
873 switch (task
->build(task
, message
))
876 /* task completed, remove it */
877 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
881 /* processed, but task needs another exchange */
882 if (task
->get_type(task
) == TASK_QUICK_MODE
||
883 task
->get_type(task
) == TASK_AGGRESSIVE_MODE
)
884 { /* we rely on initiator retransmission, except for
885 * three-message exchanges */
886 expect_request
= TRUE
;
894 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
897 /* destroy IKE_SA, but SEND response first */
903 enumerator
->destroy(enumerator
);
905 DESTROY_IF(this->responding
.packet
);
906 this->responding
.packet
= NULL
;
909 message
->destroy(message
);
910 return initiate(this);
912 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
913 &this->responding
.packet
);
914 message
->destroy(message
);
915 if (status
!= SUCCESS
)
917 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
921 if (expect_request
&& !delete)
923 return retransmit(this, this->responding
.seqnr
);
925 send_packet(this, FALSE
,
926 this->responding
.packet
->clone(this->responding
.packet
));
935 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
936 * The notify protocol_id is set to ISAKMP
938 static void send_notify(private_task_manager_t
*this, message_t
*request
,
946 if (request
->get_exchange_type(request
) == INFORMATIONAL_V1
)
947 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
948 DBG1(DBG_IKE
, "ignore malformed INFORMATIONAL request");
951 if (!this->rng
->get_bytes(this->rng
, sizeof(mid
), (void*)&mid
))
953 DBG1(DBG_IKE
, "failed to allocate message ID");
956 response
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
957 response
->set_exchange_type(response
, INFORMATIONAL_V1
);
958 response
->set_request(response
, TRUE
);
959 response
->set_message_id(response
, mid
);
960 response
->add_payload(response
, (payload_t
*)
961 notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
964 me
= this->ike_sa
->get_my_host(this->ike_sa
);
965 if (me
->is_anyaddr(me
))
967 me
= request
->get_destination(request
);
968 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
970 other
= this->ike_sa
->get_other_host(this->ike_sa
);
971 if (other
->is_anyaddr(other
))
973 other
= request
->get_source(request
);
974 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
976 response
->set_source(response
, me
->clone(me
));
977 response
->set_destination(response
, other
->clone(other
));
978 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
981 send_packet(this, TRUE
, packet
);
983 response
->destroy(response
);
987 * Process a DPD request/response
989 static bool process_dpd(private_task_manager_t
*this, message_t
*message
)
991 notify_payload_t
*notify
;
996 type
= DPD_R_U_THERE
;
997 notify
= message
->get_notify(message
, type
);
1000 type
= DPD_R_U_THERE_ACK
;
1001 notify
= message
->get_notify(message
, type
);
1007 data
= notify
->get_notification_data(notify
);
1012 seq
= untoh32(data
.ptr
);
1014 if (type
== DPD_R_U_THERE
)
1016 if (this->dpd_recv
== 0 || seq
== this->dpd_recv
)
1017 { /* check sequence validity */
1018 this->dpd_recv
= seq
+ 1;
1019 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1020 time_monotonic(NULL
));
1022 /* but respond anyway */
1023 this->ike_sa
->queue_task(this->ike_sa
,
1024 &isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE_ACK
, seq
)->task
);
1026 else /* DPD_R_U_THERE_ACK */
1028 if (seq
== this->dpd_send
- 1)
1030 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1031 time_monotonic(NULL
));
1035 DBG1(DBG_IKE
, "received invalid DPD sequence number %u "
1036 "(expected %u), ignored", seq
, this->dpd_send
- 1);
1043 * handle an incoming request message
1045 static status_t
process_request(private_task_manager_t
*this,
1048 enumerator_t
*enumerator
;
1049 task_t
*task
= NULL
;
1050 bool send_response
= FALSE
, dpd
= FALSE
;
1052 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
1053 this->passive_tasks
->get_count(this->passive_tasks
) == 0)
1054 { /* create tasks depending on request type, if not already some queued */
1055 switch (message
->get_exchange_type(message
))
1058 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
1059 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1060 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
1061 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1062 task
= (task_t
*)main_mode_create(this->ike_sa
, FALSE
);
1063 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1064 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
1065 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1066 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1067 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1070 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
1071 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1072 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
1073 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1074 task
= (task_t
*)aggressive_mode_create(this->ike_sa
, FALSE
);
1075 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1076 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
1077 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1078 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1079 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1080 this->frag
.exchange
= AGGRESSIVE
;
1083 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1085 DBG1(DBG_IKE
, "received quick mode request for "
1086 "unestablished IKE_SA, ignored");
1089 task
= (task_t
*)quick_mode_create(this->ike_sa
, NULL
,
1091 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1093 case INFORMATIONAL_V1
:
1094 if (process_dpd(this, message
))
1100 task
= (task_t
*)informational_create(this->ike_sa
, NULL
);
1101 this->passive_tasks
->insert_first(this->passive_tasks
, task
);
1105 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
1107 task
= (task_t
*)mode_config_create(this->ike_sa
,
1112 task
= (task_t
*)xauth_create(this->ike_sa
, FALSE
);
1114 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1122 return initiate(this);
1124 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
, time_monotonic(NULL
));
1126 /* let the tasks process the message */
1127 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
1128 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1130 switch (task
->process(task
, message
))
1133 /* task completed, remove it */
1134 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1135 task
->destroy(task
);
1138 /* processed, but task needs at least another call to build() */
1139 send_response
= TRUE
;
1142 send_response
= FALSE
;
1146 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1149 /* critical failure, destroy IKE_SA */
1150 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1151 enumerator
->destroy(enumerator
);
1152 task
->destroy(task
);
1157 enumerator
->destroy(enumerator
);
1161 if (build_response(this, message
) != SUCCESS
)
1167 { /* We don't send a response, so don't retransmit one if we get
1168 * the same message again. */
1169 DESTROY_IF(this->responding
.packet
);
1170 this->responding
.packet
= NULL
;
1172 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0 &&
1173 this->queued_tasks
->get_count(this->queued_tasks
) > 0)
1175 /* passive tasks completed, check if an active task has been queued,
1176 * such as XAUTH or modeconfig push */
1177 return initiate(this);
1183 * handle an incoming response message
1185 static status_t
process_response(private_task_manager_t
*this,
1188 enumerator_t
*enumerator
;
1193 if (message
->get_exchange_type(message
) != this->initiating
.type
)
1195 /* Windows server sends a fourth quick mode message having an initial
1196 * contact notify. Ignore this message for compatibility. */
1197 if (this->initiating
.type
== EXCHANGE_TYPE_UNDEFINED
&&
1198 message
->get_exchange_type(message
) == QUICK_MODE
&&
1199 message
->get_notify(message
, INITIAL_CONTACT
))
1201 DBG1(DBG_IKE
, "ignoring fourth Quick Mode message");
1204 DBG1(DBG_IKE
, "received %N response, but expected %N",
1205 exchange_type_names
, message
->get_exchange_type(message
),
1206 exchange_type_names
, this->initiating
.type
);
1207 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1211 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
1212 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1214 switch (task
->process(task
, message
))
1217 /* task completed, remove it */
1218 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1219 task
->destroy(task
);
1222 /* processed, but task needs another exchange */
1228 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1231 /* critical failure, destroy IKE_SA */
1232 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1233 enumerator
->destroy(enumerator
);
1234 task
->destroy(task
);
1239 enumerator
->destroy(enumerator
);
1241 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1242 DESTROY_IF(this->initiating
.packet
);
1243 this->initiating
.packet
= NULL
;
1245 if (this->queued
&& this->active_tasks
->get_count(this->active_tasks
) == 0)
1247 queued
= this->queued
;
1248 this->queued
= NULL
;
1249 status
= this->public.task_manager
.process_message(
1250 &this->public.task_manager
, queued
);
1251 queued
->destroy(queued
);
1252 if (status
== DESTROY_ME
)
1258 return initiate(this);
1261 static status_t
handle_fragment(private_task_manager_t
*this, message_t
*msg
)
1263 fragment_payload_t
*payload
;
1264 enumerator_t
*enumerator
;
1265 fragment_t
*fragment
;
1266 status_t status
= SUCCESS
;
1270 payload
= (fragment_payload_t
*)msg
->get_payload(msg
, FRAGMENT_V1
);
1276 if (!this->frag
.list
|| this->frag
.id
!= payload
->get_id(payload
))
1278 clear_fragments(this, payload
->get_id(payload
));
1279 this->frag
.list
= linked_list_create();
1282 num
= payload
->get_number(payload
);
1283 if (!this->frag
.last
&& payload
->is_last(payload
))
1285 this->frag
.last
= num
;
1288 enumerator
= this->frag
.list
->create_enumerator(this->frag
.list
);
1289 while (enumerator
->enumerate(enumerator
, &fragment
))
1291 if (fragment
->num
== num
)
1292 { /* ignore a duplicate fragment */
1293 DBG1(DBG_IKE
, "received duplicate fragment #%hhu", num
);
1294 enumerator
->destroy(enumerator
);
1297 if (fragment
->num
> num
)
1303 data
= payload
->get_data(payload
);
1304 this->frag
.len
+= data
.len
;
1305 if (this->frag
.len
> this->frag
.max_packet
)
1307 DBG1(DBG_IKE
, "fragmented IKE message is too large");
1308 enumerator
->destroy(enumerator
);
1309 clear_fragments(this, 0);
1315 .data
= chunk_clone(data
),
1318 this->frag
.list
->insert_before(this->frag
.list
, enumerator
, fragment
);
1319 enumerator
->destroy(enumerator
);
1321 if (this->frag
.list
->get_count(this->frag
.list
) == this->frag
.last
)
1326 bio_writer_t
*writer
;
1328 writer
= bio_writer_create(this->frag
.len
);
1329 DBG1(DBG_IKE
, "received fragment #%hhu, reassembling fragmented IKE "
1331 enumerator
= this->frag
.list
->create_enumerator(this->frag
.list
);
1332 while (enumerator
->enumerate(enumerator
, &fragment
))
1334 writer
->write_data(writer
, fragment
->data
);
1336 enumerator
->destroy(enumerator
);
1338 src
= msg
->get_source(msg
);
1339 dst
= msg
->get_destination(msg
);
1340 pkt
= packet_create_from_data(src
->clone(src
), dst
->clone(dst
),
1341 writer
->extract_buf(writer
));
1342 writer
->destroy(writer
);
1344 message
= message_create_from_packet(pkt
);
1345 if (message
->parse_header(message
) != SUCCESS
)
1347 DBG1(DBG_IKE
, "failed to parse header of reassembled IKE message");
1348 message
->destroy(message
);
1353 lib
->processor
->queue_job(lib
->processor
,
1354 (job_t
*)process_message_job_create(message
));
1358 clear_fragments(this, 0);
1361 { /* there are some fragments missing */
1362 DBG1(DBG_IKE
, "received fragment #%hhu, waiting for complete IKE "
1370 * Parse the given message and verify that it is valid.
1372 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
1376 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
1378 if (status
!= SUCCESS
)
1383 DBG1(DBG_IKE
, "unsupported exchange type");
1384 send_notify(this, msg
, INVALID_EXCHANGE_TYPE
);
1387 DBG1(DBG_IKE
, "message parsing failed");
1388 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1391 DBG1(DBG_IKE
, "message verification failed");
1392 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1395 DBG1(DBG_IKE
, "integrity check failed");
1396 send_notify(this, msg
, INVALID_HASH_INFORMATION
);
1399 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1400 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1404 DBG1(DBG_IKE
, "%N %s with message ID %u processing failed",
1405 exchange_type_names
, msg
->get_exchange_type(msg
),
1406 msg
->get_request(msg
) ? "request" : "response",
1407 msg
->get_message_id(msg
));
1409 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
, status
);
1411 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1412 { /* invalid initiation attempt, close SA */
1417 if (msg
->get_first_payload_type(msg
) == FRAGMENT_V1
)
1419 return handle_fragment(this, msg
);
1424 METHOD(task_manager_t
, process_message
, status_t
,
1425 private_task_manager_t
*this, message_t
*msg
)
1427 u_int32_t hash
, mid
, i
;
1431 /* TODO-IKEv1: update hosts more selectively */
1432 me
= msg
->get_destination(msg
);
1433 other
= msg
->get_source(msg
);
1434 mid
= msg
->get_message_id(msg
);
1435 hash
= chunk_hash(msg
->get_packet_data(msg
));
1436 for (i
= 0; i
< MAX_OLD_HASHES
; i
++)
1438 if (this->initiating
.old_hashes
[i
] == hash
)
1440 if (this->initiating
.packet
&&
1441 i
== (this->initiating
.old_hash_pos
% MAX_OLD_HASHES
) &&
1442 (msg
->get_exchange_type(msg
) == QUICK_MODE
||
1443 msg
->get_exchange_type(msg
) == AGGRESSIVE
))
1445 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1446 "resending last request", mid
);
1447 send_packet(this, TRUE
,
1448 this->initiating
.packet
->clone(this->initiating
.packet
));
1451 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1452 "but next request already sent", mid
);
1457 if ((mid
&& mid
== this->initiating
.mid
) ||
1458 (this->initiating
.mid
== 0 &&
1459 msg
->get_exchange_type(msg
) == this->initiating
.type
&&
1460 this->active_tasks
->get_count(this->active_tasks
)))
1462 msg
->set_request(msg
, FALSE
);
1463 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1464 status
= parse_message(this, msg
);
1465 if (status
== NEED_MORE
)
1469 if (status
!= SUCCESS
)
1473 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1474 time_monotonic(NULL
));
1475 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1476 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1477 if (process_response(this, msg
) != SUCCESS
)
1482 this->initiating
.old_hashes
[(++this->initiating
.old_hash_pos
) %
1483 MAX_OLD_HASHES
] = hash
;
1487 if (hash
== this->responding
.hash
)
1489 if (this->responding
.packet
)
1491 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1492 "retransmitting response", mid
);
1493 send_packet(this, FALSE
,
1494 this->responding
.packet
->clone(this->responding
.packet
));
1496 else if (this->initiating
.packet
&&
1497 this->initiating
.type
== INFORMATIONAL_V1
)
1499 DBG1(DBG_IKE
, "received retransmit of DPD request, "
1500 "retransmitting response");
1501 send_packet(this, TRUE
,
1502 this->initiating
.packet
->clone(this->initiating
.packet
));
1506 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1507 "but no response to retransmit", mid
);
1509 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1513 /* reject Main/Aggressive Modes once established */
1514 if (msg
->get_exchange_type(msg
) == ID_PROT
||
1515 msg
->get_exchange_type(msg
) == AGGRESSIVE
)
1517 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CREATED
&&
1518 this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
&&
1519 msg
->get_first_payload_type(msg
) != FRAGMENT_V1
)
1521 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1522 exchange_type_names
, msg
->get_exchange_type(msg
));
1527 if (msg
->get_exchange_type(msg
) == TRANSACTION
&&
1528 this->active_tasks
->get_count(this->active_tasks
))
1529 { /* main mode not yet complete, queue XAuth/Mode config tasks */
1532 DBG1(DBG_IKE
, "ignoring additional %N request, queue full",
1533 exchange_type_names
, TRANSACTION
);
1536 this->queued
= message_create_from_packet(msg
->get_packet(msg
));
1537 if (this->queued
->parse_header(this->queued
) != SUCCESS
)
1539 this->queued
->destroy(this->queued
);
1540 this->queued
= NULL
;
1543 DBG1(DBG_IKE
, "queueing %N request as tasks still active",
1544 exchange_type_names
, TRANSACTION
);
1548 msg
->set_request(msg
, TRUE
);
1549 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1550 status
= parse_message(this, msg
);
1551 if (status
== NEED_MORE
)
1555 if (status
!= SUCCESS
)
1559 /* if this IKE_SA is virgin, we check for a config */
1560 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1562 ike_sa_id_t
*ike_sa_id
;
1566 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1568 if (ike_cfg
== NULL
)
1570 /* no config found for these hosts, destroy */
1571 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1572 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1573 send_notify(this, msg
, NO_PROPOSAL_CHOSEN
);
1576 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1577 ike_cfg
->destroy(ike_cfg
);
1578 /* add a timeout if peer does not establish it completely */
1579 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1580 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1581 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1582 lib
->settings
->get_int(lib
->settings
,
1583 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1586 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1587 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1588 if (process_request(this, msg
) != SUCCESS
)
1593 this->responding
.hash
= hash
;
1598 METHOD(task_manager_t
, queue_task
, void,
1599 private_task_manager_t
*this, task_t
*task
)
1601 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1602 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1606 * Check if a given task has been queued already
1608 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1610 enumerator_t
*enumerator
;
1614 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1615 while (enumerator
->enumerate(enumerator
, &task
))
1617 if (task
->get_type(task
) == type
)
1623 enumerator
->destroy(enumerator
);
1627 METHOD(task_manager_t
, queue_ike
, void,
1628 private_task_manager_t
*this)
1630 peer_cfg_t
*peer_cfg
;
1632 if (!has_queued(this, TASK_ISAKMP_VENDOR
))
1634 queue_task(this, (task_t
*)isakmp_vendor_create(this->ike_sa
, TRUE
));
1636 if (!has_queued(this, TASK_ISAKMP_CERT_PRE
))
1638 queue_task(this, (task_t
*)isakmp_cert_pre_create(this->ike_sa
, TRUE
));
1640 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1641 if (peer_cfg
->use_aggressive(peer_cfg
))
1643 if (!has_queued(this, TASK_AGGRESSIVE_MODE
))
1645 queue_task(this, (task_t
*)aggressive_mode_create(this->ike_sa
, TRUE
));
1647 this->frag
.exchange
= AGGRESSIVE
;
1651 if (!has_queued(this, TASK_MAIN_MODE
))
1653 queue_task(this, (task_t
*)main_mode_create(this->ike_sa
, TRUE
));
1656 if (!has_queued(this, TASK_ISAKMP_CERT_POST
))
1658 queue_task(this, (task_t
*)isakmp_cert_post_create(this->ike_sa
, TRUE
));
1660 if (!has_queued(this, TASK_ISAKMP_NATD
))
1662 queue_task(this, (task_t
*)isakmp_natd_create(this->ike_sa
, TRUE
));
1666 METHOD(task_manager_t
, queue_ike_reauth
, void,
1667 private_task_manager_t
*this)
1669 enumerator_t
*enumerator
;
1670 child_sa_t
*child_sa
;
1674 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1675 this->ike_sa
->get_version(this->ike_sa
), TRUE
);
1677 { /* shouldn't happen */
1681 new->set_peer_cfg(new, this->ike_sa
->get_peer_cfg(this->ike_sa
));
1682 host
= this->ike_sa
->get_other_host(this->ike_sa
);
1683 new->set_other_host(new, host
->clone(host
));
1684 host
= this->ike_sa
->get_my_host(this->ike_sa
);
1685 new->set_my_host(new, host
->clone(host
));
1686 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
, TRUE
);
1687 while (enumerator
->enumerate(enumerator
, &host
))
1689 new->add_virtual_ip(new, TRUE
, host
);
1691 enumerator
->destroy(enumerator
);
1693 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1694 while (enumerator
->enumerate(enumerator
, &child_sa
))
1696 this->ike_sa
->remove_child_sa(this->ike_sa
, enumerator
);
1697 new->add_child_sa(new, child_sa
);
1699 enumerator
->destroy(enumerator
);
1701 if (!new->get_child_count(new))
1702 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1705 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1706 while (enumerator
->enumerate(enumerator
, &task
))
1708 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1710 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
1711 task
->migrate(task
, new);
1712 new->queue_task(new, task
);
1715 enumerator
->destroy(enumerator
);
1718 if (new->initiate(new, NULL
, 0, NULL
, NULL
) != DESTROY_ME
)
1720 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1721 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
1725 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1726 DBG1(DBG_IKE
, "reauthenticating IKE_SA failed");
1728 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1731 METHOD(task_manager_t
, queue_ike_rekey
, void,
1732 private_task_manager_t
*this)
1734 queue_ike_reauth(this);
1737 METHOD(task_manager_t
, queue_ike_delete
, void,
1738 private_task_manager_t
*this)
1740 enumerator_t
*enumerator
;
1741 child_sa_t
*child_sa
;
1743 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1744 while (enumerator
->enumerate(enumerator
, &child_sa
))
1746 queue_task(this, (task_t
*)
1747 quick_delete_create(this->ike_sa
, child_sa
->get_protocol(child_sa
),
1748 child_sa
->get_spi(child_sa
, TRUE
), FALSE
, FALSE
));
1750 enumerator
->destroy(enumerator
);
1752 queue_task(this, (task_t
*)isakmp_delete_create(this->ike_sa
, TRUE
));
1755 METHOD(task_manager_t
, queue_mobike
, void,
1756 private_task_manager_t
*this, bool roam
, bool address
)
1758 /* Not supported in IKEv1 */
1761 METHOD(task_manager_t
, queue_child
, void,
1762 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1763 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1767 task
= quick_mode_create(this->ike_sa
, cfg
, tsi
, tsr
);
1768 task
->use_reqid(task
, reqid
);
1770 queue_task(this, &task
->task
);
1774 * Check if two CHILD_SAs have the same traffic selector
1776 static bool have_equal_ts(child_sa_t
*child1
, child_sa_t
*child2
, bool local
)
1778 enumerator_t
*e1
, *e2
;
1779 traffic_selector_t
*ts1
, *ts2
;
1782 e1
= child1
->create_ts_enumerator(child1
, local
);
1783 e2
= child2
->create_ts_enumerator(child2
, local
);
1784 if (e1
->enumerate(e1
, &ts1
) && e2
->enumerate(e2
, &ts2
))
1786 equal
= ts1
->equals(ts1
, ts2
);
1795 * Check if a CHILD_SA is redundant and we should delete instead of rekey
1797 static bool is_redundant(private_task_manager_t
*this, child_sa_t
*child_sa
)
1799 enumerator_t
*enumerator
;
1800 child_sa_t
*current
;
1801 bool redundant
= FALSE
;
1803 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1804 while (enumerator
->enumerate(enumerator
, ¤t
))
1806 if (current
->get_state(current
) == CHILD_INSTALLED
&&
1807 streq(current
->get_name(current
), child_sa
->get_name(child_sa
)) &&
1808 have_equal_ts(current
, child_sa
, TRUE
) &&
1809 have_equal_ts(current
, child_sa
, FALSE
) &&
1810 current
->get_lifetime(current
, FALSE
) >
1811 child_sa
->get_lifetime(child_sa
, FALSE
))
1813 DBG1(DBG_IKE
, "deleting redundant CHILD_SA %s{%d}",
1814 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
));
1819 enumerator
->destroy(enumerator
);
1825 * Get the first traffic selector of a CHILD_SA, local or remote
1827 static traffic_selector_t
* get_first_ts(child_sa_t
*child_sa
, bool local
)
1829 traffic_selector_t
*ts
= NULL
;
1830 enumerator_t
*enumerator
;
1832 enumerator
= child_sa
->create_ts_enumerator(child_sa
, local
);
1833 enumerator
->enumerate(enumerator
, &ts
);
1834 enumerator
->destroy(enumerator
);
1839 METHOD(task_manager_t
, queue_child_rekey
, void,
1840 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1842 child_sa_t
*child_sa
;
1846 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, TRUE
);
1849 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, FALSE
);
1851 if (child_sa
&& child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
1853 if (is_redundant(this, child_sa
))
1855 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
,
1856 protocol
, spi
, FALSE
, FALSE
));
1860 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
1861 cfg
= child_sa
->get_config(child_sa
);
1862 task
= quick_mode_create(this->ike_sa
, cfg
->get_ref(cfg
),
1863 get_first_ts(child_sa
, TRUE
), get_first_ts(child_sa
, FALSE
));
1864 task
->use_reqid(task
, child_sa
->get_reqid(child_sa
));
1865 task
->rekey(task
, child_sa
->get_spi(child_sa
, TRUE
));
1867 queue_task(this, &task
->task
);
1872 METHOD(task_manager_t
, queue_child_delete
, void,
1873 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
,
1876 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
, protocol
,
1877 spi
, FALSE
, expired
));
1880 METHOD(task_manager_t
, queue_dpd
, void,
1881 private_task_manager_t
*this)
1883 peer_cfg_t
*peer_cfg
;
1884 u_int32_t t
, retransmit
;
1886 queue_task(this, (task_t
*)isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE
,
1888 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1890 /* compute timeout in milliseconds */
1891 t
= 1000 * peer_cfg
->get_dpd_timeout(peer_cfg
);
1894 /* use the same timeout as a retransmitting IKE message would have */
1895 for (retransmit
= 0; retransmit
<= this->retransmit_tries
; retransmit
++)
1897 t
+= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
1898 pow(this->retransmit_base
, retransmit
));
1902 /* schedule DPD timeout job */
1903 lib
->scheduler
->schedule_job_ms(lib
->scheduler
,
1904 (job_t
*)dpd_timeout_job_create(this->ike_sa
->get_id(this->ike_sa
)), t
);
1907 METHOD(task_manager_t
, adopt_tasks
, void,
1908 private_task_manager_t
*this, task_manager_t
*other_public
)
1910 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1913 /* move queued tasks from other to this */
1914 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1915 (void**)&task
) == SUCCESS
)
1917 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1918 task
->migrate(task
, this->ike_sa
);
1919 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1924 * Migrates child-creating tasks from src to dst
1926 static void migrate_child_tasks(private_task_manager_t
*this,
1927 linked_list_t
*src
, linked_list_t
*dst
)
1929 enumerator_t
*enumerator
;
1932 enumerator
= src
->create_enumerator(src
);
1933 while (enumerator
->enumerate(enumerator
, &task
))
1935 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1937 src
->remove_at(src
, enumerator
);
1938 task
->migrate(task
, this->ike_sa
);
1939 dst
->insert_last(dst
, task
);
1942 enumerator
->destroy(enumerator
);
1945 METHOD(task_manager_t
, adopt_child_tasks
, void,
1946 private_task_manager_t
*this, task_manager_t
*other_public
)
1948 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1950 /* move active child tasks from other to this */
1951 migrate_child_tasks(this, other
->active_tasks
, this->queued_tasks
);
1952 /* do the same for queued tasks */
1953 migrate_child_tasks(this, other
->queued_tasks
, this->queued_tasks
);
1956 METHOD(task_manager_t
, busy
, bool,
1957 private_task_manager_t
*this)
1959 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1962 METHOD(task_manager_t
, incr_mid
, void,
1963 private_task_manager_t
*this, bool initiate
)
1967 METHOD(task_manager_t
, reset
, void,
1968 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1970 enumerator_t
*enumerator
;
1973 /* reset message counters and retransmit packets */
1974 DESTROY_IF(this->responding
.packet
);
1975 DESTROY_IF(this->initiating
.packet
);
1976 this->responding
.packet
= NULL
;
1977 this->responding
.seqnr
= RESPONDING_SEQ
;
1978 this->responding
.retransmitted
= 0;
1979 this->initiating
.packet
= NULL
;
1980 this->initiating
.mid
= 0;
1981 this->initiating
.seqnr
= 0;
1982 this->initiating
.retransmitted
= 0;
1983 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1984 clear_fragments(this, 0);
1985 if (initiate
!= UINT_MAX
)
1987 this->dpd_send
= initiate
;
1989 if (respond
!= UINT_MAX
)
1991 this->dpd_recv
= respond
;
1994 /* reset queued tasks */
1995 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1996 while (enumerator
->enumerate(enumerator
, &task
))
1998 task
->migrate(task
, this->ike_sa
);
2000 enumerator
->destroy(enumerator
);
2002 /* reset active tasks */
2003 while (this->active_tasks
->remove_last(this->active_tasks
,
2004 (void**)&task
) == SUCCESS
)
2006 task
->migrate(task
, this->ike_sa
);
2007 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
2011 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
2012 private_task_manager_t
*this, task_queue_t queue
)
2016 case TASK_QUEUE_ACTIVE
:
2017 return this->active_tasks
->create_enumerator(this->active_tasks
);
2018 case TASK_QUEUE_PASSIVE
:
2019 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
2020 case TASK_QUEUE_QUEUED
:
2021 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
2023 return enumerator_create_empty();
2027 METHOD(task_manager_t
, destroy
, void,
2028 private_task_manager_t
*this)
2032 this->active_tasks
->destroy(this->active_tasks
);
2033 this->queued_tasks
->destroy(this->queued_tasks
);
2034 this->passive_tasks
->destroy(this->passive_tasks
);
2035 clear_fragments(this, 0);
2037 DESTROY_IF(this->queued
);
2038 DESTROY_IF(this->responding
.packet
);
2039 DESTROY_IF(this->initiating
.packet
);
2040 DESTROY_IF(this->rng
);
2047 task_manager_v1_t
*task_manager_v1_create(ike_sa_t
*ike_sa
)
2049 private_task_manager_t
*this;
2054 .process_message
= _process_message
,
2055 .queue_task
= _queue_task
,
2056 .queue_ike
= _queue_ike
,
2057 .queue_ike_rekey
= _queue_ike_rekey
,
2058 .queue_ike_reauth
= _queue_ike_reauth
,
2059 .queue_ike_delete
= _queue_ike_delete
,
2060 .queue_mobike
= _queue_mobike
,
2061 .queue_child
= _queue_child
,
2062 .queue_child_rekey
= _queue_child_rekey
,
2063 .queue_child_delete
= _queue_child_delete
,
2064 .queue_dpd
= _queue_dpd
,
2065 .initiate
= _initiate
,
2066 .retransmit
= _retransmit
,
2067 .incr_mid
= _incr_mid
,
2069 .adopt_tasks
= _adopt_tasks
,
2070 .adopt_child_tasks
= _adopt_child_tasks
,
2072 .create_task_enumerator
= _create_task_enumerator
,
2073 .flush_queue
= _flush_queue
,
2074 .destroy
= _destroy
,
2078 .type
= EXCHANGE_TYPE_UNDEFINED
,
2081 .seqnr
= RESPONDING_SEQ
,
2084 .exchange
= ID_PROT
,
2085 .max_packet
= lib
->settings
->get_int(lib
->settings
,
2086 "%s.max_packet", MAX_PACKET
, charon
->name
),
2087 .size
= lib
->settings
->get_int(lib
->settings
,
2088 "%s.fragment_size", MAX_FRAGMENT_SIZE
, charon
->name
),
2091 .rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
),
2092 .queued_tasks
= linked_list_create(),
2093 .active_tasks
= linked_list_create(),
2094 .passive_tasks
= linked_list_create(),
2095 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
2096 "%s.retransmit_tries", RETRANSMIT_TRIES
, charon
->name
),
2097 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
2098 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, charon
->name
),
2099 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
2100 "%s.retransmit_base", RETRANSMIT_BASE
, charon
->name
),
2105 DBG1(DBG_IKE
, "no RNG found, unable to create IKE_SA");
2109 if (!this->rng
->get_bytes(this->rng
, sizeof(this->dpd_send
),
2110 (void*)&this->dpd_send
))
2112 DBG1(DBG_IKE
, "failed to allocate message ID, unable to create IKE_SA");
2116 this->dpd_send
&= 0x7FFFFFFF;
2118 return &this->public;