2 * Copyright (C) 2007-2018 Tobias Brunner
3 * Copyright (C) 2007-2010 Martin Willi
4 * HSR Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "task_manager_v2.h"
21 #include <collections/array.h>
23 #include <sa/ikev2/tasks/ike_init.h>
24 #include <sa/ikev2/tasks/ike_natd.h>
25 #include <sa/ikev2/tasks/ike_mobike.h>
26 #include <sa/ikev2/tasks/ike_auth.h>
27 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
28 #include <sa/ikev2/tasks/ike_cert_pre.h>
29 #include <sa/ikev2/tasks/ike_cert_post.h>
30 #include <sa/ikev2/tasks/ike_rekey.h>
31 #include <sa/ikev2/tasks/ike_reauth.h>
32 #include <sa/ikev2/tasks/ike_reauth_complete.h>
33 #include <sa/ikev2/tasks/ike_redirect.h>
34 #include <sa/ikev2/tasks/ike_delete.h>
35 #include <sa/ikev2/tasks/ike_config.h>
36 #include <sa/ikev2/tasks/ike_dpd.h>
37 #include <sa/ikev2/tasks/ike_mid_sync.h>
38 #include <sa/ikev2/tasks/ike_vendor.h>
39 #include <sa/ikev2/tasks/ike_verify_peer_cert.h>
40 #include <sa/ikev2/tasks/child_create.h>
41 #include <sa/ikev2/tasks/child_rekey.h>
42 #include <sa/ikev2/tasks/child_delete.h>
43 #include <encoding/payloads/delete_payload.h>
44 #include <encoding/payloads/unknown_payload.h>
45 #include <processing/jobs/retransmit_job.h>
46 #include <processing/jobs/delete_ike_sa_job.h>
47 #include <processing/jobs/initiate_tasks_job.h>
50 #include <sa/ikev2/tasks/ike_me.h>
53 typedef struct private_task_manager_t private_task_manager_t
;
54 typedef struct queued_task_t queued_task_t
;
57 * private data of the task manager
59 struct private_task_manager_t
{
64 task_manager_v2_t
public;
67 * associated IKE_SA we are serving
72 * Exchange we are currently handling as responder
76 * Message ID of the exchange
81 * packet(s) for retransmission
86 * Helper to defragment the request
93 * Exchange we are currently handling as initiator
97 * Message ID of the exchange
102 * how many times we have retransmitted so far
107 * packet(s) for retransmission
112 * type of the initiated exchange
114 exchange_type_t type
;
117 * TRUE if exchange was deferred because no path was available
122 * Helper to defragment the response
129 * Array of queued tasks not yet in action
131 array_t
*queued_tasks
;
134 * Array of active tasks, initiated by ourselves
136 array_t
*active_tasks
;
139 * Array of tasks initiated by peer
141 array_t
*passive_tasks
;
144 * the task manager has been reset
150 * Number of times we retransmit messages before giving up
152 u_int retransmit_tries
;
155 * Retransmission timeout
157 double retransmit_timeout
;
160 * Base to calculate retransmission timeout
162 double retransmit_base
;
165 * Jitter to apply to calculated retransmit timeout (in percent)
167 u_int retransmit_jitter
;
170 * Limit retransmit timeout to this value
172 uint32_t retransmit_limit
;
176 * Use make-before-break instead of break-before-make reauth?
178 bool make_before_break
;
184 struct queued_task_t
{
192 * Time before which the task is not to be initiated
198 * Reset retransmission packet list
200 static void clear_packets(array_t
*array
)
204 while (array_remove(array
, ARRAY_TAIL
, &packet
))
206 packet
->destroy(packet
);
210 METHOD(task_manager_t
, flush_queue
, void,
211 private_task_manager_t
*this, task_queue_t queue
)
218 case TASK_QUEUE_ACTIVE
:
219 array
= this->active_tasks
;
221 case TASK_QUEUE_PASSIVE
:
222 array
= this->passive_tasks
;
224 case TASK_QUEUE_QUEUED
:
225 array
= this->queued_tasks
;
230 while (array_remove(array
, ARRAY_TAIL
, &task
))
232 if (queue
== TASK_QUEUE_QUEUED
)
234 queued_task_t
*queued
= (queued_task_t
*)task
;
242 METHOD(task_manager_t
, flush
, void,
243 private_task_manager_t
*this)
245 flush_queue(this, TASK_QUEUE_QUEUED
);
246 flush_queue(this, TASK_QUEUE_PASSIVE
);
247 flush_queue(this, TASK_QUEUE_ACTIVE
);
251 * Move a task of a specific type from the queue to the active list, if it is
254 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
256 enumerator_t
*enumerator
;
257 queued_task_t
*queued
;
261 time_monotonic(&now
);
263 enumerator
= array_create_enumerator(this->queued_tasks
);
264 while (enumerator
->enumerate(enumerator
, (void**)&queued
))
266 if (queued
->task
->get_type(queued
->task
) == type
&&
267 !timercmp(&now
, &queued
->time
, <))
269 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
270 array_remove_at(this->queued_tasks
, enumerator
);
271 array_insert(this->active_tasks
, ARRAY_TAIL
, queued
->task
);
277 enumerator
->destroy(enumerator
);
282 * Send packets in the given array (they get cloned). Optionally, the
283 * source and destination addresses are changed before sending it.
285 static void send_packets(private_task_manager_t
*this, array_t
*packets
,
286 host_t
*src
, host_t
*dst
)
288 packet_t
*packet
, *clone
;
291 for (i
= 0; i
< array_count(packets
); i
++)
293 array_get(packets
, i
, &packet
);
294 clone
= packet
->clone(packet
);
297 clone
->set_source(clone
, src
->clone(src
));
301 clone
->set_destination(clone
, dst
->clone(dst
));
303 charon
->sender
->send(charon
->sender
, clone
);
308 * Generates the given message and stores packet(s) in the given array
310 static bool generate_message(private_task_manager_t
*this, message_t
*message
,
313 enumerator_t
*fragments
;
316 if (this->ike_sa
->generate_message_fragmented(this->ike_sa
, message
,
317 &fragments
) != SUCCESS
)
321 while (fragments
->enumerate(fragments
, &fragment
))
323 array_insert_create(packets
, ARRAY_TAIL
, fragment
);
325 fragments
->destroy(fragments
);
326 array_compress(*packets
);
330 METHOD(task_manager_t
, retransmit
, status_t
,
331 private_task_manager_t
*this, uint32_t message_id
)
333 if (message_id
== this->initiating
.mid
&&
334 array_count(this->initiating
.packets
))
336 uint32_t timeout
, max_jitter
;
338 enumerator_t
*enumerator
;
341 ike_mobike_t
*mobike
= NULL
;
343 array_get(this->initiating
.packets
, 0, &packet
);
345 /* check if we are retransmitting a MOBIKE routability check */
346 if (this->initiating
.type
== INFORMATIONAL
)
348 enumerator
= array_create_enumerator(this->active_tasks
);
349 while (enumerator
->enumerate(enumerator
, (void*)&task
))
351 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
353 mobike
= (ike_mobike_t
*)task
;
357 enumerator
->destroy(enumerator
);
360 if (!mobike
|| !mobike
->is_probing(mobike
))
363 double retransmit_timeout
= RETRANSMIT_TIMEOUT
;
364 double base
= RETRANSMIT_BASE
;
365 u_int max_tries
= RETRANSMIT_TRIES
;
367 peer_cfg_t
*peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
370 if (this->ike_sa
->is_handover(this->ike_sa
))
372 retransmit_timeout
= peer_cfg
->get_retransmit_timeout_handover(peer_cfg
);
373 base
= peer_cfg
->get_retransmit_base_handover(peer_cfg
);
374 max_tries
= peer_cfg
->get_retransmit_retries_handover(peer_cfg
);
378 retransmit_timeout
= peer_cfg
->get_retransmit_timeout(peer_cfg
);
379 base
= peer_cfg
->get_retransmit_base(peer_cfg
);
380 max_tries
= peer_cfg
->get_retransmit_retries(peer_cfg
);
383 DBG1(DBG_IKE
, "Retries: %d, range: %d\n", this->initiating
.retransmitted
, max_tries
);
385 if (this->initiating
.retransmitted
<= max_tries
)
387 timeout
= (u_int32_t
)(retransmit_timeout
* 1000.0 *
388 pow(base
, this->initiating
.retransmitted
));
391 if (this->initiating
.retransmitted
<= this->retransmit_tries
)
393 timeout
= (uint32_t)(this->retransmit_timeout
* 1000.0 *
394 pow(this->retransmit_base
, this->initiating
.retransmitted
));
395 if (this->retransmit_limit
)
397 timeout
= min(timeout
, this->retransmit_limit
);
399 if (this->retransmit_jitter
)
401 max_jitter
= (timeout
/ 100.0) * this->retransmit_jitter
;
402 timeout
-= max_jitter
* (random() / (RAND_MAX
+ 1.0));
408 DBG1(DBG_IKE
, "giving up after %d retransmits",
409 this->initiating
.retransmitted
- 1);
410 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
,
415 if (this->initiating
.retransmitted
)
417 DBG1(DBG_IKE
, "retransmit %d of request with message ID %d",
418 this->initiating
.retransmitted
, message_id
);
419 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
, packet
,
420 this->initiating
.retransmitted
);
424 send_packets(this, this->initiating
.packets
,
425 this->ike_sa
->get_my_host(this->ike_sa
),
426 this->ike_sa
->get_other_host(this->ike_sa
));
430 if (!mobike
->transmit(mobike
, packet
))
432 DBG1(DBG_IKE
, "no route found to reach peer, MOBIKE update "
435 /* do not try to wait next attempt if we are already done and all failed */
436 if (this->initiating
.retransmitted
>= max_tries
)
438 DBG1(DBG_IKE
, "giving up after %d retransmits",
439 this->initiating
.retransmitted
);
440 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
,
445 this->ike_sa
->set_condition(this->ike_sa
, COND_STALE
, TRUE
);
446 this->initiating
.deferred
= TRUE
;
449 else if (mobike
->is_probing(mobike
))
451 timeout
= ROUTEABILITY_CHECK_INTERVAL
;
456 { /* for routeability checks, we use a more aggressive behavior */
457 if (this->initiating
.retransmitted
<= ROUTEABILITY_CHECK_TRIES
)
459 timeout
= ROUTEABILITY_CHECK_INTERVAL
;
463 DBG1(DBG_IKE
, "giving up after %d path probings",
464 this->initiating
.retransmitted
- 1);
468 if (this->initiating
.retransmitted
)
470 DBG1(DBG_IKE
, "path probing attempt %d",
471 this->initiating
.retransmitted
);
473 /* TODO-FRAG: presumably these small packets are not fragmented,
474 * we should maybe ensure this is the case when generating them */
475 if (!mobike
->transmit(mobike
, packet
))
477 DBG1(DBG_IKE
, "no route found to reach peer, path probing "
479 this->ike_sa
->set_condition(this->ike_sa
, COND_STALE
, TRUE
);
480 this->initiating
.deferred
= TRUE
;
485 this->initiating
.retransmitted
++;
486 job
= (job_t
*)retransmit_job_create(this->initiating
.mid
,
487 this->ike_sa
->get_id(this->ike_sa
));
488 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, timeout
);
493 METHOD(task_manager_t
, initiate
, status_t
,
494 private_task_manager_t
*this)
496 enumerator_t
*enumerator
;
500 exchange_type_t exchange
= 0;
502 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
)
504 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
505 exchange_type_names
, this->initiating
.type
);
506 /* do not initiate if we already have a message in the air */
507 if (this->initiating
.deferred
)
508 { /* re-initiate deferred exchange */
509 this->initiating
.deferred
= FALSE
;
510 this->initiating
.retransmitted
= 0;
511 return retransmit(this, this->initiating
.mid
);
516 if (array_count(this->active_tasks
) == 0)
518 DBG2(DBG_IKE
, "activating new tasks");
519 switch (this->ike_sa
->get_state(this->ike_sa
))
522 activate_task(this, TASK_IKE_VENDOR
);
523 if (activate_task(this, TASK_IKE_INIT
))
525 this->initiating
.mid
= 0;
526 exchange
= IKE_SA_INIT
;
527 activate_task(this, TASK_IKE_NATD
);
528 activate_task(this, TASK_IKE_CERT_PRE
);
530 /* this task has to be activated before the TASK_IKE_AUTH
531 * task, because that task pregenerates the packet after
532 * which no payloads can be added to the message anymore.
534 activate_task(this, TASK_IKE_ME
);
536 activate_task(this, TASK_IKE_AUTH
);
537 activate_task(this, TASK_IKE_CERT_POST
);
538 activate_task(this, TASK_IKE_CONFIG
);
539 activate_task(this, TASK_CHILD_CREATE
);
540 activate_task(this, TASK_IKE_AUTH_LIFETIME
);
541 activate_task(this, TASK_IKE_MOBIKE
);
544 case IKE_ESTABLISHED
:
545 if (activate_task(this, TASK_IKE_MOBIKE
))
547 exchange
= INFORMATIONAL
;
550 if (activate_task(this, TASK_IKE_DELETE
))
552 exchange
= INFORMATIONAL
;
555 if (activate_task(this, TASK_IKE_REDIRECT
))
557 exchange
= INFORMATIONAL
;
560 if (activate_task(this, TASK_CHILD_DELETE
))
562 exchange
= INFORMATIONAL
;
565 if (activate_task(this, TASK_IKE_REAUTH
))
567 exchange
= INFORMATIONAL
;
570 if (activate_task(this, TASK_CHILD_CREATE
))
572 exchange
= CREATE_CHILD_SA
;
575 if (activate_task(this, TASK_CHILD_REKEY
))
577 exchange
= CREATE_CHILD_SA
;
580 if (activate_task(this, TASK_IKE_REKEY
))
582 exchange
= CREATE_CHILD_SA
;
585 if (activate_task(this, TASK_IKE_DPD
))
587 exchange
= INFORMATIONAL
;
590 if (activate_task(this, TASK_IKE_AUTH_LIFETIME
))
592 exchange
= INFORMATIONAL
;
596 if (activate_task(this, TASK_IKE_ME
))
598 exchange
= ME_CONNECT
;
602 if (activate_task(this, TASK_IKE_REAUTH_COMPLETE
))
604 exchange
= INFORMATIONAL
;
607 if (activate_task(this, TASK_IKE_VERIFY_PEER_CERT
))
609 exchange
= INFORMATIONAL
;
614 if (activate_task(this, TASK_IKE_DELETE
))
616 exchange
= INFORMATIONAL
;
626 DBG2(DBG_IKE
, "reinitiating already active tasks");
627 enumerator
= array_create_enumerator(this->active_tasks
);
628 while (enumerator
->enumerate(enumerator
, &task
))
630 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
631 switch (task
->get_type(task
))
634 exchange
= IKE_SA_INIT
;
639 case TASK_CHILD_CREATE
:
640 case TASK_CHILD_REKEY
:
642 exchange
= CREATE_CHILD_SA
;
644 case TASK_IKE_MOBIKE
:
645 exchange
= INFORMATIONAL
;
652 enumerator
->destroy(enumerator
);
657 DBG2(DBG_IKE
, "nothing to initiate");
658 /* nothing to do yet... */
662 me
= this->ike_sa
->get_my_host(this->ike_sa
);
663 other
= this->ike_sa
->get_other_host(this->ike_sa
);
665 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
666 message
->set_message_id(message
, this->initiating
.mid
);
667 message
->set_source(message
, me
->clone(me
));
668 message
->set_destination(message
, other
->clone(other
));
669 message
->set_exchange_type(message
, exchange
);
670 this->initiating
.type
= exchange
;
671 this->initiating
.retransmitted
= 0;
672 this->initiating
.deferred
= FALSE
;
674 enumerator
= array_create_enumerator(this->active_tasks
);
675 while (enumerator
->enumerate(enumerator
, &task
))
677 switch (task
->build(task
, message
))
680 /* task completed, remove it */
681 array_remove_at(this->active_tasks
, enumerator
);
685 /* processed, but task needs another exchange */
689 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
690 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
&&
691 this->ike_sa
->get_state(this->ike_sa
) != IKE_REKEYED
)
693 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
697 /* critical failure, destroy IKE_SA */
698 enumerator
->destroy(enumerator
);
699 message
->destroy(message
);
704 enumerator
->destroy(enumerator
);
706 /* update exchange type if a task changed it */
707 this->initiating
.type
= message
->get_exchange_type(message
);
708 if (this->initiating
.type
== EXCHANGE_TYPE_UNDEFINED
)
710 message
->destroy(message
);
711 return initiate(this);
714 if (!generate_message(this, message
, &this->initiating
.packets
))
716 /* message generation failed. There is nothing more to do than to
718 message
->destroy(message
);
720 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
723 message
->destroy(message
);
725 array_compress(this->active_tasks
);
726 array_compress(this->queued_tasks
);
728 return retransmit(this, this->initiating
.mid
);
732 * handle an incoming response message
734 static status_t
process_response(private_task_manager_t
*this,
737 enumerator_t
*enumerator
;
740 if (message
->get_exchange_type(message
) != this->initiating
.type
)
742 DBG1(DBG_IKE
, "received %N response, but expected %N",
743 exchange_type_names
, message
->get_exchange_type(message
),
744 exchange_type_names
, this->initiating
.type
);
745 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
749 /* handle fatal INVALID_SYNTAX notifies */
750 switch (message
->get_exchange_type(message
))
752 case CREATE_CHILD_SA
:
754 if (message
->get_notify(message
, INVALID_SYNTAX
))
756 DBG1(DBG_IKE
, "received %N notify error, destroying IKE_SA",
757 notify_type_names
, INVALID_SYNTAX
);
758 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
766 enumerator
= array_create_enumerator(this->active_tasks
);
767 while (enumerator
->enumerate(enumerator
, &task
))
769 if (!task
->pre_process
)
773 switch (task
->pre_process(task
, message
))
779 /* just ignore the message */
780 DBG1(DBG_IKE
, "ignore invalid %N response",
781 exchange_type_names
, message
->get_exchange_type(message
));
782 enumerator
->destroy(enumerator
);
785 /* critical failure, destroy IKE_SA */
786 enumerator
->destroy(enumerator
);
790 enumerator
->destroy(enumerator
);
792 if (this->initiating
.retransmitted
> 1)
794 packet_t
*packet
= NULL
;
795 array_get(this->initiating
.packets
, 0, &packet
);
796 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_CLEARED
, packet
);
799 /* catch if we get reset while processing */
801 enumerator
= array_create_enumerator(this->active_tasks
);
802 while (enumerator
->enumerate(enumerator
, &task
))
804 switch (task
->process(task
, message
))
807 /* task completed, remove it */
808 array_remove_at(this->active_tasks
, enumerator
);
812 /* processed, but task needs another exchange */
816 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
819 /* critical failure, destroy IKE_SA */
820 array_remove_at(this->active_tasks
, enumerator
);
821 enumerator
->destroy(enumerator
);
826 { /* start all over again if we were reset */
828 enumerator
->destroy(enumerator
);
829 return initiate(this);
832 enumerator
->destroy(enumerator
);
834 this->initiating
.mid
++;
835 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
836 clear_packets(this->initiating
.packets
);
838 array_compress(this->active_tasks
);
840 return initiate(this);
844 * handle exchange collisions
846 static bool handle_collisions(private_task_manager_t
*this, task_t
*task
)
848 enumerator_t
*enumerator
;
852 type
= task
->get_type(task
);
854 /* do we have to check */
855 if (type
== TASK_IKE_REKEY
|| type
== TASK_CHILD_REKEY
||
856 type
== TASK_CHILD_DELETE
|| type
== TASK_IKE_DELETE
)
858 /* find an exchange collision, and notify these tasks */
859 enumerator
= array_create_enumerator(this->active_tasks
);
860 while (enumerator
->enumerate(enumerator
, &active
))
862 switch (active
->get_type(active
))
865 if (type
== TASK_IKE_REKEY
|| type
== TASK_IKE_DELETE
)
867 ike_rekey_t
*rekey
= (ike_rekey_t
*)active
;
868 rekey
->collide(rekey
, task
);
872 case TASK_CHILD_REKEY
:
873 if (type
== TASK_CHILD_REKEY
|| type
== TASK_CHILD_DELETE
)
875 child_rekey_t
*rekey
= (child_rekey_t
*)active
;
876 rekey
->collide(rekey
, task
);
883 enumerator
->destroy(enumerator
);
886 enumerator
->destroy(enumerator
);
892 * build a response depending on the "passive" task list
894 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
896 enumerator_t
*enumerator
;
900 bool delete = FALSE
, hook
= FALSE
, mid_sync
= FALSE
;
901 ike_sa_id_t
*id
= NULL
;
902 uint64_t responder_spi
= 0;
905 me
= request
->get_destination(request
);
906 other
= request
->get_source(request
);
908 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
909 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
910 /* send response along the path the request came in */
911 message
->set_source(message
, me
->clone(me
));
912 message
->set_destination(message
, other
->clone(other
));
913 message
->set_message_id(message
, this->responding
.mid
);
914 message
->set_request(message
, FALSE
);
916 enumerator
= array_create_enumerator(this->passive_tasks
);
917 while (enumerator
->enumerate(enumerator
, (void*)&task
))
919 if (task
->get_type(task
) == TASK_IKE_MID_SYNC
)
923 switch (task
->build(task
, message
))
926 /* task completed, remove it */
927 array_remove_at(this->passive_tasks
, enumerator
);
928 if (!handle_collisions(this, task
))
934 /* processed, but task needs another exchange */
935 if (handle_collisions(this, task
))
937 array_remove_at(this->passive_tasks
, enumerator
);
945 /* destroy IKE_SA, but SEND response first */
946 if (handle_collisions(this, task
))
948 array_remove_at(this->passive_tasks
, enumerator
);
958 enumerator
->destroy(enumerator
);
960 /* RFC 5996, section 2.6 mentions that in the event of a failure during
961 * IKE_SA_INIT the responder's SPI will be 0 in the response, while it
962 * actually explicitly allows it to be non-zero. Since we use the responder
963 * SPI to create hashes in the IKE_SA manager we can only set the SPI to
964 * zero temporarily, otherwise checking the SA in would fail. */
965 if (delete && request
->get_exchange_type(request
) == IKE_SA_INIT
)
967 id
= this->ike_sa
->get_id(this->ike_sa
);
968 responder_spi
= id
->get_responder_spi(id
);
969 id
->set_responder_spi(id
, 0);
972 /* message complete, send it */
973 clear_packets(this->responding
.packets
);
974 result
= generate_message(this, message
, &this->responding
.packets
);
975 message
->destroy(message
);
978 id
->set_responder_spi(id
, responder_spi
);
982 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
986 send_packets(this, this->responding
.packets
, NULL
, NULL
);
991 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
997 /* we don't want to resend messages to sync MIDs if requests with the
998 * previous MID arrive */
999 clear_packets(this->responding
.packets
);
1000 /* avoid increasing the expected message ID after handling a message
1001 * to sync MIDs with MID 0 */
1005 array_compress(this->passive_tasks
);
1011 * handle an incoming request message
1013 static status_t
process_request(private_task_manager_t
*this,
1016 enumerator_t
*enumerator
;
1017 task_t
*task
= NULL
;
1019 notify_payload_t
*notify
;
1020 delete_payload_t
*delete;
1021 ike_sa_state_t state
;
1023 if (array_count(this->passive_tasks
) == 0)
1024 { /* create tasks depending on request type, if not already some queued */
1025 state
= this->ike_sa
->get_state(this->ike_sa
);
1026 switch (message
->get_exchange_type(message
))
1030 task
= (task_t
*)ike_vendor_create(this->ike_sa
, FALSE
);
1031 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1032 task
= (task_t
*)ike_init_create(this->ike_sa
, FALSE
, NULL
);
1033 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1034 task
= (task_t
*)ike_natd_create(this->ike_sa
, FALSE
);
1035 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1036 task
= (task_t
*)ike_cert_pre_create(this->ike_sa
, FALSE
);
1037 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1039 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
1040 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1042 task
= (task_t
*)ike_auth_create(this->ike_sa
, FALSE
);
1043 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1044 task
= (task_t
*)ike_cert_post_create(this->ike_sa
, FALSE
);
1045 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1046 task
= (task_t
*)ike_config_create(this->ike_sa
, FALSE
);
1047 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1048 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
, FALSE
,
1050 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1051 task
= (task_t
*)ike_auth_lifetime_create(this->ike_sa
, FALSE
);
1052 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1053 task
= (task_t
*)ike_mobike_create(this->ike_sa
, FALSE
);
1054 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1057 case CREATE_CHILD_SA
:
1058 { /* FIXME: we should prevent this on mediation connections */
1059 bool notify_found
= FALSE
, ts_found
= FALSE
;
1061 if (state
== IKE_CREATED
||
1062 state
== IKE_CONNECTING
)
1064 DBG1(DBG_IKE
, "received CREATE_CHILD_SA request for "
1065 "unestablished IKE_SA, rejected");
1069 enumerator
= message
->create_payload_enumerator(message
);
1070 while (enumerator
->enumerate(enumerator
, &payload
))
1072 switch (payload
->get_type(payload
))
1075 { /* if we find a rekey notify, its CHILD_SA rekeying */
1076 notify
= (notify_payload_t
*)payload
;
1077 if (notify
->get_notify_type(notify
) == REKEY_SA
&&
1078 (notify
->get_protocol_id(notify
) == PROTO_AH
||
1079 notify
->get_protocol_id(notify
) == PROTO_ESP
))
1081 notify_found
= TRUE
;
1085 case PLV2_TS_INITIATOR
:
1086 case PLV2_TS_RESPONDER
:
1087 { /* if we don't find a TS, its IKE rekeying */
1095 enumerator
->destroy(enumerator
);
1101 task
= (task_t
*)child_rekey_create(this->ike_sa
,
1106 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
,
1112 task
= (task_t
*)ike_rekey_create(this->ike_sa
, FALSE
);
1114 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1119 enumerator
= message
->create_payload_enumerator(message
);
1120 while (enumerator
->enumerate(enumerator
, &payload
))
1122 switch (payload
->get_type(payload
))
1126 notify
= (notify_payload_t
*)payload
;
1127 if (state
== IKE_REKEYED
)
1129 DBG1(DBG_IKE
, "received unexpected notify %N "
1130 "for rekeyed IKE_SA, ignored",
1132 notify
->get_notify_type(notify
));
1135 switch (notify
->get_notify_type(notify
))
1137 case ADDITIONAL_IP4_ADDRESS
:
1138 case ADDITIONAL_IP6_ADDRESS
:
1139 case NO_ADDITIONAL_ADDRESSES
:
1140 case UPDATE_SA_ADDRESSES
:
1141 case NO_NATS_ALLOWED
:
1142 case UNACCEPTABLE_ADDRESSES
:
1143 case UNEXPECTED_NAT_DETECTED
:
1145 case NAT_DETECTION_SOURCE_IP
:
1146 case NAT_DETECTION_DESTINATION_IP
:
1147 task
= (task_t
*)ike_mobike_create(
1148 this->ike_sa
, FALSE
);
1151 task
= (task_t
*)ike_auth_lifetime_create(
1152 this->ike_sa
, FALSE
);
1154 case AUTHENTICATION_FAILED
:
1155 /* initiator failed to authenticate us.
1156 * We use ike_delete to handle this, which
1157 * invokes all the required hooks. */
1158 task
= (task_t
*)ike_delete_create(
1159 this->ike_sa
, FALSE
);
1162 task
= (task_t
*)ike_redirect_create(
1163 this->ike_sa
, NULL
);
1165 case IKEV2_MESSAGE_ID_SYNC
:
1166 task
= (task_t
*)ike_mid_sync_create(
1176 delete = (delete_payload_t
*)payload
;
1177 if (delete->get_protocol_id(delete) == PROTO_IKE
)
1179 task
= (task_t
*)ike_delete_create(this->ike_sa
,
1184 task
= (task_t
*)child_delete_create(this->ike_sa
,
1185 PROTO_NONE
, 0, FALSE
);
1197 enumerator
->destroy(enumerator
);
1201 task
= (task_t
*)ike_dpd_create(FALSE
);
1203 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1209 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
1210 array_insert(this->passive_tasks
, ARRAY_TAIL
, task
);
1218 enumerator
= array_create_enumerator(this->passive_tasks
);
1219 while (enumerator
->enumerate(enumerator
, &task
))
1221 if (!task
->pre_process
)
1225 switch (task
->pre_process(task
, message
))
1231 /* just ignore the message */
1232 DBG1(DBG_IKE
, "ignore invalid %N request",
1233 exchange_type_names
, message
->get_exchange_type(message
));
1234 enumerator
->destroy(enumerator
);
1235 switch (message
->get_exchange_type(message
))
1238 /* no point in keeping the SA when it was created with
1239 * an invalid IKE_SA_INIT message */
1242 /* remove tasks we queued for this request */
1243 flush_queue(this, TASK_QUEUE_PASSIVE
);
1249 /* critical failure, destroy IKE_SA */
1250 enumerator
->destroy(enumerator
);
1254 enumerator
->destroy(enumerator
);
1256 /* let the tasks process the message */
1257 enumerator
= array_create_enumerator(this->passive_tasks
);
1258 while (enumerator
->enumerate(enumerator
, (void*)&task
))
1260 switch (task
->process(task
, message
))
1263 /* task completed, remove it */
1264 array_remove_at(this->passive_tasks
, enumerator
);
1265 task
->destroy(task
);
1268 /* processed, but task needs at least another call to build() */
1272 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1275 /* critical failure, destroy IKE_SA */
1276 array_remove_at(this->passive_tasks
, enumerator
);
1277 enumerator
->destroy(enumerator
);
1278 task
->destroy(task
);
1282 enumerator
->destroy(enumerator
);
1284 return build_response(this, message
);
1287 METHOD(task_manager_t
, incr_mid
, void,
1288 private_task_manager_t
*this, bool initiate
)
1292 this->initiating
.mid
++;
1296 this->responding
.mid
++;
1300 METHOD(task_manager_t
, get_mid
, uint32_t,
1301 private_task_manager_t
*this, bool initiate
)
1303 return initiate
? this->initiating
.mid
: this->responding
.mid
;
1307 * Handle the given IKE fragment, if it is one.
1309 * Returns SUCCESS if the message is not a fragment, and NEED_MORE if it was
1310 * handled properly. Error states are returned if the fragment was invalid or
1311 * the reassembled message could not have been processed properly.
1313 static status_t
handle_fragment(private_task_manager_t
*this,
1314 message_t
**defrag
, message_t
*msg
)
1316 message_t
*reassembled
;
1319 if (!msg
->get_payload(msg
, PLV2_FRAGMENT
))
1325 *defrag
= message_create_defrag(msg
);
1331 status
= (*defrag
)->add_fragment(*defrag
, msg
);
1332 if (status
== SUCCESS
)
1334 /* reinject the reassembled message */
1335 reassembled
= *defrag
;
1337 status
= this->ike_sa
->process_message(this->ike_sa
, reassembled
);
1338 if (status
== SUCCESS
)
1340 /* avoid processing the last fragment */
1343 reassembled
->destroy(reassembled
);
1349 * Send a notify back to the sender
1351 static void send_notify_response(private_task_manager_t
*this,
1352 message_t
*request
, notify_type_t type
,
1355 message_t
*response
;
1359 response
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
1360 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
1361 response
->set_request(response
, FALSE
);
1362 response
->set_message_id(response
, request
->get_message_id(request
));
1363 response
->add_notify(response
, FALSE
, type
, data
);
1364 me
= this->ike_sa
->get_my_host(this->ike_sa
);
1365 if (me
->is_anyaddr(me
))
1367 me
= request
->get_destination(request
);
1368 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
1370 other
= this->ike_sa
->get_other_host(this->ike_sa
);
1371 if (other
->is_anyaddr(other
))
1373 other
= request
->get_source(request
);
1374 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
1376 response
->set_source(response
, me
->clone(me
));
1377 response
->set_destination(response
, other
->clone(other
));
1378 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
1379 &packet
) == SUCCESS
)
1381 charon
->sender
->send(charon
->sender
, packet
);
1383 response
->destroy(response
);
1387 * Check if a given task has been queued already
1389 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1391 enumerator_t
*enumerator
;
1393 queued_task_t
*queued
;
1395 enumerator
= array_create_enumerator(this->queued_tasks
);
1396 while (enumerator
->enumerate(enumerator
, &queued
))
1398 if (queued
->task
->get_type(queued
->task
) == type
)
1404 enumerator
->destroy(enumerator
);
1409 * Send an INVALID_SYNTAX notify and destroy the IKE_SA for authenticated
1412 static status_t
send_invalid_syntax(private_task_manager_t
*this,
1415 send_notify_response(this, msg
, INVALID_SYNTAX
, chunk_empty
);
1416 incr_mid(this, FALSE
);
1418 /* IKE_SA_INIT is currently the only type the parser accepts unprotected,
1419 * don't destroy the IKE_SA if such a message is invalid */
1420 if (msg
->get_exchange_type(msg
) == IKE_SA_INIT
)
1428 * Parse the given message and verify that it is valid.
1430 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
1432 status_t parse_status
, status
;
1435 parse_status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
1437 if (parse_status
== SUCCESS
)
1438 { /* check for unsupported critical payloads */
1439 enumerator_t
*enumerator
;
1440 unknown_payload_t
*unknown
;
1443 enumerator
= msg
->create_payload_enumerator(msg
);
1444 while (enumerator
->enumerate(enumerator
, &payload
))
1446 if (payload
->get_type(payload
) == PL_UNKNOWN
)
1448 unknown
= (unknown_payload_t
*)payload
;
1449 if (unknown
->is_critical(unknown
))
1451 type
= unknown
->get_type(unknown
);
1452 DBG1(DBG_ENC
, "payload type %N is not supported, "
1453 "but payload is critical!", payload_type_names
, type
);
1454 parse_status
= NOT_SUPPORTED
;
1459 enumerator
->destroy(enumerator
);
1462 status
= parse_status
;
1464 if (parse_status
!= SUCCESS
)
1466 bool is_request
= msg
->get_request(msg
);
1468 switch (parse_status
)
1471 DBG1(DBG_IKE
, "critical unknown payloads found");
1474 send_notify_response(this, msg
,
1475 UNSUPPORTED_CRITICAL_PAYLOAD
,
1476 chunk_from_thing(type
));
1477 incr_mid(this, FALSE
);
1481 DBG1(DBG_IKE
, "message parsing failed");
1484 status
= send_invalid_syntax(this, msg
);
1488 DBG1(DBG_IKE
, "message verification failed");
1491 status
= send_invalid_syntax(this, msg
);
1495 DBG1(DBG_IKE
, "integrity check failed");
1498 if (has_queued(this, TASK_IKE_MOBIKE
))
1500 DBG1(DBG_IKE
, "MOBIKE task was queued");
1501 /* ignore error response for MOBIKE */
1502 notify_payload_t
* notify
= msg
->get_notify(msg
, INVALID_IKE_SPI
);
1505 DBG1(DBG_IKE
, "Ignore path probing attempt error. Server is alive and reachable");
1512 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1516 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1517 exchange_type_names
, msg
->get_exchange_type(msg
),
1518 is_request
? "request" : "response",
1519 msg
->get_message_id(msg
));
1521 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
,
1524 switch (this->ike_sa
->get_state(this->ike_sa
))
1527 /* invalid initiation attempt, close SA */
1528 status
= DESTROY_ME
;
1530 case IKE_CONNECTING
:
1532 /* don't trigger updown event in these states */
1535 if (status
== DESTROY_ME
)
1537 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
1546 * Check if a message with message ID 0 looks like it is used to synchronize
1549 static bool looks_like_mid_sync(private_task_manager_t
*this, message_t
*msg
,
1552 enumerator_t
*enumerator
;
1553 notify_payload_t
*notify
;
1555 bool found
= FALSE
, other
= FALSE
;
1557 if (msg
->get_exchange_type(msg
) == INFORMATIONAL
)
1559 enumerator
= msg
->create_payload_enumerator(msg
);
1560 while (enumerator
->enumerate(enumerator
, &payload
))
1562 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
1564 notify
= (notify_payload_t
*)payload
;
1565 switch (notify
->get_notify_type(notify
))
1567 case IKEV2_MESSAGE_ID_SYNC
:
1568 case IPSEC_REPLAY_COUNTER_SYNC
:
1581 enumerator
->destroy(enumerator
);
1583 return found
&& !other
;
1587 * Check whether we should reject the given request message
1589 static inline bool reject_request(private_task_manager_t
*this,
1592 ike_sa_state_t state
;
1593 exchange_type_t type
;
1594 ike_sa_id_t
*ike_sa_id
;
1595 bool reject
= FALSE
;
1597 state
= this->ike_sa
->get_state(this->ike_sa
);
1598 type
= msg
->get_exchange_type(msg
);
1600 /* reject initial messages if not received in specific states */
1604 reject
= state
!= IKE_CREATED
;
1607 reject
= state
!= IKE_CONNECTING
;
1617 /* after rekeying we only expect a DELETE in an INFORMATIONAL */
1619 reject
= type
!= INFORMATIONAL
;
1621 /* also reject requests for half-open IKE_SAs as initiator */
1623 case IKE_CONNECTING
:
1624 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1625 reject
= ike_sa_id
->is_initiator(ike_sa_id
);
1634 DBG1(DBG_IKE
, "ignoring %N in IKE_SA state %N", exchange_type_names
,
1635 type
, ike_sa_state_names
, state
);
1640 * Check if a message with message ID 0 looks like it is used to synchronize
1641 * the message IDs and we are prepared to process it.
1643 * Note: This is not called if the responder never sent a message before (i.e.
1646 static bool is_mid_sync(private_task_manager_t
*this, message_t
*msg
)
1648 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_ESTABLISHED
&&
1649 this->ike_sa
->supports_extension(this->ike_sa
,
1650 EXT_IKE_MESSAGE_ID_SYNC
))
1652 return looks_like_mid_sync(this, msg
, TRUE
);
1657 METHOD(task_manager_t
, process_message
, status_t
,
1658 private_task_manager_t
*this, message_t
*msg
)
1663 bool schedule_delete_job
= FALSE
;
1665 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1666 status
= parse_message(this, msg
);
1667 if (status
!= SUCCESS
)
1672 me
= msg
->get_destination(msg
);
1673 other
= msg
->get_source(msg
);
1675 /* if this IKE_SA is virgin, we check for a config */
1676 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1680 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1682 if (ike_cfg
== NULL
)
1684 /* no config found for these hosts, destroy */
1685 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1686 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1687 send_notify_response(this, msg
,
1688 NO_PROPOSAL_CHOSEN
, chunk_empty
);
1692 /* deleting IKE_SA without config can cause crash, return success */
1696 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1697 ike_cfg
->destroy(ike_cfg
);
1698 /* add a timeout if peer does not establish it completely */
1699 schedule_delete_job
= TRUE
;
1702 mid
= msg
->get_message_id(msg
);
1703 if (msg
->get_request(msg
))
1705 if (mid
== this->responding
.mid
|| (mid
== 0 && is_mid_sync(this, msg
)))
1707 if (reject_request(this, msg
))
1711 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1712 { /* with MOBIKE, we do no implicit updates */
1713 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, mid
== 1);
1715 status
= handle_fragment(this, &this->responding
.defrag
, msg
);
1716 if (status
!= SUCCESS
)
1718 if (status
== NEED_MORE
)
1720 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1721 time_monotonic(NULL
));
1725 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1726 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1727 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1730 switch (process_request(this, msg
))
1733 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1734 time_monotonic(NULL
));
1735 this->responding
.mid
++;
1744 else if ((mid
== this->responding
.mid
- 1) &&
1745 array_count(this->responding
.packets
) &&
1746 !(mid
== 0 && looks_like_mid_sync(this, msg
, FALSE
)))
1748 status
= handle_fragment(this, &this->responding
.defrag
, msg
);
1749 if (status
!= SUCCESS
)
1751 if (status
== NEED_MORE
)
1753 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1754 time_monotonic(NULL
));
1758 DBG1(DBG_IKE
, "received retransmit of request with ID %d, "
1759 "retransmitting response", mid
);
1760 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1761 time_monotonic(NULL
));
1762 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1763 send_packets(this, this->responding
.packets
,
1764 msg
->get_destination(msg
), msg
->get_source(msg
));
1768 DBG1(DBG_IKE
, "received message ID %d, expected %d, ignored",
1769 mid
, this->responding
.mid
);
1774 if (mid
== this->initiating
.mid
)
1776 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1777 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1778 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1779 { /* only do updates based on verified messages (or initial ones) */
1780 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1781 { /* with MOBIKE, we do no implicit updates. we force an
1782 * update of the local address on IKE_SA_INIT, but never
1783 * for the remote address */
1784 this->ike_sa
->update_hosts(this->ike_sa
, me
, NULL
, mid
== 0);
1785 this->ike_sa
->update_hosts(this->ike_sa
, NULL
, other
, FALSE
);
1788 status
= handle_fragment(this, &this->initiating
.defrag
, msg
);
1789 if (status
!= SUCCESS
)
1791 if (status
== NEED_MORE
)
1793 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1794 time_monotonic(NULL
));
1798 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1799 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1800 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1803 if (process_response(this, msg
) != SUCCESS
)
1808 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1809 time_monotonic(NULL
));
1813 DBG1(DBG_IKE
, "received message ID %d, expected %d, ignored",
1814 mid
, this->initiating
.mid
);
1819 if (schedule_delete_job
)
1821 ike_sa_id_t
*ike_sa_id
;
1824 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1825 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1826 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1827 lib
->settings
->get_int(lib
->settings
,
1828 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1834 METHOD(task_manager_t
, queue_task_delayed
, void,
1835 private_task_manager_t
*this, task_t
*task
, uint32_t delay
)
1837 queued_task_t
*queued
;
1840 time_monotonic(&time
);
1845 DBG2(DBG_IKE
, "queueing %N task (delayed by %us)", task_type_names
,
1846 task
->get_type(task
), delay
);
1847 time
.tv_sec
+= delay
;
1849 job
= (job_t
*)initiate_tasks_job_create(
1850 this->ike_sa
->get_id(this->ike_sa
));
1851 lib
->scheduler
->schedule_job_tv(lib
->scheduler
, job
, time
);
1855 DBG2(DBG_IKE
, "queueing %N task", task_type_names
,
1856 task
->get_type(task
));
1862 array_insert(this->queued_tasks
, ARRAY_TAIL
, queued
);
1865 METHOD(task_manager_t
, queue_task
, void,
1866 private_task_manager_t
*this, task_t
*task
)
1868 queue_task_delayed(this, task
, 0);
1871 METHOD(task_manager_t
, queue_ike
, void,
1872 private_task_manager_t
*this)
1874 if (!has_queued(this, TASK_IKE_VENDOR
))
1876 queue_task(this, (task_t
*)ike_vendor_create(this->ike_sa
, TRUE
));
1878 if (!has_queued(this, TASK_IKE_INIT
))
1880 queue_task(this, (task_t
*)ike_init_create(this->ike_sa
, TRUE
, NULL
));
1882 if (!has_queued(this, TASK_IKE_NATD
))
1884 queue_task(this, (task_t
*)ike_natd_create(this->ike_sa
, TRUE
));
1886 if (!has_queued(this, TASK_IKE_CERT_PRE
))
1888 queue_task(this, (task_t
*)ike_cert_pre_create(this->ike_sa
, TRUE
));
1890 if (!has_queued(this, TASK_IKE_AUTH
))
1892 queue_task(this, (task_t
*)ike_auth_create(this->ike_sa
, TRUE
));
1894 if (!has_queued(this, TASK_IKE_CERT_POST
))
1896 queue_task(this, (task_t
*)ike_cert_post_create(this->ike_sa
, TRUE
));
1898 if (!has_queued(this, TASK_IKE_CONFIG
))
1900 queue_task(this, (task_t
*)ike_config_create(this->ike_sa
, TRUE
));
1902 if (!has_queued(this, TASK_IKE_AUTH_LIFETIME
))
1904 queue_task(this, (task_t
*)ike_auth_lifetime_create(this->ike_sa
, TRUE
));
1906 if (!has_queued(this, TASK_IKE_MOBIKE
))
1908 peer_cfg_t
*peer_cfg
;
1910 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1911 if (peer_cfg
->use_mobike(peer_cfg
))
1913 queue_task(this, (task_t
*)ike_mobike_create(this->ike_sa
, TRUE
));
1917 if (!has_queued(this, TASK_IKE_ME
))
1919 queue_task(this, (task_t
*)ike_me_create(this->ike_sa
, TRUE
));
1924 METHOD(task_manager_t
, queue_ike_rekey
, void,
1925 private_task_manager_t
*this)
1927 queue_task(this, (task_t
*)ike_rekey_create(this->ike_sa
, TRUE
));
1931 * Start reauthentication using make-before-break
1933 static void trigger_mbb_reauth(private_task_manager_t
*this)
1935 enumerator_t
*enumerator
;
1936 child_sa_t
*child_sa
;
1941 queued_task_t
*queued
;
1942 bool children
= FALSE
;
1944 new = charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
1945 this->ike_sa
->get_version(this->ike_sa
), TRUE
);
1947 { /* shouldn't happen */
1951 peer
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1952 new->set_peer_cfg(new, peer
);
1953 host
= this->ike_sa
->get_other_host(this->ike_sa
);
1954 new->set_other_host(new, host
->clone(host
));
1955 host
= this->ike_sa
->get_my_host(this->ike_sa
);
1956 new->set_my_host(new, host
->clone(host
));
1957 enumerator
= this->ike_sa
->create_virtual_ip_enumerator(this->ike_sa
, TRUE
);
1958 while (enumerator
->enumerate(enumerator
, &host
))
1960 new->add_virtual_ip(new, TRUE
, host
);
1962 enumerator
->destroy(enumerator
);
1964 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
1965 while (enumerator
->enumerate(enumerator
, &child_sa
))
1967 child_create_t
*child_create
;
1969 switch (child_sa
->get_state(child_sa
))
1973 /* ignore CHILD_SAs in these states */
1978 cfg
= child_sa
->get_config(child_sa
);
1979 child_create
= child_create_create(new, cfg
->get_ref(cfg
),
1981 child_create
->use_reqid(child_create
, child_sa
->get_reqid(child_sa
));
1982 child_create
->use_marks(child_create
,
1983 child_sa
->get_mark(child_sa
, TRUE
).value
,
1984 child_sa
->get_mark(child_sa
, FALSE
).value
);
1985 /* interface IDs are not migrated as the new CHILD_SAs on old and new
1986 * IKE_SA go though regular updown events */
1987 new->queue_task(new, &child_create
->task
);
1990 enumerator
->destroy(enumerator
);
1992 enumerator
= array_create_enumerator(this->queued_tasks
);
1993 while (enumerator
->enumerate(enumerator
, &queued
))
1995 if (queued
->task
->get_type(queued
->task
) == TASK_CHILD_CREATE
)
1997 queued
->task
->migrate(queued
->task
, new);
1998 new->queue_task(new, queued
->task
);
1999 array_remove_at(this->queued_tasks
, enumerator
);
2004 enumerator
->destroy(enumerator
);
2008 /* allow reauth of mediation connections without CHILD_SAs */
2009 && !peer
->is_mediation(peer
)
2013 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
2014 DBG1(DBG_IKE
, "unable to reauthenticate IKE_SA, no CHILD_SA "
2019 /* suspend online revocation checking until the SA is established */
2020 new->set_condition(new, COND_ONLINE_VALIDATION_SUSPENDED
, TRUE
);
2022 if (new->initiate(new, NULL
, 0, NULL
, NULL
) != DESTROY_ME
)
2024 new->queue_task(new, (task_t
*)ike_verify_peer_cert_create(new));
2025 new->queue_task(new, (task_t
*)ike_reauth_complete_create(new,
2026 this->ike_sa
->get_id(this->ike_sa
)));
2027 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, new);
2031 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
, new);
2032 DBG1(DBG_IKE
, "reauthenticating IKE_SA failed");
2034 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
2037 METHOD(task_manager_t
, queue_ike_reauth
, void,
2038 private_task_manager_t
*this)
2040 if (this->make_before_break
)
2042 return trigger_mbb_reauth(this);
2044 queue_task(this, (task_t
*)ike_reauth_create(this->ike_sa
));
2047 METHOD(task_manager_t
, queue_ike_delete
, void,
2048 private_task_manager_t
*this)
2050 queue_task(this, (task_t
*)ike_delete_create(this->ike_sa
, TRUE
));
2054 * There is no need to queue more than one mobike task, so this either returns
2055 * an already queued task or queues one if there is none yet.
2057 static ike_mobike_t
*queue_mobike_task(private_task_manager_t
*this)
2059 enumerator_t
*enumerator
;
2060 queued_task_t
*queued
;
2061 ike_mobike_t
*mobike
= NULL
;
2063 enumerator
= array_create_enumerator(this->queued_tasks
);
2064 while (enumerator
->enumerate(enumerator
, &queued
))
2066 if (queued
->task
->get_type(queued
->task
) == TASK_IKE_MOBIKE
)
2068 mobike
= (ike_mobike_t
*)queued
->task
;
2072 enumerator
->destroy(enumerator
);
2076 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
2077 queue_task(this, &mobike
->task
);
2082 METHOD(task_manager_t
, queue_mobike
, void,
2083 private_task_manager_t
*this, bool roam
, bool address
)
2085 ike_mobike_t
*mobike
;
2087 mobike
= queue_mobike_task(this);
2090 enumerator_t
*enumerator
;
2093 mobike
->roam(mobike
, address
);
2095 /* enable path probing for a currently active MOBIKE task. This might
2096 * not be the case if an address appeared on a new interface while the
2097 * current address is not working but has not yet disappeared. */
2098 enumerator
= array_create_enumerator(this->active_tasks
);
2099 while (enumerator
->enumerate(enumerator
, ¤t
))
2101 if (current
->get_type(current
) == TASK_IKE_MOBIKE
)
2103 ike_mobike_t
*active
= (ike_mobike_t
*)current
;
2104 active
->enable_probing(active
);
2108 enumerator
->destroy(enumerator
);
2112 mobike
->addresses(mobike
);
2116 METHOD(task_manager_t
, queue_dpd
, void,
2117 private_task_manager_t
*this)
2119 ike_mobike_t
*mobike
;
2121 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
2124 peer_cfg_t
*cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
2125 if (cfg
->get_peer_id(cfg
) ||
2126 this->ike_sa
->has_condition(this->ike_sa
, COND_ORIGINAL_INITIATOR
))
2128 if (this->ike_sa
->has_condition(this->ike_sa
, COND_ORIGINAL_INITIATOR
))
2131 /* use mobike enabled DPD to detect NAT mapping changes */
2132 mobike
= queue_mobike_task(this);
2133 mobike
->dpd(mobike
);
2137 queue_task(this, (task_t
*)ike_dpd_create(TRUE
));
2140 METHOD(task_manager_t
, queue_child
, void,
2141 private_task_manager_t
*this, child_cfg_t
*cfg
, uint32_t reqid
,
2142 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
2144 child_create_t
*task
;
2146 task
= child_create_create(this->ike_sa
, cfg
, FALSE
, tsi
, tsr
);
2149 task
->use_reqid(task
, reqid
);
2151 queue_task(this, &task
->task
);
2154 METHOD(task_manager_t
, queue_child_rekey
, void,
2155 private_task_manager_t
*this, protocol_id_t protocol
, uint32_t spi
)
2157 queue_task(this, (task_t
*)child_rekey_create(this->ike_sa
, protocol
, spi
));
2160 METHOD(task_manager_t
, queue_child_delete
, void,
2161 private_task_manager_t
*this, protocol_id_t protocol
, uint32_t spi
,
2164 queue_task(this, (task_t
*)child_delete_create(this->ike_sa
,
2165 protocol
, spi
, expired
));
2168 METHOD(task_manager_t
, adopt_tasks
, void,
2169 private_task_manager_t
*this, task_manager_t
*other_public
)
2171 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
2172 queued_task_t
*queued
;
2175 time_monotonic(&now
);
2177 /* move queued tasks from other to this */
2178 while (array_remove(other
->queued_tasks
, ARRAY_TAIL
, &queued
))
2180 DBG2(DBG_IKE
, "migrating %N task", task_type_names
,
2181 queued
->task
->get_type(queued
->task
));
2183 if (queued
->task
->get_type(queued
->task
) == TASK_IKE_DELETE
)
2185 DBG1(DBG_IKE
, "do not migrate TASK_IKE_DELETE");
2189 queued
->task
->migrate(queued
->task
, this->ike_sa
);
2190 /* don't delay tasks on the new IKE_SA */
2192 array_insert(this->queued_tasks
, ARRAY_HEAD
, queued
);
2196 METHOD(task_manager_t
, busy
, bool,
2197 private_task_manager_t
*this)
2199 return array_count(this->active_tasks
) > 0;
2202 METHOD(task_manager_t
, reset
, void,
2203 private_task_manager_t
*this, uint32_t initiate
, uint32_t respond
)
2205 enumerator_t
*enumerator
;
2206 queued_task_t
*queued
;
2210 /* reset message counters and retransmit packets */
2211 clear_packets(this->responding
.packets
);
2212 clear_packets(this->initiating
.packets
);
2213 DESTROY_IF(this->responding
.defrag
);
2214 DESTROY_IF(this->initiating
.defrag
);
2215 this->responding
.defrag
= NULL
;
2216 this->initiating
.defrag
= NULL
;
2217 if (initiate
!= UINT_MAX
)
2219 this->initiating
.mid
= initiate
;
2221 if (respond
!= UINT_MAX
)
2223 this->responding
.mid
= respond
;
2225 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
2227 time_monotonic(&now
);
2228 /* reset queued tasks */
2229 enumerator
= array_create_enumerator(this->queued_tasks
);
2230 while (enumerator
->enumerate(enumerator
, &queued
))
2233 queued
->task
->migrate(queued
->task
, this->ike_sa
);
2235 enumerator
->destroy(enumerator
);
2237 /* reset active tasks */
2238 while (array_remove(this->active_tasks
, ARRAY_TAIL
, &task
))
2240 task
->migrate(task
, this->ike_sa
);
2245 array_insert(this->queued_tasks
, ARRAY_HEAD
, queued
);
2252 * Data for a task queue enumerator
2255 enumerator_t
public;
2257 enumerator_t
*inner
;
2258 queued_task_t
*queued
;
2259 } task_enumerator_t
;
2261 METHOD(enumerator_t
, task_enumerator_destroy
, void,
2262 task_enumerator_t
*this)
2264 this->inner
->destroy(this->inner
);
2268 METHOD(enumerator_t
, task_enumerator_enumerate
, bool,
2269 task_enumerator_t
*this, va_list args
)
2273 VA_ARGS_VGET(args
, task
);
2274 if (this->queue
== TASK_QUEUE_QUEUED
)
2276 if (this->inner
->enumerate(this->inner
, &this->queued
))
2278 *task
= this->queued
->task
;
2282 else if (this->inner
->enumerate(this->inner
, task
))
2289 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
2290 private_task_manager_t
*this, task_queue_t queue
)
2292 task_enumerator_t
*enumerator
;
2296 .enumerate
= enumerator_enumerate_default
,
2297 .venumerate
= _task_enumerator_enumerate
,
2298 .destroy
= _task_enumerator_destroy
,
2304 case TASK_QUEUE_ACTIVE
:
2305 enumerator
->inner
= array_create_enumerator(this->active_tasks
);
2307 case TASK_QUEUE_PASSIVE
:
2308 enumerator
->inner
= array_create_enumerator(this->passive_tasks
);
2310 case TASK_QUEUE_QUEUED
:
2311 enumerator
->inner
= array_create_enumerator(this->queued_tasks
);
2314 enumerator
->inner
= enumerator_create_empty();
2317 return &enumerator
->public;
2320 METHOD(task_manager_t
, remove_task
, void,
2321 private_task_manager_t
*this, enumerator_t
*enumerator_public
)
2323 task_enumerator_t
*enumerator
= (task_enumerator_t
*)enumerator_public
;
2325 switch (enumerator
->queue
)
2327 case TASK_QUEUE_ACTIVE
:
2328 array_remove_at(this->active_tasks
, enumerator
->inner
);
2330 case TASK_QUEUE_PASSIVE
:
2331 array_remove_at(this->passive_tasks
, enumerator
->inner
);
2333 case TASK_QUEUE_QUEUED
:
2334 array_remove_at(this->queued_tasks
, enumerator
->inner
);
2335 free(enumerator
->queued
);
2336 enumerator
->queued
= NULL
;
2343 METHOD(task_manager_t
, destroy
, void,
2344 private_task_manager_t
*this)
2348 array_destroy(this->active_tasks
);
2349 array_destroy(this->queued_tasks
);
2350 array_destroy(this->passive_tasks
);
2352 clear_packets(this->responding
.packets
);
2353 array_destroy(this->responding
.packets
);
2354 clear_packets(this->initiating
.packets
);
2355 array_destroy(this->initiating
.packets
);
2356 DESTROY_IF(this->responding
.defrag
);
2357 DESTROY_IF(this->initiating
.defrag
);
2364 task_manager_v2_t
*task_manager_v2_create(ike_sa_t
*ike_sa
)
2366 private_task_manager_t
*this;
2371 .process_message
= _process_message
,
2372 .queue_task
= _queue_task
,
2373 .queue_task_delayed
= _queue_task_delayed
,
2374 .queue_ike
= _queue_ike
,
2375 .queue_ike_rekey
= _queue_ike_rekey
,
2376 .queue_ike_reauth
= _queue_ike_reauth
,
2377 .queue_ike_delete
= _queue_ike_delete
,
2378 .queue_mobike
= _queue_mobike
,
2379 .queue_child
= _queue_child
,
2380 .queue_child_rekey
= _queue_child_rekey
,
2381 .queue_child_delete
= _queue_child_delete
,
2382 .queue_dpd
= _queue_dpd
,
2383 .initiate
= _initiate
,
2384 .retransmit
= _retransmit
,
2385 .incr_mid
= _incr_mid
,
2386 .get_mid
= _get_mid
,
2388 .adopt_tasks
= _adopt_tasks
,
2390 .create_task_enumerator
= _create_task_enumerator
,
2391 .remove_task
= _remove_task
,
2393 .flush_queue
= _flush_queue
,
2394 .destroy
= _destroy
,
2398 .initiating
.type
= EXCHANGE_TYPE_UNDEFINED
,
2399 .queued_tasks
= array_create(0, 0),
2400 .active_tasks
= array_create(0, 0),
2401 .passive_tasks
= array_create(0, 0),
2403 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
2404 "%s.retransmit_tries", RETRANSMIT_TRIES
, lib
->ns
),
2405 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
2406 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, lib
->ns
),
2407 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
2408 "%s.retransmit_base", RETRANSMIT_BASE
, lib
->ns
),
2409 .retransmit_jitter
= min(lib
->settings
->get_int(lib
->settings
,
2410 "%s.retransmit_jitter", 0, lib
->ns
), RETRANSMIT_JITTER_MAX
),
2411 .retransmit_limit
= lib
->settings
->get_int(lib
->settings
,
2412 "%s.retransmit_limit", 0, lib
->ns
) * 1000,
2414 .make_before_break
= lib
->settings
->get_bool(lib
->settings
,
2415 "%s.make_before_break", FALSE
, lib
->ns
),
2418 return &this->public;