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 fragmentation_t fragmentation
= FRAGMENTATION_NO
;
419 ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
422 fragmentation
= ike_cfg
->fragmentation(ike_cfg
);
424 data
= packet
->get_data(packet
);
425 if (data
.len
> this->frag
.size
&& (fragmentation
== FRAGMENTATION_FORCE
||
426 (this->ike_sa
->supports_extension(this->ike_sa
, EXT_IKE_FRAGMENTATION
) &&
427 fragmentation
== FRAGMENTATION_YES
)))
429 fragment_payload_t
*fragment
;
431 size_t len
, frag_size
;
434 /* reduce size due to non-ESP marker */
435 nat
= this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_ANY
);
436 frag_size
= this->frag
.size
- (nat
? 4 : 0);
438 src
= packet
->get_source(packet
);
439 dst
= packet
->get_destination(packet
);
440 count
= (data
.len
/ (frag_size
+ 1)) + 1;
442 DBG1(DBG_IKE
, "sending IKE message with length of %zu bytes in "
443 "%hhu fragments", data
.len
, count
);
444 for (num
= 1; num
<= count
; num
++)
446 len
= min(data
.len
, frag_size
);
447 fragment
= fragment_payload_create_from_data(num
, num
== count
,
448 chunk_create(data
.ptr
, len
));
449 if (!send_fragment(this, request
, src
, dst
, fragment
))
451 packet
->destroy(packet
);
454 data
= chunk_skip(data
, len
);
456 packet
->destroy(packet
);
459 charon
->sender
->send(charon
->sender
, packet
);
464 * Retransmit a packet, either as initiator or as responder
466 static status_t
retransmit_packet(private_task_manager_t
*this, bool request
,
467 u_int32_t seqnr
, u_int mid
, u_int retransmitted
, packet_t
*packet
)
471 if (retransmitted
> this->retransmit_tries
)
473 DBG1(DBG_IKE
, "giving up after %u retransmits", retransmitted
- 1);
474 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
, packet
);
477 t
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
478 pow(this->retransmit_base
, retransmitted
));
481 DBG1(DBG_IKE
, "sending retransmit %u of %s message ID %u, seq %u",
482 retransmitted
, seqnr
< RESPONDING_SEQ
? "request" : "response",
483 mid
, seqnr
< RESPONDING_SEQ
? seqnr
: seqnr
- RESPONDING_SEQ
);
484 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
, packet
);
486 if (!send_packet(this, request
, packet
->clone(packet
)))
490 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, (job_t
*)
491 retransmit_job_create(seqnr
, this->ike_sa
->get_id(this->ike_sa
)), t
);
495 METHOD(task_manager_t
, retransmit
, status_t
,
496 private_task_manager_t
*this, u_int32_t seqnr
)
498 status_t status
= SUCCESS
;
500 if (seqnr
== this->initiating
.seqnr
&& this->initiating
.packet
)
502 status
= retransmit_packet(this, TRUE
, seqnr
, this->initiating
.mid
,
503 this->initiating
.retransmitted
, this->initiating
.packet
);
504 if (status
== NEED_MORE
)
506 this->initiating
.retransmitted
++;
510 if (seqnr
== this->responding
.seqnr
&& this->responding
.packet
)
512 status
= retransmit_packet(this, FALSE
, seqnr
, this->responding
.mid
,
513 this->responding
.retransmitted
, this->responding
.packet
);
514 if (status
== NEED_MORE
)
516 this->responding
.retransmitted
++;
524 * Check if we have to wait for a mode config before starting a quick mode
526 static bool mode_config_expected(private_task_manager_t
*this)
528 enumerator_t
*enumerator
;
529 peer_cfg_t
*peer_cfg
;
533 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
536 enumerator
= peer_cfg
->create_pool_enumerator(peer_cfg
);
537 if (!enumerator
->enumerate(enumerator
, &pool
))
538 { /* no pool configured */
539 enumerator
->destroy(enumerator
);
542 enumerator
->destroy(enumerator
);
544 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
,
546 if (!enumerator
->enumerate(enumerator
, &host
))
547 { /* have a pool, but no VIP assigned yet */
548 enumerator
->destroy(enumerator
);
551 enumerator
->destroy(enumerator
);
556 METHOD(task_manager_t
, initiate
, status_t
,
557 private_task_manager_t
*this)
559 enumerator_t
*enumerator
;
564 exchange_type_t exchange
= EXCHANGE_TYPE_UNDEFINED
;
565 bool new_mid
= FALSE
, expect_response
= FALSE
, cancelled
= FALSE
, keep
= FALSE
;
567 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
&&
568 this->initiating
.type
!= INFORMATIONAL_V1
)
570 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
571 exchange_type_names
, this->initiating
.type
);
572 /* do not initiate if we already have a message in the air */
576 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
578 DBG2(DBG_IKE
, "activating new tasks");
579 switch (this->ike_sa
->get_state(this->ike_sa
))
582 activate_task(this, TASK_ISAKMP_VENDOR
);
583 activate_task(this, TASK_ISAKMP_CERT_PRE
);
584 if (activate_task(this, TASK_MAIN_MODE
))
588 else if (activate_task(this, TASK_AGGRESSIVE_MODE
))
590 exchange
= AGGRESSIVE
;
592 activate_task(this, TASK_ISAKMP_CERT_POST
);
593 activate_task(this, TASK_ISAKMP_NATD
);
596 if (activate_task(this, TASK_ISAKMP_DELETE
))
598 exchange
= INFORMATIONAL_V1
;
602 if (activate_task(this, TASK_XAUTH
))
604 exchange
= TRANSACTION
;
608 if (activate_task(this, TASK_INFORMATIONAL
))
610 exchange
= INFORMATIONAL_V1
;
615 case IKE_ESTABLISHED
:
616 if (activate_task(this, TASK_MODE_CONFIG
))
618 exchange
= TRANSACTION
;
622 if (!mode_config_expected(this) &&
623 activate_task(this, TASK_QUICK_MODE
))
625 exchange
= QUICK_MODE
;
629 if (activate_task(this, TASK_INFORMATIONAL
))
631 exchange
= INFORMATIONAL_V1
;
635 if (activate_task(this, TASK_QUICK_DELETE
))
637 exchange
= INFORMATIONAL_V1
;
641 if (activate_task(this, TASK_ISAKMP_DELETE
))
643 exchange
= INFORMATIONAL_V1
;
647 if (activate_task(this, TASK_ISAKMP_DPD
))
649 exchange
= INFORMATIONAL_V1
;
660 DBG2(DBG_IKE
, "reinitiating already active tasks");
661 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
662 while (enumerator
->enumerate(enumerator
, (void**)&task
))
664 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
665 switch (task
->get_type(task
))
670 case TASK_AGGRESSIVE_MODE
:
671 exchange
= AGGRESSIVE
;
673 case TASK_QUICK_MODE
:
674 exchange
= QUICK_MODE
;
677 exchange
= TRANSACTION
;
685 enumerator
->destroy(enumerator
);
688 if (exchange
== EXCHANGE_TYPE_UNDEFINED
)
690 DBG2(DBG_IKE
, "nothing to initiate");
691 /* nothing to do yet... */
695 me
= this->ike_sa
->get_my_host(this->ike_sa
);
696 other
= this->ike_sa
->get_other_host(this->ike_sa
);
700 if (!this->rng
->get_bytes(this->rng
, sizeof(this->initiating
.mid
),
701 (void*)&this->initiating
.mid
))
703 DBG1(DBG_IKE
, "failed to allocate message ID, destroying IKE_SA");
708 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
709 message
->set_message_id(message
, this->initiating
.mid
);
710 message
->set_source(message
, me
->clone(me
));
711 message
->set_destination(message
, other
->clone(other
));
712 message
->set_exchange_type(message
, exchange
);
713 this->initiating
.type
= exchange
;
714 this->initiating
.retransmitted
= 0;
716 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
717 while (enumerator
->enumerate(enumerator
, (void*)&task
))
719 switch (task
->build(task
, message
))
722 /* task completed, remove it */
723 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
724 if (task
->get_type(task
) == TASK_AGGRESSIVE_MODE
||
725 task
->get_type(task
) == TASK_QUICK_MODE
)
726 { /* last message of three message exchange */
732 expect_response
= TRUE
;
733 /* processed, but task needs another exchange */
740 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
742 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
746 /* critical failure, destroy IKE_SA */
747 enumerator
->destroy(enumerator
);
748 message
->destroy(message
);
754 enumerator
->destroy(enumerator
);
756 if (this->active_tasks
->get_count(this->active_tasks
) == 0 &&
757 (exchange
== QUICK_MODE
|| exchange
== AGGRESSIVE
))
758 { /* tasks completed, no exchange active anymore */
759 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
763 message
->destroy(message
);
764 return initiate(this);
767 DESTROY_IF(this->initiating
.packet
);
768 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
769 &this->initiating
.packet
);
770 if (status
!= SUCCESS
)
772 /* message generation failed. There is nothing more to do than to
774 message
->destroy(message
);
776 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
780 this->initiating
.seqnr
++;
783 message
->destroy(message
);
784 return retransmit(this, this->initiating
.seqnr
);
787 { /* keep the packet for retransmission, the responder might request it */
788 send_packet(this, TRUE
,
789 this->initiating
.packet
->clone(this->initiating
.packet
));
793 send_packet(this, TRUE
, this->initiating
.packet
);
794 this->initiating
.packet
= NULL
;
796 message
->destroy(message
);
798 if (exchange
== INFORMATIONAL_V1
)
800 switch (this->ike_sa
->get_state(this->ike_sa
))
803 /* close after sending an INFORMATIONAL when unestablished */
806 /* close after sending a DELETE */
812 return initiate(this);
816 * build a response depending on the "passive" task list
818 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
820 enumerator_t
*enumerator
;
824 bool delete = FALSE
, cancelled
= FALSE
, expect_request
= FALSE
;
827 me
= request
->get_destination(request
);
828 other
= request
->get_source(request
);
830 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
831 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
832 /* send response along the path the request came in */
833 message
->set_source(message
, me
->clone(me
));
834 message
->set_destination(message
, other
->clone(other
));
835 message
->set_message_id(message
, request
->get_message_id(request
));
836 message
->set_request(message
, FALSE
);
838 this->responding
.mid
= request
->get_message_id(request
);
839 this->responding
.retransmitted
= 0;
840 this->responding
.seqnr
++;
842 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
843 while (enumerator
->enumerate(enumerator
, (void*)&task
))
845 switch (task
->build(task
, message
))
848 /* task completed, remove it */
849 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
853 /* processed, but task needs another exchange */
854 if (task
->get_type(task
) == TASK_QUICK_MODE
||
855 task
->get_type(task
) == TASK_AGGRESSIVE_MODE
)
856 { /* we rely on initiator retransmission, except for
857 * three-message exchanges */
858 expect_request
= TRUE
;
866 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
869 /* destroy IKE_SA, but SEND response first */
875 enumerator
->destroy(enumerator
);
877 DESTROY_IF(this->responding
.packet
);
878 this->responding
.packet
= NULL
;
881 message
->destroy(message
);
882 return initiate(this);
884 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
885 &this->responding
.packet
);
886 message
->destroy(message
);
887 if (status
!= SUCCESS
)
889 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
893 if (expect_request
&& !delete)
895 return retransmit(this, this->responding
.seqnr
);
897 send_packet(this, FALSE
,
898 this->responding
.packet
->clone(this->responding
.packet
));
907 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
908 * The notify protocol_id is set to ISAKMP
910 static void send_notify(private_task_manager_t
*this, message_t
*request
,
918 if (request
->get_exchange_type(request
) == INFORMATIONAL_V1
)
919 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
920 DBG1(DBG_IKE
, "ignore malformed INFORMATIONAL request");
923 if (!this->rng
->get_bytes(this->rng
, sizeof(mid
), (void*)&mid
))
925 DBG1(DBG_IKE
, "failed to allocate message ID");
928 response
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
929 response
->set_exchange_type(response
, INFORMATIONAL_V1
);
930 response
->set_request(response
, TRUE
);
931 response
->set_message_id(response
, mid
);
932 response
->add_payload(response
, (payload_t
*)
933 notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
936 me
= this->ike_sa
->get_my_host(this->ike_sa
);
937 if (me
->is_anyaddr(me
))
939 me
= request
->get_destination(request
);
940 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
942 other
= this->ike_sa
->get_other_host(this->ike_sa
);
943 if (other
->is_anyaddr(other
))
945 other
= request
->get_source(request
);
946 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
948 response
->set_source(response
, me
->clone(me
));
949 response
->set_destination(response
, other
->clone(other
));
950 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
953 send_packet(this, TRUE
, packet
);
955 response
->destroy(response
);
959 * Process a DPD request/response
961 static bool process_dpd(private_task_manager_t
*this, message_t
*message
)
963 notify_payload_t
*notify
;
968 type
= DPD_R_U_THERE
;
969 notify
= message
->get_notify(message
, type
);
972 type
= DPD_R_U_THERE_ACK
;
973 notify
= message
->get_notify(message
, type
);
979 data
= notify
->get_notification_data(notify
);
984 seq
= untoh32(data
.ptr
);
986 if (type
== DPD_R_U_THERE
)
988 if (this->dpd_recv
== 0 || seq
== this->dpd_recv
)
989 { /* check sequence validity */
990 this->dpd_recv
= seq
+ 1;
991 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
992 time_monotonic(NULL
));
994 /* but respond anyway */
995 this->ike_sa
->queue_task(this->ike_sa
,
996 &isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE_ACK
, seq
)->task
);
998 else /* DPD_R_U_THERE_ACK */
1000 if (seq
== this->dpd_send
- 1)
1002 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1003 time_monotonic(NULL
));
1007 DBG1(DBG_IKE
, "received invalid DPD sequence number %u "
1008 "(expected %u), ignored", seq
, this->dpd_send
- 1);
1015 * handle an incoming request message
1017 static status_t
process_request(private_task_manager_t
*this,
1020 enumerator_t
*enumerator
;
1021 task_t
*task
= NULL
;
1022 bool send_response
= FALSE
, dpd
= FALSE
;
1024 if (message
->get_exchange_type(message
) == INFORMATIONAL_V1
||
1025 this->passive_tasks
->get_count(this->passive_tasks
) == 0)
1026 { /* create tasks depending on request type, if not already some queued */
1027 switch (message
->get_exchange_type(message
))
1030 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
1031 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1032 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
1033 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1034 task
= (task_t
*)main_mode_create(this->ike_sa
, FALSE
);
1035 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1036 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
1037 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1038 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1039 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1042 task
= (task_t
*)isakmp_vendor_create(this->ike_sa
, FALSE
);
1043 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1044 task
= (task_t
*)isakmp_cert_pre_create(this->ike_sa
, FALSE
);
1045 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1046 task
= (task_t
*)aggressive_mode_create(this->ike_sa
, FALSE
);
1047 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1048 task
= (task_t
*)isakmp_cert_post_create(this->ike_sa
, FALSE
);
1049 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1050 task
= (task_t
*)isakmp_natd_create(this->ike_sa
, FALSE
);
1051 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1052 this->frag
.exchange
= AGGRESSIVE
;
1055 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_ESTABLISHED
)
1057 DBG1(DBG_IKE
, "received quick mode request for "
1058 "unestablished IKE_SA, ignored");
1061 task
= (task_t
*)quick_mode_create(this->ike_sa
, NULL
,
1063 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1065 case INFORMATIONAL_V1
:
1066 if (process_dpd(this, message
))
1072 task
= (task_t
*)informational_create(this->ike_sa
, NULL
);
1073 this->passive_tasks
->insert_first(this->passive_tasks
, task
);
1077 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
1079 task
= (task_t
*)mode_config_create(this->ike_sa
, FALSE
);
1083 task
= (task_t
*)xauth_create(this->ike_sa
, FALSE
);
1085 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
1093 return initiate(this);
1095 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
, time_monotonic(NULL
));
1097 /* let the tasks process the message */
1098 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
1099 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1101 switch (task
->process(task
, message
))
1104 /* task completed, remove it */
1105 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1106 task
->destroy(task
);
1109 /* processed, but task needs at least another call to build() */
1110 send_response
= TRUE
;
1113 send_response
= FALSE
;
1117 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1120 /* critical failure, destroy IKE_SA */
1121 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
1122 enumerator
->destroy(enumerator
);
1123 task
->destroy(task
);
1128 enumerator
->destroy(enumerator
);
1132 if (build_response(this, message
) != SUCCESS
)
1138 { /* We don't send a response, so don't retransmit one if we get
1139 * the same message again. */
1140 DESTROY_IF(this->responding
.packet
);
1141 this->responding
.packet
= NULL
;
1143 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0 &&
1144 this->queued_tasks
->get_count(this->queued_tasks
) > 0)
1146 /* passive tasks completed, check if an active task has been queued,
1147 * such as XAUTH or modeconfig push */
1148 return initiate(this);
1154 * handle an incoming response message
1156 static status_t
process_response(private_task_manager_t
*this,
1159 enumerator_t
*enumerator
;
1164 if (message
->get_exchange_type(message
) != this->initiating
.type
)
1166 DBG1(DBG_IKE
, "received %N response, but expected %N",
1167 exchange_type_names
, message
->get_exchange_type(message
),
1168 exchange_type_names
, this->initiating
.type
);
1169 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1173 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
1174 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1176 switch (task
->process(task
, message
))
1179 /* task completed, remove it */
1180 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1181 task
->destroy(task
);
1184 /* processed, but task needs another exchange */
1190 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1193 /* critical failure, destroy IKE_SA */
1194 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
1195 enumerator
->destroy(enumerator
);
1196 task
->destroy(task
);
1201 enumerator
->destroy(enumerator
);
1203 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1204 DESTROY_IF(this->initiating
.packet
);
1205 this->initiating
.packet
= NULL
;
1207 if (this->queued
&& this->active_tasks
->get_count(this->active_tasks
) == 0)
1209 queued
= this->queued
;
1210 this->queued
= NULL
;
1211 status
= this->public.task_manager
.process_message(
1212 &this->public.task_manager
, queued
);
1213 queued
->destroy(queued
);
1214 if (status
== DESTROY_ME
)
1220 return initiate(this);
1223 static status_t
handle_fragment(private_task_manager_t
*this, message_t
*msg
)
1225 fragment_payload_t
*payload
;
1226 enumerator_t
*enumerator
;
1227 fragment_t
*fragment
;
1228 status_t status
= SUCCESS
;
1232 payload
= (fragment_payload_t
*)msg
->get_payload(msg
, FRAGMENT_V1
);
1238 if (this->frag
.id
!= payload
->get_id(payload
))
1240 clear_fragments(this, payload
->get_id(payload
));
1241 this->frag
.list
= linked_list_create();
1244 num
= payload
->get_number(payload
);
1245 if (!this->frag
.last
&& payload
->is_last(payload
))
1247 this->frag
.last
= num
;
1250 enumerator
= this->frag
.list
->create_enumerator(this->frag
.list
);
1251 while (enumerator
->enumerate(enumerator
, &fragment
))
1253 if (fragment
->num
== num
)
1254 { /* ignore a duplicate fragment */
1255 DBG1(DBG_IKE
, "received duplicate fragment #%hhu", num
);
1256 enumerator
->destroy(enumerator
);
1259 if (fragment
->num
> num
)
1265 data
= payload
->get_data(payload
);
1266 this->frag
.len
+= data
.len
;
1267 if (this->frag
.len
> this->frag
.max_packet
)
1269 DBG1(DBG_IKE
, "fragmented IKE message is too large");
1270 enumerator
->destroy(enumerator
);
1271 clear_fragments(this, 0);
1277 .data
= chunk_clone(data
),
1280 this->frag
.list
->insert_before(this->frag
.list
, enumerator
, fragment
);
1281 enumerator
->destroy(enumerator
);
1283 if (this->frag
.list
->get_count(this->frag
.list
) == this->frag
.last
)
1288 bio_writer_t
*writer
;
1290 writer
= bio_writer_create(this->frag
.len
);
1291 DBG1(DBG_IKE
, "received fragment #%hhu, reassembling fragmented IKE "
1293 enumerator
= this->frag
.list
->create_enumerator(this->frag
.list
);
1294 while (enumerator
->enumerate(enumerator
, &fragment
))
1296 writer
->write_data(writer
, fragment
->data
);
1298 enumerator
->destroy(enumerator
);
1300 src
= msg
->get_source(msg
);
1301 dst
= msg
->get_destination(msg
);
1302 pkt
= packet_create_from_data(src
->clone(src
), dst
->clone(dst
),
1303 writer
->extract_buf(writer
));
1304 writer
->destroy(writer
);
1306 message
= message_create_from_packet(pkt
);
1307 if (message
->parse_header(message
) != SUCCESS
)
1309 DBG1(DBG_IKE
, "failed to parse header of reassembled IKE message");
1310 message
->destroy(message
);
1315 lib
->processor
->queue_job(lib
->processor
,
1316 (job_t
*)process_message_job_create(message
));
1320 clear_fragments(this, 0);
1323 { /* there are some fragments missing */
1324 DBG1(DBG_IKE
, "received fragment #%hhu, waiting for complete IKE "
1332 * Parse the given message and verify that it is valid.
1334 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
1338 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
1340 if (status
!= SUCCESS
)
1345 DBG1(DBG_IKE
, "unsupported exchange type");
1346 send_notify(this, msg
, INVALID_EXCHANGE_TYPE
);
1349 DBG1(DBG_IKE
, "message parsing failed");
1350 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1353 DBG1(DBG_IKE
, "message verification failed");
1354 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1357 DBG1(DBG_IKE
, "integrity check failed");
1358 send_notify(this, msg
, INVALID_HASH_INFORMATION
);
1361 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1362 send_notify(this, msg
, PAYLOAD_MALFORMED
);
1366 DBG1(DBG_IKE
, "%N %s with message ID %u processing failed",
1367 exchange_type_names
, msg
->get_exchange_type(msg
),
1368 msg
->get_request(msg
) ? "request" : "response",
1369 msg
->get_message_id(msg
));
1371 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
, status
);
1373 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1374 { /* invalid initiation attempt, close SA */
1379 if (msg
->get_first_payload_type(msg
) == FRAGMENT_V1
)
1381 return handle_fragment(this, msg
);
1386 METHOD(task_manager_t
, process_message
, status_t
,
1387 private_task_manager_t
*this, message_t
*msg
)
1389 u_int32_t hash
, mid
, i
;
1393 /* TODO-IKEv1: update hosts more selectively */
1394 me
= msg
->get_destination(msg
);
1395 other
= msg
->get_source(msg
);
1396 mid
= msg
->get_message_id(msg
);
1397 hash
= chunk_hash(msg
->get_packet_data(msg
));
1398 for (i
= 0; i
< MAX_OLD_HASHES
; i
++)
1400 if (this->initiating
.old_hashes
[i
] == hash
)
1402 if (this->initiating
.packet
&&
1403 i
== (this->initiating
.old_hash_pos
% MAX_OLD_HASHES
) &&
1404 (msg
->get_exchange_type(msg
) == QUICK_MODE
||
1405 msg
->get_exchange_type(msg
) == AGGRESSIVE
))
1407 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1408 "resending last request", mid
);
1409 send_packet(this, TRUE
,
1410 this->initiating
.packet
->clone(this->initiating
.packet
));
1413 DBG1(DBG_IKE
, "received retransmit of response with ID %u, "
1414 "but next request already sent", mid
);
1419 if ((mid
&& mid
== this->initiating
.mid
) ||
1420 (this->initiating
.mid
== 0 &&
1421 msg
->get_exchange_type(msg
) == this->initiating
.type
&&
1422 this->active_tasks
->get_count(this->active_tasks
)))
1424 msg
->set_request(msg
, FALSE
);
1425 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1426 status
= parse_message(this, msg
);
1427 if (status
== NEED_MORE
)
1431 if (status
!= SUCCESS
)
1435 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1436 time_monotonic(NULL
));
1437 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1438 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1439 if (process_response(this, msg
) != SUCCESS
)
1444 this->initiating
.old_hashes
[(++this->initiating
.old_hash_pos
) %
1445 MAX_OLD_HASHES
] = hash
;
1449 if (hash
== this->responding
.hash
)
1451 if (this->responding
.packet
)
1453 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1454 "retransmitting response", mid
);
1455 send_packet(this, FALSE
,
1456 this->responding
.packet
->clone(this->responding
.packet
));
1458 else if (this->initiating
.packet
&&
1459 this->initiating
.type
== INFORMATIONAL_V1
)
1461 DBG1(DBG_IKE
, "received retransmit of DPD request, "
1462 "retransmitting response");
1463 send_packet(this, TRUE
,
1464 this->initiating
.packet
->clone(this->initiating
.packet
));
1468 DBG1(DBG_IKE
, "received retransmit of request with ID %u, "
1469 "but no response to retransmit", mid
);
1471 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1475 /* reject Main/Agressive Modes once established */
1476 if (msg
->get_exchange_type(msg
) == ID_PROT
||
1477 msg
->get_exchange_type(msg
) == AGGRESSIVE
)
1479 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CREATED
&&
1480 this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
&&
1481 msg
->get_first_payload_type(msg
) != FRAGMENT_V1
)
1483 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1484 exchange_type_names
, msg
->get_exchange_type(msg
));
1489 if (msg
->get_exchange_type(msg
) == TRANSACTION
&&
1490 this->active_tasks
->get_count(this->active_tasks
))
1491 { /* main mode not yet complete, queue XAuth/Mode config tasks */
1494 DBG1(DBG_IKE
, "ignoring additional %N request, queue full",
1495 exchange_type_names
, TRANSACTION
);
1498 this->queued
= message_create_from_packet(msg
->get_packet(msg
));
1499 if (this->queued
->parse_header(this->queued
) != SUCCESS
)
1501 this->queued
->destroy(this->queued
);
1502 this->queued
= NULL
;
1505 DBG1(DBG_IKE
, "queueing %N request as tasks still active",
1506 exchange_type_names
, TRANSACTION
);
1510 msg
->set_request(msg
, TRUE
);
1511 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1512 status
= parse_message(this, msg
);
1513 if (status
== NEED_MORE
)
1517 if (status
!= SUCCESS
)
1521 /* if this IKE_SA is virgin, we check for a config */
1522 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1524 ike_sa_id_t
*ike_sa_id
;
1528 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1530 if (ike_cfg
== NULL
)
1532 /* no config found for these hosts, destroy */
1533 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1534 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1535 send_notify(this, msg
, NO_PROPOSAL_CHOSEN
);
1538 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1539 ike_cfg
->destroy(ike_cfg
);
1540 /* add a timeout if peer does not establish it completely */
1541 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1542 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1543 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1544 lib
->settings
->get_int(lib
->settings
,
1545 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1548 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
1549 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1550 if (process_request(this, msg
) != SUCCESS
)
1555 this->responding
.hash
= hash
;
1560 METHOD(task_manager_t
, queue_task
, void,
1561 private_task_manager_t
*this, task_t
*task
)
1563 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1564 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1568 * Check if a given task has been queued already
1570 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1572 enumerator_t
*enumerator
;
1576 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1577 while (enumerator
->enumerate(enumerator
, &task
))
1579 if (task
->get_type(task
) == type
)
1585 enumerator
->destroy(enumerator
);
1589 METHOD(task_manager_t
, queue_ike
, void,
1590 private_task_manager_t
*this)
1592 peer_cfg_t
*peer_cfg
;
1594 if (!has_queued(this, TASK_ISAKMP_VENDOR
))
1596 queue_task(this, (task_t
*)isakmp_vendor_create(this->ike_sa
, TRUE
));
1598 if (!has_queued(this, TASK_ISAKMP_CERT_PRE
))
1600 queue_task(this, (task_t
*)isakmp_cert_pre_create(this->ike_sa
, TRUE
));
1602 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1603 if (peer_cfg
->use_aggressive(peer_cfg
))
1605 if (!has_queued(this, TASK_AGGRESSIVE_MODE
))
1607 queue_task(this, (task_t
*)aggressive_mode_create(this->ike_sa
, TRUE
));
1609 this->frag
.exchange
= AGGRESSIVE
;
1613 if (!has_queued(this, TASK_MAIN_MODE
))
1615 queue_task(this, (task_t
*)main_mode_create(this->ike_sa
, TRUE
));
1618 if (!has_queued(this, TASK_ISAKMP_CERT_POST
))
1620 queue_task(this, (task_t
*)isakmp_cert_post_create(this->ike_sa
, TRUE
));
1622 if (!has_queued(this, TASK_ISAKMP_NATD
))
1624 queue_task(this, (task_t
*)isakmp_natd_create(this->ike_sa
, TRUE
));
1628 METHOD(task_manager_t
, queue_ike_reauth
, void,
1629 private_task_manager_t
*this)
1631 enumerator_t
*enumerator
;
1632 child_sa_t
*child_sa
;
1636 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1637 this->ike_sa
->get_version(this->ike_sa
), TRUE
);
1639 { /* shouldn't happen */
1643 new->set_peer_cfg(new, this->ike_sa
->get_peer_cfg(this->ike_sa
));
1644 host
= this->ike_sa
->get_other_host(this->ike_sa
);
1645 new->set_other_host(new, host
->clone(host
));
1646 host
= this->ike_sa
->get_my_host(this->ike_sa
);
1647 new->set_my_host(new, host
->clone(host
));
1648 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
, TRUE
);
1649 while (enumerator
->enumerate(enumerator
, &host
))
1651 new->add_virtual_ip(new, TRUE
, host
);
1653 enumerator
->destroy(enumerator
);
1655 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1656 while (enumerator
->enumerate(enumerator
, &child_sa
))
1658 this->ike_sa
->remove_child_sa(this->ike_sa
, enumerator
);
1659 new->add_child_sa(new, child_sa
);
1661 enumerator
->destroy(enumerator
);
1663 if (!new->get_child_count(new))
1664 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1667 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1668 while (enumerator
->enumerate(enumerator
, &task
))
1670 if (task
->get_type(task
) == TASK_QUICK_MODE
)
1672 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
1673 task
->migrate(task
, new);
1674 new->queue_task(new, task
);
1677 enumerator
->destroy(enumerator
);
1680 if (new->initiate(new, NULL
, 0, NULL
, NULL
) != DESTROY_ME
)
1682 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
1683 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
1687 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
1688 DBG1(DBG_IKE
, "reauthenticating IKE_SA failed");
1690 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
1693 METHOD(task_manager_t
, queue_ike_rekey
, void,
1694 private_task_manager_t
*this)
1696 queue_ike_reauth(this);
1699 METHOD(task_manager_t
, queue_ike_delete
, void,
1700 private_task_manager_t
*this)
1702 enumerator_t
*enumerator
;
1703 child_sa_t
*child_sa
;
1705 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1706 while (enumerator
->enumerate(enumerator
, &child_sa
))
1708 queue_task(this, (task_t
*)
1709 quick_delete_create(this->ike_sa
, child_sa
->get_protocol(child_sa
),
1710 child_sa
->get_spi(child_sa
, TRUE
), FALSE
, FALSE
));
1712 enumerator
->destroy(enumerator
);
1714 queue_task(this, (task_t
*)isakmp_delete_create(this->ike_sa
, TRUE
));
1717 METHOD(task_manager_t
, queue_mobike
, void,
1718 private_task_manager_t
*this, bool roam
, bool address
)
1720 /* Not supported in IKEv1 */
1723 METHOD(task_manager_t
, queue_child
, void,
1724 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1725 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1729 task
= quick_mode_create(this->ike_sa
, cfg
, tsi
, tsr
);
1730 task
->use_reqid(task
, reqid
);
1732 queue_task(this, &task
->task
);
1736 * Check if two CHILD_SAs have the same traffic selector
1738 static bool have_equal_ts(child_sa_t
*a
, child_sa_t
*b
, bool local
)
1740 linked_list_t
*list
;
1741 traffic_selector_t
*ts_a
, *ts_b
;
1743 list
= a
->get_traffic_selectors(a
, local
);
1744 if (list
->get_first(list
, (void**)&ts_a
) == SUCCESS
)
1746 list
= b
->get_traffic_selectors(b
, local
);
1747 if (list
->get_first(list
, (void**)&ts_b
) == SUCCESS
)
1749 return ts_a
->equals(ts_a
, ts_b
);
1756 * Check if a CHILD_SA is redundant and we should delete instead of rekey
1758 static bool is_redundant(private_task_manager_t
*this, child_sa_t
*child_sa
)
1760 enumerator_t
*enumerator
;
1761 child_sa_t
*current
;
1762 bool redundant
= FALSE
;
1764 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1765 while (enumerator
->enumerate(enumerator
, ¤t
))
1767 if (current
->get_state(current
) == CHILD_INSTALLED
&&
1768 streq(current
->get_name(current
), child_sa
->get_name(child_sa
)) &&
1769 have_equal_ts(current
, child_sa
, TRUE
) &&
1770 have_equal_ts(current
, child_sa
, FALSE
) &&
1771 current
->get_lifetime(current
, FALSE
) >
1772 child_sa
->get_lifetime(child_sa
, FALSE
))
1774 DBG1(DBG_IKE
, "deleting redundant CHILD_SA %s{%d}",
1775 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
));
1780 enumerator
->destroy(enumerator
);
1786 * Get the first traffic selector of a CHILD_SA, local or remote
1788 static traffic_selector_t
* get_first_ts(child_sa_t
*child_sa
, bool local
)
1790 traffic_selector_t
*ts
= NULL
;
1791 linked_list_t
*list
;
1793 list
= child_sa
->get_traffic_selectors(child_sa
, local
);
1794 if (list
->get_first(list
, (void**)&ts
) == SUCCESS
)
1801 METHOD(task_manager_t
, queue_child_rekey
, void,
1802 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1804 child_sa_t
*child_sa
;
1808 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, TRUE
);
1811 child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
, spi
, FALSE
);
1813 if (child_sa
&& child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
1815 if (is_redundant(this, child_sa
))
1817 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
,
1818 protocol
, spi
, FALSE
, FALSE
));
1822 child_sa
->set_state(child_sa
, CHILD_REKEYING
);
1823 cfg
= child_sa
->get_config(child_sa
);
1824 task
= quick_mode_create(this->ike_sa
, cfg
->get_ref(cfg
),
1825 get_first_ts(child_sa
, TRUE
), get_first_ts(child_sa
, FALSE
));
1826 task
->use_reqid(task
, child_sa
->get_reqid(child_sa
));
1827 task
->rekey(task
, child_sa
->get_spi(child_sa
, TRUE
));
1829 queue_task(this, &task
->task
);
1834 METHOD(task_manager_t
, queue_child_delete
, void,
1835 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
,
1838 queue_task(this, (task_t
*)quick_delete_create(this->ike_sa
, protocol
,
1839 spi
, FALSE
, expired
));
1842 METHOD(task_manager_t
, queue_dpd
, void,
1843 private_task_manager_t
*this)
1845 peer_cfg_t
*peer_cfg
;
1846 u_int32_t t
, retransmit
;
1848 queue_task(this, (task_t
*)isakmp_dpd_create(this->ike_sa
, DPD_R_U_THERE
,
1850 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1852 /* compute timeout in milliseconds */
1853 t
= 1000 * peer_cfg
->get_dpd_timeout(peer_cfg
);
1856 /* use the same timeout as a retransmitting IKE message would have */
1857 for (retransmit
= 0; retransmit
<= this->retransmit_tries
; retransmit
++)
1859 t
+= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
1860 pow(this->retransmit_base
, retransmit
));
1864 /* schedule DPD timeout job */
1865 lib
->scheduler
->schedule_job_ms(lib
->scheduler
,
1866 (job_t
*)dpd_timeout_job_create(this->ike_sa
->get_id(this->ike_sa
)), t
);
1869 METHOD(task_manager_t
, adopt_tasks
, void,
1870 private_task_manager_t
*this, task_manager_t
*other_public
)
1872 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1875 /* move queued tasks from other to this */
1876 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1877 (void**)&task
) == SUCCESS
)
1879 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1880 task
->migrate(task
, this->ike_sa
);
1881 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1885 METHOD(task_manager_t
, busy
, bool,
1886 private_task_manager_t
*this)
1888 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1891 METHOD(task_manager_t
, incr_mid
, void,
1892 private_task_manager_t
*this, bool initiate
)
1896 METHOD(task_manager_t
, reset
, void,
1897 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1899 enumerator_t
*enumerator
;
1902 /* reset message counters and retransmit packets */
1903 DESTROY_IF(this->responding
.packet
);
1904 DESTROY_IF(this->initiating
.packet
);
1905 this->responding
.packet
= NULL
;
1906 this->responding
.seqnr
= RESPONDING_SEQ
;
1907 this->responding
.retransmitted
= 0;
1908 this->initiating
.packet
= NULL
;
1909 this->initiating
.mid
= 0;
1910 this->initiating
.seqnr
= 0;
1911 this->initiating
.retransmitted
= 0;
1912 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1913 clear_fragments(this, 0);
1914 if (initiate
!= UINT_MAX
)
1916 this->dpd_send
= initiate
;
1918 if (respond
!= UINT_MAX
)
1920 this->dpd_recv
= respond
;
1923 /* reset queued tasks */
1924 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1925 while (enumerator
->enumerate(enumerator
, &task
))
1927 task
->migrate(task
, this->ike_sa
);
1929 enumerator
->destroy(enumerator
);
1931 /* reset active tasks */
1932 while (this->active_tasks
->remove_last(this->active_tasks
,
1933 (void**)&task
) == SUCCESS
)
1935 task
->migrate(task
, this->ike_sa
);
1936 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1940 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1941 private_task_manager_t
*this, task_queue_t queue
)
1945 case TASK_QUEUE_ACTIVE
:
1946 return this->active_tasks
->create_enumerator(this->active_tasks
);
1947 case TASK_QUEUE_PASSIVE
:
1948 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
1949 case TASK_QUEUE_QUEUED
:
1950 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
1952 return enumerator_create_empty();
1956 METHOD(task_manager_t
, destroy
, void,
1957 private_task_manager_t
*this)
1961 this->active_tasks
->destroy(this->active_tasks
);
1962 this->queued_tasks
->destroy(this->queued_tasks
);
1963 this->passive_tasks
->destroy(this->passive_tasks
);
1964 clear_fragments(this, 0);
1966 DESTROY_IF(this->queued
);
1967 DESTROY_IF(this->responding
.packet
);
1968 DESTROY_IF(this->initiating
.packet
);
1969 DESTROY_IF(this->rng
);
1976 task_manager_v1_t
*task_manager_v1_create(ike_sa_t
*ike_sa
)
1978 private_task_manager_t
*this;
1983 .process_message
= _process_message
,
1984 .queue_task
= _queue_task
,
1985 .queue_ike
= _queue_ike
,
1986 .queue_ike_rekey
= _queue_ike_rekey
,
1987 .queue_ike_reauth
= _queue_ike_reauth
,
1988 .queue_ike_delete
= _queue_ike_delete
,
1989 .queue_mobike
= _queue_mobike
,
1990 .queue_child
= _queue_child
,
1991 .queue_child_rekey
= _queue_child_rekey
,
1992 .queue_child_delete
= _queue_child_delete
,
1993 .queue_dpd
= _queue_dpd
,
1994 .initiate
= _initiate
,
1995 .retransmit
= _retransmit
,
1996 .incr_mid
= _incr_mid
,
1998 .adopt_tasks
= _adopt_tasks
,
2000 .create_task_enumerator
= _create_task_enumerator
,
2001 .flush_queue
= _flush_queue
,
2002 .destroy
= _destroy
,
2006 .type
= EXCHANGE_TYPE_UNDEFINED
,
2009 .seqnr
= RESPONDING_SEQ
,
2012 .exchange
= ID_PROT
,
2013 .max_packet
= lib
->settings
->get_int(lib
->settings
,
2014 "%s.max_packet", MAX_PACKET
, charon
->name
),
2015 .size
= lib
->settings
->get_int(lib
->settings
,
2016 "%s.fragment_size", MAX_FRAGMENT_SIZE
, charon
->name
),
2019 .rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
),
2020 .queued_tasks
= linked_list_create(),
2021 .active_tasks
= linked_list_create(),
2022 .passive_tasks
= linked_list_create(),
2023 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
2024 "%s.retransmit_tries", RETRANSMIT_TRIES
, charon
->name
),
2025 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
2026 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, charon
->name
),
2027 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
2028 "%s.retransmit_base", RETRANSMIT_BASE
, charon
->name
),
2033 DBG1(DBG_IKE
, "no RNG found, unable to create IKE_SA");
2037 if (!this->rng
->get_bytes(this->rng
, sizeof(this->dpd_send
),
2038 (void*)&this->dpd_send
))
2040 DBG1(DBG_IKE
, "failed to allocate message ID, unable to create IKE_SA");
2044 this->dpd_send
&= 0x7FFFFFFF;
2046 return &this->public;