2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2010 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_v2.h"
22 #include <sa/ikev2/tasks/ike_init.h>
23 #include <sa/ikev2/tasks/ike_natd.h>
24 #include <sa/ikev2/tasks/ike_mobike.h>
25 #include <sa/ikev2/tasks/ike_auth.h>
26 #include <sa/ikev2/tasks/ike_auth_lifetime.h>
27 #include <sa/ikev2/tasks/ike_cert_pre.h>
28 #include <sa/ikev2/tasks/ike_cert_post.h>
29 #include <sa/ikev2/tasks/ike_rekey.h>
30 #include <sa/ikev2/tasks/ike_reauth.h>
31 #include <sa/ikev2/tasks/ike_delete.h>
32 #include <sa/ikev2/tasks/ike_config.h>
33 #include <sa/ikev2/tasks/ike_dpd.h>
34 #include <sa/ikev2/tasks/ike_vendor.h>
35 #include <sa/ikev2/tasks/child_create.h>
36 #include <sa/ikev2/tasks/child_rekey.h>
37 #include <sa/ikev2/tasks/child_delete.h>
38 #include <encoding/payloads/delete_payload.h>
39 #include <encoding/payloads/unknown_payload.h>
40 #include <processing/jobs/retransmit_job.h>
41 #include <processing/jobs/delete_ike_sa_job.h>
44 #include <sa/ikev2/tasks/ike_me.h>
47 typedef struct exchange_t exchange_t
;
50 * An exchange in the air, used do detect and handle retransmission
55 * Message ID used for this transaction
60 * generated packet for retransmission
65 typedef struct private_task_manager_t private_task_manager_t
;
68 * private data of the task manager
70 struct private_task_manager_t
{
75 task_manager_v2_t
public;
78 * associated IKE_SA we are serving
83 * Exchange we are currently handling as responder
87 * Message ID of the exchange
92 * packet for retransmission
99 * Exchange we are currently handling as initiator
103 * Message ID of the exchange
108 * how many times we have retransmitted so far
113 * packet for retransmission
118 * type of the initated exchange
120 exchange_type_t type
;
125 * List of queued tasks not yet in action
127 linked_list_t
*queued_tasks
;
130 * List of active tasks, initiated by ourselve
132 linked_list_t
*active_tasks
;
135 * List of tasks initiated by peer
137 linked_list_t
*passive_tasks
;
140 * the task manager has been reset
145 * Number of times we retransmit messages before giving up
147 u_int retransmit_tries
;
150 * Retransmission timeout
152 double retransmit_timeout
;
155 * Base to calculate retransmission timeout
157 double retransmit_base
;
161 * flush all tasks in the task manager
163 static void flush(private_task_manager_t
*this)
165 this->passive_tasks
->destroy_offset(this->passive_tasks
,
166 offsetof(task_t
, destroy
));
167 this->passive_tasks
= linked_list_create();
168 this->active_tasks
->destroy_offset(this->active_tasks
,
169 offsetof(task_t
, destroy
));
170 this->active_tasks
= linked_list_create();
171 this->queued_tasks
->destroy_offset(this->queued_tasks
,
172 offsetof(task_t
, destroy
));
173 this->queued_tasks
= linked_list_create();
177 * move a task of a specific type from the queue to the active list
179 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
181 enumerator_t
*enumerator
;
185 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
186 while (enumerator
->enumerate(enumerator
, (void**)&task
))
188 if (task
->get_type(task
) == type
)
190 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
191 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
192 this->active_tasks
->insert_last(this->active_tasks
, task
);
197 enumerator
->destroy(enumerator
);
201 METHOD(task_manager_t
, retransmit
, status_t
,
202 private_task_manager_t
*this, u_int32_t message_id
)
204 if (this->initiating
.packet
&& message_id
== this->initiating
.mid
)
208 enumerator_t
*enumerator
;
211 ike_mobike_t
*mobike
= NULL
;
213 /* check if we are retransmitting a MOBIKE routability check */
214 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
215 while (enumerator
->enumerate(enumerator
, (void*)&task
))
217 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
219 mobike
= (ike_mobike_t
*)task
;
220 if (!mobike
->is_probing(mobike
))
227 enumerator
->destroy(enumerator
);
231 if (this->initiating
.retransmitted
<= this->retransmit_tries
)
233 timeout
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
234 pow(this->retransmit_base
, this->initiating
.retransmitted
));
238 DBG1(DBG_IKE
, "giving up after %d retransmits",
239 this->initiating
.retransmitted
- 1);
240 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
242 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
247 if (this->initiating
.retransmitted
)
249 DBG1(DBG_IKE
, "retransmit %d of request with message ID %d",
250 this->initiating
.retransmitted
, message_id
);
252 packet
= this->initiating
.packet
->clone(this->initiating
.packet
);
253 charon
->sender
->send(charon
->sender
, packet
);
256 { /* for routeability checks, we use a more aggressive behavior */
257 if (this->initiating
.retransmitted
<= ROUTEABILITY_CHECK_TRIES
)
259 timeout
= ROUTEABILITY_CHECK_INTERVAL
;
263 DBG1(DBG_IKE
, "giving up after %d path probings",
264 this->initiating
.retransmitted
- 1);
265 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
269 if (this->initiating
.retransmitted
)
271 DBG1(DBG_IKE
, "path probing attempt %d",
272 this->initiating
.retransmitted
);
274 mobike
->transmit(mobike
, this->initiating
.packet
);
277 this->initiating
.retransmitted
++;
278 job
= (job_t
*)retransmit_job_create(this->initiating
.mid
,
279 this->ike_sa
->get_id(this->ike_sa
));
280 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, timeout
);
285 METHOD(task_manager_t
, initiate
, status_t
,
286 private_task_manager_t
*this)
288 enumerator_t
*enumerator
;
293 exchange_type_t exchange
= 0;
295 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
)
297 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
298 exchange_type_names
, this->initiating
.type
);
299 /* do not initiate if we already have a message in the air */
303 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
305 DBG2(DBG_IKE
, "activating new tasks");
306 switch (this->ike_sa
->get_state(this->ike_sa
))
309 activate_task(this, TASK_IKE_VENDOR
);
310 if (activate_task(this, TASK_IKE_INIT
))
312 this->initiating
.mid
= 0;
313 exchange
= IKE_SA_INIT
;
314 activate_task(this, TASK_IKE_NATD
);
315 activate_task(this, TASK_IKE_CERT_PRE
);
317 /* this task has to be activated before the TASK_IKE_AUTH
318 * task, because that task pregenerates the packet after
319 * which no payloads can be added to the message anymore.
321 activate_task(this, TASK_IKE_ME
);
323 activate_task(this, TASK_IKE_AUTH
);
324 activate_task(this, TASK_IKE_CERT_POST
);
325 activate_task(this, TASK_IKE_CONFIG
);
326 activate_task(this, TASK_CHILD_CREATE
);
327 activate_task(this, TASK_IKE_AUTH_LIFETIME
);
328 activate_task(this, TASK_IKE_MOBIKE
);
331 case IKE_ESTABLISHED
:
332 if (activate_task(this, TASK_CHILD_CREATE
))
334 exchange
= CREATE_CHILD_SA
;
337 if (activate_task(this, TASK_CHILD_DELETE
))
339 exchange
= INFORMATIONAL
;
342 if (activate_task(this, TASK_CHILD_REKEY
))
344 exchange
= CREATE_CHILD_SA
;
347 if (activate_task(this, TASK_IKE_DELETE
))
349 exchange
= INFORMATIONAL
;
352 if (activate_task(this, TASK_IKE_REKEY
))
354 exchange
= CREATE_CHILD_SA
;
357 if (activate_task(this, TASK_IKE_REAUTH
))
359 exchange
= INFORMATIONAL
;
362 if (activate_task(this, TASK_IKE_MOBIKE
))
364 exchange
= INFORMATIONAL
;
367 if (activate_task(this, TASK_IKE_DPD
))
369 exchange
= INFORMATIONAL
;
372 if (activate_task(this, TASK_IKE_AUTH_LIFETIME
))
374 exchange
= INFORMATIONAL
;
378 if (activate_task(this, TASK_IKE_ME
))
380 exchange
= ME_CONNECT
;
385 if (activate_task(this, TASK_IKE_DELETE
))
387 exchange
= INFORMATIONAL
;
397 DBG2(DBG_IKE
, "reinitiating already active tasks");
398 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
399 while (enumerator
->enumerate(enumerator
, (void**)&task
))
401 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
402 switch (task
->get_type(task
))
405 exchange
= IKE_SA_INIT
;
410 case TASK_CHILD_CREATE
:
411 case TASK_CHILD_REKEY
:
413 exchange
= CREATE_CHILD_SA
;
415 case TASK_IKE_MOBIKE
:
416 exchange
= INFORMATIONAL
;
423 enumerator
->destroy(enumerator
);
428 DBG2(DBG_IKE
, "nothing to initiate");
429 /* nothing to do yet... */
433 me
= this->ike_sa
->get_my_host(this->ike_sa
);
434 other
= this->ike_sa
->get_other_host(this->ike_sa
);
436 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
437 message
->set_message_id(message
, this->initiating
.mid
);
438 message
->set_source(message
, me
->clone(me
));
439 message
->set_destination(message
, other
->clone(other
));
440 message
->set_exchange_type(message
, exchange
);
441 this->initiating
.type
= exchange
;
442 this->initiating
.retransmitted
= 0;
444 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
445 while (enumerator
->enumerate(enumerator
, (void*)&task
))
447 switch (task
->build(task
, message
))
450 /* task completed, remove it */
451 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
455 /* processed, but task needs another exchange */
459 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
461 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
465 /* critical failure, destroy IKE_SA */
466 enumerator
->destroy(enumerator
);
467 message
->destroy(message
);
472 enumerator
->destroy(enumerator
);
474 /* update exchange type if a task changed it */
475 this->initiating
.type
= message
->get_exchange_type(message
);
477 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
478 &this->initiating
.packet
);
479 if (status
!= SUCCESS
)
481 /* message generation failed. There is nothing more to do than to
483 message
->destroy(message
);
485 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
488 message
->destroy(message
);
490 return retransmit(this, this->initiating
.mid
);
494 * handle an incoming response message
496 static status_t
process_response(private_task_manager_t
*this,
499 enumerator_t
*enumerator
;
502 if (message
->get_exchange_type(message
) != this->initiating
.type
)
504 DBG1(DBG_IKE
, "received %N response, but expected %N",
505 exchange_type_names
, message
->get_exchange_type(message
),
506 exchange_type_names
, this->initiating
.type
);
507 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
511 /* catch if we get resetted while processing */
513 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
514 while (enumerator
->enumerate(enumerator
, (void*)&task
))
516 switch (task
->process(task
, message
))
519 /* task completed, remove it */
520 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
524 /* processed, but task needs another exchange */
528 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
531 /* critical failure, destroy IKE_SA */
532 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
533 enumerator
->destroy(enumerator
);
538 { /* start all over again if we were reset */
540 enumerator
->destroy(enumerator
);
541 return initiate(this);
544 enumerator
->destroy(enumerator
);
546 this->initiating
.mid
++;
547 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
548 this->initiating
.packet
->destroy(this->initiating
.packet
);
549 this->initiating
.packet
= NULL
;
551 return initiate(this);
555 * handle exchange collisions
557 static bool handle_collisions(private_task_manager_t
*this, task_t
*task
)
559 enumerator_t
*enumerator
;
563 type
= task
->get_type(task
);
565 /* do we have to check */
566 if (type
== TASK_IKE_REKEY
|| type
== TASK_CHILD_REKEY
||
567 type
== TASK_CHILD_DELETE
|| type
== TASK_IKE_DELETE
||
568 type
== TASK_IKE_REAUTH
)
570 /* find an exchange collision, and notify these tasks */
571 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
572 while (enumerator
->enumerate(enumerator
, (void**)&active
))
574 switch (active
->get_type(active
))
577 if (type
== TASK_IKE_REKEY
|| type
== TASK_IKE_DELETE
||
578 type
== TASK_IKE_REAUTH
)
580 ike_rekey_t
*rekey
= (ike_rekey_t
*)active
;
581 rekey
->collide(rekey
, task
);
585 case TASK_CHILD_REKEY
:
586 if (type
== TASK_CHILD_REKEY
|| type
== TASK_CHILD_DELETE
)
588 child_rekey_t
*rekey
= (child_rekey_t
*)active
;
589 rekey
->collide(rekey
, task
);
596 enumerator
->destroy(enumerator
);
599 enumerator
->destroy(enumerator
);
605 * build a response depending on the "passive" task list
607 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
609 enumerator_t
*enumerator
;
616 me
= request
->get_destination(request
);
617 other
= request
->get_source(request
);
619 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
620 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
621 /* send response along the path the request came in */
622 message
->set_source(message
, me
->clone(me
));
623 message
->set_destination(message
, other
->clone(other
));
624 message
->set_message_id(message
, this->responding
.mid
);
625 message
->set_request(message
, FALSE
);
627 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
628 while (enumerator
->enumerate(enumerator
, (void*)&task
))
630 switch (task
->build(task
, message
))
633 /* task completed, remove it */
634 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
635 if (!handle_collisions(this, task
))
641 /* processed, but task needs another exchange */
642 if (handle_collisions(this, task
))
644 this->passive_tasks
->remove_at(this->passive_tasks
,
651 /* destroy IKE_SA, but SEND response first */
660 enumerator
->destroy(enumerator
);
662 /* remove resonder SPI if IKE_SA_INIT failed */
663 if (delete && request
->get_exchange_type(request
) == IKE_SA_INIT
)
665 ike_sa_id_t
*id
= this->ike_sa
->get_id(this->ike_sa
);
666 id
->set_responder_spi(id
, 0);
669 /* message complete, send it */
670 DESTROY_IF(this->responding
.packet
);
671 this->responding
.packet
= NULL
;
672 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
673 &this->responding
.packet
);
674 message
->destroy(message
);
675 if (status
!= SUCCESS
)
677 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
681 charon
->sender
->send(charon
->sender
,
682 this->responding
.packet
->clone(this->responding
.packet
));
685 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
692 * handle an incoming request message
694 static status_t
process_request(private_task_manager_t
*this,
697 enumerator_t
*enumerator
;
700 notify_payload_t
*notify
;
701 delete_payload_t
*delete;
703 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0)
704 { /* create tasks depending on request type, if not already some queued */
705 switch (message
->get_exchange_type(message
))
709 task
= (task_t
*)ike_vendor_create(this->ike_sa
, FALSE
);
710 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
711 task
= (task_t
*)ike_init_create(this->ike_sa
, FALSE
, NULL
);
712 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
713 task
= (task_t
*)ike_natd_create(this->ike_sa
, FALSE
);
714 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
715 task
= (task_t
*)ike_cert_pre_create(this->ike_sa
, FALSE
);
716 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
718 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
719 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
721 task
= (task_t
*)ike_auth_create(this->ike_sa
, FALSE
);
722 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
723 task
= (task_t
*)ike_cert_post_create(this->ike_sa
, FALSE
);
724 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
725 task
= (task_t
*)ike_config_create(this->ike_sa
, FALSE
);
726 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
727 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
, FALSE
,
729 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
730 task
= (task_t
*)ike_auth_lifetime_create(this->ike_sa
, FALSE
);
731 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
732 task
= (task_t
*)ike_mobike_create(this->ike_sa
, FALSE
);
733 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
736 case CREATE_CHILD_SA
:
737 { /* FIXME: we should prevent this on mediation connections */
738 bool notify_found
= FALSE
, ts_found
= FALSE
;
739 enumerator
= message
->create_payload_enumerator(message
);
740 while (enumerator
->enumerate(enumerator
, &payload
))
742 switch (payload
->get_type(payload
))
745 { /* if we find a rekey notify, its CHILD_SA rekeying */
746 notify
= (notify_payload_t
*)payload
;
747 if (notify
->get_notify_type(notify
) == REKEY_SA
&&
748 (notify
->get_protocol_id(notify
) == PROTO_AH
||
749 notify
->get_protocol_id(notify
) == PROTO_ESP
))
755 case TRAFFIC_SELECTOR_INITIATOR
:
756 case TRAFFIC_SELECTOR_RESPONDER
:
757 { /* if we don't find a TS, its IKE rekeying */
765 enumerator
->destroy(enumerator
);
771 task
= (task_t
*)child_rekey_create(this->ike_sa
,
776 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
,
782 task
= (task_t
*)ike_rekey_create(this->ike_sa
, FALSE
);
784 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
789 enumerator
= message
->create_payload_enumerator(message
);
790 while (enumerator
->enumerate(enumerator
, &payload
))
792 switch (payload
->get_type(payload
))
796 notify
= (notify_payload_t
*)payload
;
797 switch (notify
->get_notify_type(notify
))
799 case ADDITIONAL_IP4_ADDRESS
:
800 case ADDITIONAL_IP6_ADDRESS
:
801 case NO_ADDITIONAL_ADDRESSES
:
802 case UPDATE_SA_ADDRESSES
:
803 case NO_NATS_ALLOWED
:
804 case UNACCEPTABLE_ADDRESSES
:
805 case UNEXPECTED_NAT_DETECTED
:
807 case NAT_DETECTION_SOURCE_IP
:
808 case NAT_DETECTION_DESTINATION_IP
:
809 task
= (task_t
*)ike_mobike_create(
810 this->ike_sa
, FALSE
);
813 task
= (task_t
*)ike_auth_lifetime_create(
814 this->ike_sa
, FALSE
);
823 delete = (delete_payload_t
*)payload
;
824 if (delete->get_protocol_id(delete) == PROTO_IKE
)
826 task
= (task_t
*)ike_delete_create(this->ike_sa
,
831 task
= (task_t
*)child_delete_create(this->ike_sa
,
832 PROTO_NONE
, 0, FALSE
);
844 enumerator
->destroy(enumerator
);
848 task
= (task_t
*)ike_dpd_create(FALSE
);
850 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
856 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
857 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
865 /* let the tasks process the message */
866 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
867 while (enumerator
->enumerate(enumerator
, (void*)&task
))
869 switch (task
->process(task
, message
))
872 /* task completed, remove it */
873 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
877 /* processed, but task needs at least another call to build() */
881 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
884 /* critical failure, destroy IKE_SA */
885 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
886 enumerator
->destroy(enumerator
);
891 enumerator
->destroy(enumerator
);
893 return build_response(this, message
);
896 METHOD(task_manager_t
, incr_mid
, void,
897 private_task_manager_t
*this, bool initiate
)
901 this->initiating
.mid
++;
905 this->responding
.mid
++;
910 * Send a notify back to the sender
912 static void send_notify_response(private_task_manager_t
*this,
913 message_t
*request
, notify_type_t type
,
920 response
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
921 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
922 response
->set_request(response
, FALSE
);
923 response
->set_message_id(response
, request
->get_message_id(request
));
924 response
->add_notify(response
, FALSE
, type
, data
);
925 me
= this->ike_sa
->get_my_host(this->ike_sa
);
926 if (me
->is_anyaddr(me
))
928 me
= request
->get_destination(request
);
929 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
931 other
= this->ike_sa
->get_other_host(this->ike_sa
);
932 if (other
->is_anyaddr(other
))
934 other
= request
->get_source(request
);
935 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
937 response
->set_source(response
, me
->clone(me
));
938 response
->set_destination(response
, other
->clone(other
));
939 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
942 charon
->sender
->send(charon
->sender
, packet
);
944 response
->destroy(response
);
948 * Parse the given message and verify that it is valid.
950 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
955 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
957 if (status
== SUCCESS
)
958 { /* check for unsupported critical payloads */
959 enumerator_t
*enumerator
;
960 unknown_payload_t
*unknown
;
963 enumerator
= msg
->create_payload_enumerator(msg
);
964 while (enumerator
->enumerate(enumerator
, &payload
))
966 unknown
= (unknown_payload_t
*)payload
;
967 type
= payload
->get_type(payload
);
968 if (!payload_is_known(type
) &&
969 unknown
->is_critical(unknown
))
971 DBG1(DBG_ENC
, "payload type %N is not supported, "
972 "but its critical!", payload_type_names
, type
);
973 status
= NOT_SUPPORTED
;
976 enumerator
->destroy(enumerator
);
979 if (status
!= SUCCESS
)
981 bool is_request
= msg
->get_request(msg
);
986 DBG1(DBG_IKE
, "critical unknown payloads found");
989 send_notify_response(this, msg
,
990 UNSUPPORTED_CRITICAL_PAYLOAD
,
991 chunk_from_thing(type
));
992 incr_mid(this, FALSE
);
996 DBG1(DBG_IKE
, "message parsing failed");
999 send_notify_response(this, msg
,
1000 INVALID_SYNTAX
, chunk_empty
);
1001 incr_mid(this, FALSE
);
1005 DBG1(DBG_IKE
, "message verification failed");
1008 send_notify_response(this, msg
,
1009 INVALID_SYNTAX
, chunk_empty
);
1010 incr_mid(this, FALSE
);
1014 DBG1(DBG_IKE
, "integrity check failed");
1018 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1022 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1023 exchange_type_names
, msg
->get_exchange_type(msg
),
1024 is_request
? "request" : "response",
1025 msg
->get_message_id(msg
));
1027 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1028 { /* invalid initiation attempt, close SA */
1036 METHOD(task_manager_t
, process_message
, status_t
,
1037 private_task_manager_t
*this, message_t
*msg
)
1043 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1044 status
= parse_message(this, msg
);
1045 if (status
!= SUCCESS
)
1050 me
= msg
->get_destination(msg
);
1051 other
= msg
->get_source(msg
);
1053 /* if this IKE_SA is virgin, we check for a config */
1054 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1056 ike_sa_id_t
*ike_sa_id
;
1059 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
, me
, other
);
1060 if (ike_cfg
== NULL
)
1062 /* no config found for these hosts, destroy */
1063 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1064 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1065 send_notify_response(this, msg
,
1066 NO_PROPOSAL_CHOSEN
, chunk_empty
);
1069 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1070 ike_cfg
->destroy(ike_cfg
);
1071 /* add a timeout if peer does not establish it completely */
1072 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1073 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1074 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1075 lib
->settings
->get_int(lib
->settings
,
1076 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
));
1078 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1079 time_monotonic(NULL
));
1081 mid
= msg
->get_message_id(msg
);
1082 if (msg
->get_request(msg
))
1084 if (mid
== this->responding
.mid
)
1086 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1087 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1088 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1089 { /* only do host updates based on verified messages */
1090 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1091 { /* with MOBIKE, we do no implicit updates */
1092 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, mid
== 1);
1095 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1096 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1097 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1100 if (process_request(this, msg
) != SUCCESS
)
1105 this->responding
.mid
++;
1107 else if ((mid
== this->responding
.mid
- 1) && this->responding
.packet
)
1112 DBG1(DBG_IKE
, "received retransmit of request with ID %d, "
1113 "retransmitting response", mid
);
1114 clone
= this->responding
.packet
->clone(this->responding
.packet
);
1115 host
= msg
->get_destination(msg
);
1116 clone
->set_source(clone
, host
->clone(host
));
1117 host
= msg
->get_source(msg
);
1118 clone
->set_destination(clone
, host
->clone(host
));
1119 charon
->sender
->send(charon
->sender
, clone
);
1123 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1124 mid
, this->responding
.mid
);
1129 if (mid
== this->initiating
.mid
)
1131 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1132 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1133 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1134 { /* only do host updates based on verified messages */
1135 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1136 { /* with MOBIKE, we do no implicit updates */
1137 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, FALSE
);
1140 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1141 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1142 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1145 if (process_response(this, msg
) != SUCCESS
)
1153 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1154 mid
, this->initiating
.mid
);
1161 METHOD(task_manager_t
, queue_task
, void,
1162 private_task_manager_t
*this, task_t
*task
)
1164 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
1165 { /* there is no need to queue more than one mobike task */
1166 enumerator_t
*enumerator
;
1169 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1170 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
1172 if (current
->get_type(current
) == TASK_IKE_MOBIKE
)
1174 enumerator
->destroy(enumerator
);
1175 task
->destroy(task
);
1179 enumerator
->destroy(enumerator
);
1181 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1182 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1186 * Check if a given task has been queued already
1188 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1190 enumerator_t
*enumerator
;
1194 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1195 while (enumerator
->enumerate(enumerator
, &task
))
1197 if (task
->get_type(task
) == type
)
1203 enumerator
->destroy(enumerator
);
1207 METHOD(task_manager_t
, queue_ike
, void,
1208 private_task_manager_t
*this)
1210 if (!has_queued(this, TASK_IKE_VENDOR
))
1212 queue_task(this, (task_t
*)ike_vendor_create(this->ike_sa
, TRUE
));
1214 if (!has_queued(this, TASK_IKE_INIT
))
1216 queue_task(this, (task_t
*)ike_init_create(this->ike_sa
, TRUE
, NULL
));
1218 if (!has_queued(this, TASK_IKE_NATD
))
1220 queue_task(this, (task_t
*)ike_natd_create(this->ike_sa
, TRUE
));
1222 if (!has_queued(this, TASK_IKE_CERT_PRE
))
1224 queue_task(this, (task_t
*)ike_cert_pre_create(this->ike_sa
, TRUE
));
1226 if (!has_queued(this, TASK_IKE_AUTH
))
1228 queue_task(this, (task_t
*)ike_auth_create(this->ike_sa
, TRUE
));
1230 if (!has_queued(this, TASK_IKE_CERT_POST
))
1232 queue_task(this, (task_t
*)ike_cert_post_create(this->ike_sa
, TRUE
));
1234 if (!has_queued(this, TASK_IKE_CONFIG
))
1236 queue_task(this, (task_t
*)ike_config_create(this->ike_sa
, TRUE
));
1238 if (!has_queued(this, TASK_IKE_AUTH_LIFETIME
))
1240 queue_task(this, (task_t
*)ike_auth_lifetime_create(this->ike_sa
, TRUE
));
1242 if (!has_queued(this, TASK_IKE_MOBIKE
))
1244 peer_cfg_t
*peer_cfg
;
1246 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1247 if (peer_cfg
->use_mobike(peer_cfg
))
1249 queue_task(this, (task_t
*)ike_mobike_create(this->ike_sa
, TRUE
));
1253 if (!has_queued(this, TASK_IKE_ME
))
1255 queue_task(this, (task_t
*)ike_me_create(this->ike_sa
, TRUE
));
1260 METHOD(task_manager_t
, queue_ike_rekey
, void,
1261 private_task_manager_t
*this)
1263 queue_task(this, (task_t
*)ike_rekey_create(this->ike_sa
, TRUE
));
1266 METHOD(task_manager_t
, queue_ike_reauth
, void,
1267 private_task_manager_t
*this)
1269 queue_task(this, (task_t
*)ike_reauth_create(this->ike_sa
));
1272 METHOD(task_manager_t
, queue_ike_delete
, void,
1273 private_task_manager_t
*this)
1275 queue_task(this, (task_t
*)ike_delete_create(this->ike_sa
, TRUE
));
1278 METHOD(task_manager_t
, queue_mobike
, void,
1279 private_task_manager_t
*this, bool roam
, bool address
)
1281 ike_mobike_t
*mobike
;
1283 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1286 mobike
->roam(mobike
, address
);
1290 mobike
->addresses(mobike
);
1292 queue_task(this, &mobike
->task
);
1295 METHOD(task_manager_t
, queue_child
, void,
1296 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1297 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1299 child_create_t
*task
;
1301 task
= child_create_create(this->ike_sa
, cfg
, FALSE
, tsi
, tsr
);
1304 task
->use_reqid(task
, reqid
);
1306 queue_task(this, &task
->task
);
1309 METHOD(task_manager_t
, queue_child_rekey
, void,
1310 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1312 queue_task(this, (task_t
*)child_rekey_create(this->ike_sa
, protocol
, spi
));
1315 METHOD(task_manager_t
, queue_child_delete
, void,
1316 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
,
1319 queue_task(this, (task_t
*)child_delete_create(this->ike_sa
,
1320 protocol
, spi
, expired
));
1323 METHOD(task_manager_t
, queue_dpd
, void,
1324 private_task_manager_t
*this)
1326 ike_mobike_t
*mobike
;
1328 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
) &&
1329 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_HERE
))
1331 /* use mobike enabled DPD to detect NAT mapping changes */
1332 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1333 mobike
->dpd(mobike
);
1334 queue_task(this, &mobike
->task
);
1338 queue_task(this, (task_t
*)ike_dpd_create(TRUE
));
1342 METHOD(task_manager_t
, adopt_tasks
, void,
1343 private_task_manager_t
*this, task_manager_t
*other_public
)
1345 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1348 /* move queued tasks from other to this */
1349 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1350 (void**)&task
) == SUCCESS
)
1352 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1353 task
->migrate(task
, this->ike_sa
);
1354 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1358 METHOD(task_manager_t
, busy
, bool,
1359 private_task_manager_t
*this)
1361 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1364 METHOD(task_manager_t
, reset
, void,
1365 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1367 enumerator_t
*enumerator
;
1370 /* reset message counters and retransmit packets */
1371 DESTROY_IF(this->responding
.packet
);
1372 DESTROY_IF(this->initiating
.packet
);
1373 this->responding
.packet
= NULL
;
1374 this->initiating
.packet
= NULL
;
1375 if (initiate
!= UINT_MAX
)
1377 this->initiating
.mid
= initiate
;
1379 if (respond
!= UINT_MAX
)
1381 this->responding
.mid
= respond
;
1383 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1385 /* reset queued tasks */
1386 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1387 while (enumerator
->enumerate(enumerator
, &task
))
1389 task
->migrate(task
, this->ike_sa
);
1391 enumerator
->destroy(enumerator
);
1393 /* reset active tasks */
1394 while (this->active_tasks
->remove_last(this->active_tasks
,
1395 (void**)&task
) == SUCCESS
)
1397 task
->migrate(task
, this->ike_sa
);
1398 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1404 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1405 private_task_manager_t
*this, task_queue_t queue
)
1409 case TASK_QUEUE_ACTIVE
:
1410 return this->active_tasks
->create_enumerator(this->active_tasks
);
1411 case TASK_QUEUE_PASSIVE
:
1412 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
1413 case TASK_QUEUE_QUEUED
:
1414 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
1416 return enumerator_create_empty();
1420 METHOD(task_manager_t
, destroy
, void,
1421 private_task_manager_t
*this)
1425 this->active_tasks
->destroy(this->active_tasks
);
1426 this->queued_tasks
->destroy(this->queued_tasks
);
1427 this->passive_tasks
->destroy(this->passive_tasks
);
1429 DESTROY_IF(this->responding
.packet
);
1430 DESTROY_IF(this->initiating
.packet
);
1437 task_manager_v2_t
*task_manager_v2_create(ike_sa_t
*ike_sa
)
1439 private_task_manager_t
*this;
1444 .process_message
= _process_message
,
1445 .queue_task
= _queue_task
,
1446 .queue_ike
= _queue_ike
,
1447 .queue_ike_rekey
= _queue_ike_rekey
,
1448 .queue_ike_reauth
= _queue_ike_reauth
,
1449 .queue_ike_delete
= _queue_ike_delete
,
1450 .queue_mobike
= _queue_mobike
,
1451 .queue_child
= _queue_child
,
1452 .queue_child_rekey
= _queue_child_rekey
,
1453 .queue_child_delete
= _queue_child_delete
,
1454 .queue_dpd
= _queue_dpd
,
1455 .initiate
= _initiate
,
1456 .retransmit
= _retransmit
,
1457 .incr_mid
= _incr_mid
,
1459 .adopt_tasks
= _adopt_tasks
,
1461 .create_task_enumerator
= _create_task_enumerator
,
1462 .destroy
= _destroy
,
1466 .initiating
.type
= EXCHANGE_TYPE_UNDEFINED
,
1467 .queued_tasks
= linked_list_create(),
1468 .active_tasks
= linked_list_create(),
1469 .passive_tasks
= linked_list_create(),
1470 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
1471 "charon.retransmit_tries", RETRANSMIT_TRIES
),
1472 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
1473 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT
),
1474 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
1475 "charon.retransmit_base", RETRANSMIT_BASE
),
1478 return &this->public;