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
;
160 METHOD(task_manager_t
, flush_queue
, void,
161 private_task_manager_t
*this, task_queue_t queue
)
168 case TASK_QUEUE_ACTIVE
:
169 list
= this->active_tasks
;
171 case TASK_QUEUE_PASSIVE
:
172 list
= this->passive_tasks
;
174 case TASK_QUEUE_QUEUED
:
175 list
= this->queued_tasks
;
180 while (list
->remove_last(list
, (void**)&task
) == SUCCESS
)
187 * flush all tasks in the task manager
189 static void flush(private_task_manager_t
*this)
191 flush_queue(this, TASK_QUEUE_QUEUED
);
192 flush_queue(this, TASK_QUEUE_PASSIVE
);
193 flush_queue(this, TASK_QUEUE_ACTIVE
);
197 * move a task of a specific type from the queue to the active list
199 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
201 enumerator_t
*enumerator
;
205 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
206 while (enumerator
->enumerate(enumerator
, (void**)&task
))
208 if (task
->get_type(task
) == type
)
210 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
211 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
212 this->active_tasks
->insert_last(this->active_tasks
, task
);
217 enumerator
->destroy(enumerator
);
221 METHOD(task_manager_t
, retransmit
, status_t
,
222 private_task_manager_t
*this, u_int32_t message_id
)
224 if (this->initiating
.packet
&& message_id
== this->initiating
.mid
)
228 enumerator_t
*enumerator
;
231 ike_mobike_t
*mobike
= NULL
;
233 /* check if we are retransmitting a MOBIKE routability check */
234 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
235 while (enumerator
->enumerate(enumerator
, (void*)&task
))
237 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
239 mobike
= (ike_mobike_t
*)task
;
240 if (!mobike
->is_probing(mobike
))
247 enumerator
->destroy(enumerator
);
251 if (this->initiating
.retransmitted
<= this->retransmit_tries
)
253 timeout
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
254 pow(this->retransmit_base
, this->initiating
.retransmitted
));
258 DBG1(DBG_IKE
, "giving up after %d retransmits",
259 this->initiating
.retransmitted
- 1);
260 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND_TIMEOUT
,
261 this->initiating
.packet
);
265 if (this->initiating
.retransmitted
)
267 DBG1(DBG_IKE
, "retransmit %d of request with message ID %d",
268 this->initiating
.retransmitted
, message_id
);
269 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_SEND
,
270 this->initiating
.packet
);
272 packet
= this->initiating
.packet
->clone(this->initiating
.packet
);
273 charon
->sender
->send(charon
->sender
, packet
);
276 { /* for routeability checks, we use a more aggressive behavior */
277 if (this->initiating
.retransmitted
<= ROUTEABILITY_CHECK_TRIES
)
279 timeout
= ROUTEABILITY_CHECK_INTERVAL
;
283 DBG1(DBG_IKE
, "giving up after %d path probings",
284 this->initiating
.retransmitted
- 1);
288 if (this->initiating
.retransmitted
)
290 DBG1(DBG_IKE
, "path probing attempt %d",
291 this->initiating
.retransmitted
);
293 mobike
->transmit(mobike
, this->initiating
.packet
);
296 this->initiating
.retransmitted
++;
297 job
= (job_t
*)retransmit_job_create(this->initiating
.mid
,
298 this->ike_sa
->get_id(this->ike_sa
));
299 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, timeout
);
304 METHOD(task_manager_t
, initiate
, status_t
,
305 private_task_manager_t
*this)
307 enumerator_t
*enumerator
;
312 exchange_type_t exchange
= 0;
314 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
)
316 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
317 exchange_type_names
, this->initiating
.type
);
318 /* do not initiate if we already have a message in the air */
322 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
324 DBG2(DBG_IKE
, "activating new tasks");
325 switch (this->ike_sa
->get_state(this->ike_sa
))
328 activate_task(this, TASK_IKE_VENDOR
);
329 if (activate_task(this, TASK_IKE_INIT
))
331 this->initiating
.mid
= 0;
332 exchange
= IKE_SA_INIT
;
333 activate_task(this, TASK_IKE_NATD
);
334 activate_task(this, TASK_IKE_CERT_PRE
);
336 /* this task has to be activated before the TASK_IKE_AUTH
337 * task, because that task pregenerates the packet after
338 * which no payloads can be added to the message anymore.
340 activate_task(this, TASK_IKE_ME
);
342 activate_task(this, TASK_IKE_AUTH
);
343 activate_task(this, TASK_IKE_CERT_POST
);
344 activate_task(this, TASK_IKE_CONFIG
);
345 activate_task(this, TASK_CHILD_CREATE
);
346 activate_task(this, TASK_IKE_AUTH_LIFETIME
);
347 activate_task(this, TASK_IKE_MOBIKE
);
350 case IKE_ESTABLISHED
:
351 if (activate_task(this, TASK_CHILD_CREATE
))
353 exchange
= CREATE_CHILD_SA
;
356 if (activate_task(this, TASK_CHILD_DELETE
))
358 exchange
= INFORMATIONAL
;
361 if (activate_task(this, TASK_CHILD_REKEY
))
363 exchange
= CREATE_CHILD_SA
;
366 if (activate_task(this, TASK_IKE_DELETE
))
368 exchange
= INFORMATIONAL
;
371 if (activate_task(this, TASK_IKE_REKEY
))
373 exchange
= CREATE_CHILD_SA
;
376 if (activate_task(this, TASK_IKE_REAUTH
))
378 exchange
= INFORMATIONAL
;
381 if (activate_task(this, TASK_IKE_MOBIKE
))
383 exchange
= INFORMATIONAL
;
386 if (activate_task(this, TASK_IKE_DPD
))
388 exchange
= INFORMATIONAL
;
391 if (activate_task(this, TASK_IKE_AUTH_LIFETIME
))
393 exchange
= INFORMATIONAL
;
397 if (activate_task(this, TASK_IKE_ME
))
399 exchange
= ME_CONNECT
;
404 if (activate_task(this, TASK_IKE_DELETE
))
406 exchange
= INFORMATIONAL
;
416 DBG2(DBG_IKE
, "reinitiating already active tasks");
417 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
418 while (enumerator
->enumerate(enumerator
, (void**)&task
))
420 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
421 switch (task
->get_type(task
))
424 exchange
= IKE_SA_INIT
;
429 case TASK_CHILD_CREATE
:
430 case TASK_CHILD_REKEY
:
432 exchange
= CREATE_CHILD_SA
;
434 case TASK_IKE_MOBIKE
:
435 exchange
= INFORMATIONAL
;
442 enumerator
->destroy(enumerator
);
447 DBG2(DBG_IKE
, "nothing to initiate");
448 /* nothing to do yet... */
452 me
= this->ike_sa
->get_my_host(this->ike_sa
);
453 other
= this->ike_sa
->get_other_host(this->ike_sa
);
455 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
456 message
->set_message_id(message
, this->initiating
.mid
);
457 message
->set_source(message
, me
->clone(me
));
458 message
->set_destination(message
, other
->clone(other
));
459 message
->set_exchange_type(message
, exchange
);
460 this->initiating
.type
= exchange
;
461 this->initiating
.retransmitted
= 0;
463 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
464 while (enumerator
->enumerate(enumerator
, (void*)&task
))
466 switch (task
->build(task
, message
))
469 /* task completed, remove it */
470 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
474 /* processed, but task needs another exchange */
478 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
480 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
484 /* critical failure, destroy IKE_SA */
485 enumerator
->destroy(enumerator
);
486 message
->destroy(message
);
491 enumerator
->destroy(enumerator
);
493 /* update exchange type if a task changed it */
494 this->initiating
.type
= message
->get_exchange_type(message
);
496 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
497 &this->initiating
.packet
);
498 if (status
!= SUCCESS
)
500 /* message generation failed. There is nothing more to do than to
502 message
->destroy(message
);
504 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
507 message
->destroy(message
);
509 return retransmit(this, this->initiating
.mid
);
513 * handle an incoming response message
515 static status_t
process_response(private_task_manager_t
*this,
518 enumerator_t
*enumerator
;
521 if (message
->get_exchange_type(message
) != this->initiating
.type
)
523 DBG1(DBG_IKE
, "received %N response, but expected %N",
524 exchange_type_names
, message
->get_exchange_type(message
),
525 exchange_type_names
, this->initiating
.type
);
526 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
530 /* catch if we get resetted while processing */
532 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
533 while (enumerator
->enumerate(enumerator
, (void*)&task
))
535 switch (task
->process(task
, message
))
538 /* task completed, remove it */
539 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
543 /* processed, but task needs another exchange */
547 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
550 /* critical failure, destroy IKE_SA */
551 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
552 enumerator
->destroy(enumerator
);
557 { /* start all over again if we were reset */
559 enumerator
->destroy(enumerator
);
560 return initiate(this);
563 enumerator
->destroy(enumerator
);
565 this->initiating
.mid
++;
566 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
567 this->initiating
.packet
->destroy(this->initiating
.packet
);
568 this->initiating
.packet
= NULL
;
570 return initiate(this);
574 * handle exchange collisions
576 static bool handle_collisions(private_task_manager_t
*this, task_t
*task
)
578 enumerator_t
*enumerator
;
582 type
= task
->get_type(task
);
584 /* do we have to check */
585 if (type
== TASK_IKE_REKEY
|| type
== TASK_CHILD_REKEY
||
586 type
== TASK_CHILD_DELETE
|| type
== TASK_IKE_DELETE
||
587 type
== TASK_IKE_REAUTH
)
589 /* find an exchange collision, and notify these tasks */
590 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
591 while (enumerator
->enumerate(enumerator
, (void**)&active
))
593 switch (active
->get_type(active
))
596 if (type
== TASK_IKE_REKEY
|| type
== TASK_IKE_DELETE
||
597 type
== TASK_IKE_REAUTH
)
599 ike_rekey_t
*rekey
= (ike_rekey_t
*)active
;
600 rekey
->collide(rekey
, task
);
604 case TASK_CHILD_REKEY
:
605 if (type
== TASK_CHILD_REKEY
|| type
== TASK_CHILD_DELETE
)
607 child_rekey_t
*rekey
= (child_rekey_t
*)active
;
608 rekey
->collide(rekey
, task
);
615 enumerator
->destroy(enumerator
);
618 enumerator
->destroy(enumerator
);
624 * build a response depending on the "passive" task list
626 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
628 enumerator_t
*enumerator
;
632 bool delete = FALSE
, hook
= FALSE
;
633 ike_sa_id_t
*id
= NULL
;
634 u_int64_t responder_spi
;
637 me
= request
->get_destination(request
);
638 other
= request
->get_source(request
);
640 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
641 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
642 /* send response along the path the request came in */
643 message
->set_source(message
, me
->clone(me
));
644 message
->set_destination(message
, other
->clone(other
));
645 message
->set_message_id(message
, this->responding
.mid
);
646 message
->set_request(message
, FALSE
);
648 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
649 while (enumerator
->enumerate(enumerator
, (void*)&task
))
651 switch (task
->build(task
, message
))
654 /* task completed, remove it */
655 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
656 if (!handle_collisions(this, task
))
662 /* processed, but task needs another exchange */
663 if (handle_collisions(this, task
))
665 this->passive_tasks
->remove_at(this->passive_tasks
,
674 /* destroy IKE_SA, but SEND response first */
683 enumerator
->destroy(enumerator
);
685 /* RFC 5996, section 2.6 mentions that in the event of a failure during
686 * IKE_SA_INIT the responder's SPI will be 0 in the response, while it
687 * actually explicitly allows it to be non-zero. Since we use the responder
688 * SPI to create hashes in the IKE_SA manager we can only set the SPI to
689 * zero temporarily, otherwise checking the SA in would fail. */
690 if (delete && request
->get_exchange_type(request
) == IKE_SA_INIT
)
692 id
= this->ike_sa
->get_id(this->ike_sa
);
693 responder_spi
= id
->get_responder_spi(id
);
694 id
->set_responder_spi(id
, 0);
697 /* message complete, send it */
698 DESTROY_IF(this->responding
.packet
);
699 this->responding
.packet
= NULL
;
700 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
701 &this->responding
.packet
);
702 message
->destroy(message
);
705 id
->set_responder_spi(id
, responder_spi
);
707 if (status
!= SUCCESS
)
709 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
713 charon
->sender
->send(charon
->sender
,
714 this->responding
.packet
->clone(this->responding
.packet
));
719 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
727 * handle an incoming request message
729 static status_t
process_request(private_task_manager_t
*this,
732 enumerator_t
*enumerator
;
735 notify_payload_t
*notify
;
736 delete_payload_t
*delete;
738 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0)
739 { /* create tasks depending on request type, if not already some queued */
740 switch (message
->get_exchange_type(message
))
744 task
= (task_t
*)ike_vendor_create(this->ike_sa
, FALSE
);
745 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
746 task
= (task_t
*)ike_init_create(this->ike_sa
, FALSE
, NULL
);
747 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
748 task
= (task_t
*)ike_natd_create(this->ike_sa
, FALSE
);
749 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
750 task
= (task_t
*)ike_cert_pre_create(this->ike_sa
, FALSE
);
751 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
753 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
754 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
756 task
= (task_t
*)ike_auth_create(this->ike_sa
, FALSE
);
757 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
758 task
= (task_t
*)ike_cert_post_create(this->ike_sa
, FALSE
);
759 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
760 task
= (task_t
*)ike_config_create(this->ike_sa
, FALSE
);
761 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
762 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
, FALSE
,
764 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
765 task
= (task_t
*)ike_auth_lifetime_create(this->ike_sa
, FALSE
);
766 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
767 task
= (task_t
*)ike_mobike_create(this->ike_sa
, FALSE
);
768 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
771 case CREATE_CHILD_SA
:
772 { /* FIXME: we should prevent this on mediation connections */
773 bool notify_found
= FALSE
, ts_found
= FALSE
;
774 enumerator
= message
->create_payload_enumerator(message
);
775 while (enumerator
->enumerate(enumerator
, &payload
))
777 switch (payload
->get_type(payload
))
780 { /* if we find a rekey notify, its CHILD_SA rekeying */
781 notify
= (notify_payload_t
*)payload
;
782 if (notify
->get_notify_type(notify
) == REKEY_SA
&&
783 (notify
->get_protocol_id(notify
) == PROTO_AH
||
784 notify
->get_protocol_id(notify
) == PROTO_ESP
))
790 case TRAFFIC_SELECTOR_INITIATOR
:
791 case TRAFFIC_SELECTOR_RESPONDER
:
792 { /* if we don't find a TS, its IKE rekeying */
800 enumerator
->destroy(enumerator
);
806 task
= (task_t
*)child_rekey_create(this->ike_sa
,
811 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
,
817 task
= (task_t
*)ike_rekey_create(this->ike_sa
, FALSE
);
819 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
824 enumerator
= message
->create_payload_enumerator(message
);
825 while (enumerator
->enumerate(enumerator
, &payload
))
827 switch (payload
->get_type(payload
))
831 notify
= (notify_payload_t
*)payload
;
832 switch (notify
->get_notify_type(notify
))
834 case ADDITIONAL_IP4_ADDRESS
:
835 case ADDITIONAL_IP6_ADDRESS
:
836 case NO_ADDITIONAL_ADDRESSES
:
837 case UPDATE_SA_ADDRESSES
:
838 case NO_NATS_ALLOWED
:
839 case UNACCEPTABLE_ADDRESSES
:
840 case UNEXPECTED_NAT_DETECTED
:
842 case NAT_DETECTION_SOURCE_IP
:
843 case NAT_DETECTION_DESTINATION_IP
:
844 task
= (task_t
*)ike_mobike_create(
845 this->ike_sa
, FALSE
);
848 task
= (task_t
*)ike_auth_lifetime_create(
849 this->ike_sa
, FALSE
);
858 delete = (delete_payload_t
*)payload
;
859 if (delete->get_protocol_id(delete) == PROTO_IKE
)
861 task
= (task_t
*)ike_delete_create(this->ike_sa
,
866 task
= (task_t
*)child_delete_create(this->ike_sa
,
867 PROTO_NONE
, 0, FALSE
);
879 enumerator
->destroy(enumerator
);
883 task
= (task_t
*)ike_dpd_create(FALSE
);
885 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
891 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
892 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
900 /* let the tasks process the message */
901 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
902 while (enumerator
->enumerate(enumerator
, (void*)&task
))
904 switch (task
->process(task
, message
))
907 /* task completed, remove it */
908 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
912 /* processed, but task needs at least another call to build() */
916 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
919 /* critical failure, destroy IKE_SA */
920 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
921 enumerator
->destroy(enumerator
);
926 enumerator
->destroy(enumerator
);
928 return build_response(this, message
);
931 METHOD(task_manager_t
, incr_mid
, void,
932 private_task_manager_t
*this, bool initiate
)
936 this->initiating
.mid
++;
940 this->responding
.mid
++;
945 * Send a notify back to the sender
947 static void send_notify_response(private_task_manager_t
*this,
948 message_t
*request
, notify_type_t type
,
955 response
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
956 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
957 response
->set_request(response
, FALSE
);
958 response
->set_message_id(response
, request
->get_message_id(request
));
959 response
->add_notify(response
, FALSE
, type
, data
);
960 me
= this->ike_sa
->get_my_host(this->ike_sa
);
961 if (me
->is_anyaddr(me
))
963 me
= request
->get_destination(request
);
964 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
966 other
= this->ike_sa
->get_other_host(this->ike_sa
);
967 if (other
->is_anyaddr(other
))
969 other
= request
->get_source(request
);
970 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
972 response
->set_source(response
, me
->clone(me
));
973 response
->set_destination(response
, other
->clone(other
));
974 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
977 charon
->sender
->send(charon
->sender
, packet
);
979 response
->destroy(response
);
983 * Parse the given message and verify that it is valid.
985 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
990 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
992 if (status
== SUCCESS
)
993 { /* check for unsupported critical payloads */
994 enumerator_t
*enumerator
;
995 unknown_payload_t
*unknown
;
998 enumerator
= msg
->create_payload_enumerator(msg
);
999 while (enumerator
->enumerate(enumerator
, &payload
))
1001 unknown
= (unknown_payload_t
*)payload
;
1002 type
= payload
->get_type(payload
);
1003 if (!payload_is_known(type
) &&
1004 unknown
->is_critical(unknown
))
1006 DBG1(DBG_ENC
, "payload type %N is not supported, "
1007 "but its critical!", payload_type_names
, type
);
1008 status
= NOT_SUPPORTED
;
1012 enumerator
->destroy(enumerator
);
1015 if (status
!= SUCCESS
)
1017 bool is_request
= msg
->get_request(msg
);
1022 DBG1(DBG_IKE
, "critical unknown payloads found");
1025 send_notify_response(this, msg
,
1026 UNSUPPORTED_CRITICAL_PAYLOAD
,
1027 chunk_from_thing(type
));
1028 incr_mid(this, FALSE
);
1032 DBG1(DBG_IKE
, "message parsing failed");
1035 send_notify_response(this, msg
,
1036 INVALID_SYNTAX
, chunk_empty
);
1037 incr_mid(this, FALSE
);
1041 DBG1(DBG_IKE
, "message verification failed");
1044 send_notify_response(this, msg
,
1045 INVALID_SYNTAX
, chunk_empty
);
1046 incr_mid(this, FALSE
);
1050 DBG1(DBG_IKE
, "integrity check failed");
1054 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1058 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1059 exchange_type_names
, msg
->get_exchange_type(msg
),
1060 is_request
? "request" : "response",
1061 msg
->get_message_id(msg
));
1063 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_BODY
, msg
, status
);
1065 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1066 { /* invalid initiation attempt, close SA */
1074 METHOD(task_manager_t
, process_message
, status_t
,
1075 private_task_manager_t
*this, message_t
*msg
)
1081 charon
->bus
->message(charon
->bus
, msg
, TRUE
, FALSE
);
1082 status
= parse_message(this, msg
);
1083 if (status
!= SUCCESS
)
1088 me
= msg
->get_destination(msg
);
1089 other
= msg
->get_source(msg
);
1091 /* if this IKE_SA is virgin, we check for a config */
1092 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1094 ike_sa_id_t
*ike_sa_id
;
1097 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
,
1099 if (ike_cfg
== NULL
)
1101 /* no config found for these hosts, destroy */
1102 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1103 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1104 send_notify_response(this, msg
,
1105 NO_PROPOSAL_CHOSEN
, chunk_empty
);
1108 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1109 ike_cfg
->destroy(ike_cfg
);
1110 /* add a timeout if peer does not establish it completely */
1111 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1112 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1113 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1114 lib
->settings
->get_int(lib
->settings
,
1115 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
,
1118 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1119 time_monotonic(NULL
));
1121 mid
= msg
->get_message_id(msg
);
1122 if (msg
->get_request(msg
))
1124 if (mid
== this->responding
.mid
)
1126 /* reject initial messages once established */
1127 if (msg
->get_exchange_type(msg
) == IKE_SA_INIT
||
1128 msg
->get_exchange_type(msg
) == IKE_AUTH
)
1130 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CREATED
&&
1131 this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
1133 DBG1(DBG_IKE
, "ignoring %N in established IKE_SA state",
1134 exchange_type_names
, msg
->get_exchange_type(msg
));
1138 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1139 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1140 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1141 { /* only do host updates based on verified messages */
1142 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1143 { /* with MOBIKE, we do no implicit updates */
1144 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, mid
== 1);
1147 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1148 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1149 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1152 if (process_request(this, msg
) != SUCCESS
)
1157 this->responding
.mid
++;
1159 else if ((mid
== this->responding
.mid
- 1) && this->responding
.packet
)
1164 DBG1(DBG_IKE
, "received retransmit of request with ID %d, "
1165 "retransmitting response", mid
);
1166 charon
->bus
->alert(charon
->bus
, ALERT_RETRANSMIT_RECEIVE
, msg
);
1167 clone
= this->responding
.packet
->clone(this->responding
.packet
);
1168 host
= msg
->get_destination(msg
);
1169 clone
->set_source(clone
, host
->clone(host
));
1170 host
= msg
->get_source(msg
);
1171 clone
->set_destination(clone
, host
->clone(host
));
1172 charon
->sender
->send(charon
->sender
, clone
);
1176 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1177 mid
, this->responding
.mid
);
1178 if (msg
->get_exchange_type(msg
) == IKE_SA_INIT
)
1179 { /* clean up IKE_SA state if IKE_SA_INIT has invalid msg ID */
1186 if (mid
== this->initiating
.mid
)
1188 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1189 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1190 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1191 { /* only do host updates based on verified messages */
1192 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1193 { /* with MOBIKE, we do no implicit updates */
1194 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, FALSE
);
1197 charon
->bus
->message(charon
->bus
, msg
, TRUE
, TRUE
);
1198 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1199 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1202 if (process_response(this, msg
) != SUCCESS
)
1210 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1211 mid
, this->initiating
.mid
);
1218 METHOD(task_manager_t
, queue_task
, void,
1219 private_task_manager_t
*this, task_t
*task
)
1221 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
1222 { /* there is no need to queue more than one mobike task */
1223 enumerator_t
*enumerator
;
1226 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1227 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
1229 if (current
->get_type(current
) == TASK_IKE_MOBIKE
)
1231 enumerator
->destroy(enumerator
);
1232 task
->destroy(task
);
1236 enumerator
->destroy(enumerator
);
1238 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1239 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1243 * Check if a given task has been queued already
1245 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1247 enumerator_t
*enumerator
;
1251 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1252 while (enumerator
->enumerate(enumerator
, &task
))
1254 if (task
->get_type(task
) == type
)
1260 enumerator
->destroy(enumerator
);
1264 METHOD(task_manager_t
, queue_ike
, void,
1265 private_task_manager_t
*this)
1267 if (!has_queued(this, TASK_IKE_VENDOR
))
1269 queue_task(this, (task_t
*)ike_vendor_create(this->ike_sa
, TRUE
));
1271 if (!has_queued(this, TASK_IKE_INIT
))
1273 queue_task(this, (task_t
*)ike_init_create(this->ike_sa
, TRUE
, NULL
));
1275 if (!has_queued(this, TASK_IKE_NATD
))
1277 queue_task(this, (task_t
*)ike_natd_create(this->ike_sa
, TRUE
));
1279 if (!has_queued(this, TASK_IKE_CERT_PRE
))
1281 queue_task(this, (task_t
*)ike_cert_pre_create(this->ike_sa
, TRUE
));
1283 if (!has_queued(this, TASK_IKE_AUTH
))
1285 queue_task(this, (task_t
*)ike_auth_create(this->ike_sa
, TRUE
));
1287 if (!has_queued(this, TASK_IKE_CERT_POST
))
1289 queue_task(this, (task_t
*)ike_cert_post_create(this->ike_sa
, TRUE
));
1291 if (!has_queued(this, TASK_IKE_CONFIG
))
1293 queue_task(this, (task_t
*)ike_config_create(this->ike_sa
, TRUE
));
1295 if (!has_queued(this, TASK_IKE_AUTH_LIFETIME
))
1297 queue_task(this, (task_t
*)ike_auth_lifetime_create(this->ike_sa
, TRUE
));
1299 if (!has_queued(this, TASK_IKE_MOBIKE
))
1301 peer_cfg_t
*peer_cfg
;
1303 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1304 if (peer_cfg
->use_mobike(peer_cfg
))
1306 queue_task(this, (task_t
*)ike_mobike_create(this->ike_sa
, TRUE
));
1310 if (!has_queued(this, TASK_IKE_ME
))
1312 queue_task(this, (task_t
*)ike_me_create(this->ike_sa
, TRUE
));
1317 METHOD(task_manager_t
, queue_ike_rekey
, void,
1318 private_task_manager_t
*this)
1320 queue_task(this, (task_t
*)ike_rekey_create(this->ike_sa
, TRUE
));
1323 METHOD(task_manager_t
, queue_ike_reauth
, void,
1324 private_task_manager_t
*this)
1326 queue_task(this, (task_t
*)ike_reauth_create(this->ike_sa
));
1329 METHOD(task_manager_t
, queue_ike_delete
, void,
1330 private_task_manager_t
*this)
1332 queue_task(this, (task_t
*)ike_delete_create(this->ike_sa
, TRUE
));
1335 METHOD(task_manager_t
, queue_mobike
, void,
1336 private_task_manager_t
*this, bool roam
, bool address
)
1338 ike_mobike_t
*mobike
;
1340 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1343 mobike
->roam(mobike
, address
);
1347 mobike
->addresses(mobike
);
1349 queue_task(this, &mobike
->task
);
1352 METHOD(task_manager_t
, queue_child
, void,
1353 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1354 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1356 child_create_t
*task
;
1358 task
= child_create_create(this->ike_sa
, cfg
, FALSE
, tsi
, tsr
);
1361 task
->use_reqid(task
, reqid
);
1363 queue_task(this, &task
->task
);
1366 METHOD(task_manager_t
, queue_child_rekey
, void,
1367 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1369 queue_task(this, (task_t
*)child_rekey_create(this->ike_sa
, protocol
, spi
));
1372 METHOD(task_manager_t
, queue_child_delete
, void,
1373 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
,
1376 queue_task(this, (task_t
*)child_delete_create(this->ike_sa
,
1377 protocol
, spi
, expired
));
1380 METHOD(task_manager_t
, queue_dpd
, void,
1381 private_task_manager_t
*this)
1383 ike_mobike_t
*mobike
;
1385 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
) &&
1386 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_HERE
))
1388 /* use mobike enabled DPD to detect NAT mapping changes */
1389 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1390 mobike
->dpd(mobike
);
1391 queue_task(this, &mobike
->task
);
1395 queue_task(this, (task_t
*)ike_dpd_create(TRUE
));
1399 METHOD(task_manager_t
, adopt_tasks
, void,
1400 private_task_manager_t
*this, task_manager_t
*other_public
)
1402 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1405 /* move queued tasks from other to this */
1406 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1407 (void**)&task
) == SUCCESS
)
1409 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1410 task
->migrate(task
, this->ike_sa
);
1411 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1415 METHOD(task_manager_t
, busy
, bool,
1416 private_task_manager_t
*this)
1418 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1421 METHOD(task_manager_t
, reset
, void,
1422 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1424 enumerator_t
*enumerator
;
1427 /* reset message counters and retransmit packets */
1428 DESTROY_IF(this->responding
.packet
);
1429 DESTROY_IF(this->initiating
.packet
);
1430 this->responding
.packet
= NULL
;
1431 this->initiating
.packet
= NULL
;
1432 if (initiate
!= UINT_MAX
)
1434 this->initiating
.mid
= initiate
;
1436 if (respond
!= UINT_MAX
)
1438 this->responding
.mid
= respond
;
1440 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1442 /* reset queued tasks */
1443 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1444 while (enumerator
->enumerate(enumerator
, &task
))
1446 task
->migrate(task
, this->ike_sa
);
1448 enumerator
->destroy(enumerator
);
1450 /* reset active tasks */
1451 while (this->active_tasks
->remove_last(this->active_tasks
,
1452 (void**)&task
) == SUCCESS
)
1454 task
->migrate(task
, this->ike_sa
);
1455 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1461 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1462 private_task_manager_t
*this, task_queue_t queue
)
1466 case TASK_QUEUE_ACTIVE
:
1467 return this->active_tasks
->create_enumerator(this->active_tasks
);
1468 case TASK_QUEUE_PASSIVE
:
1469 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
1470 case TASK_QUEUE_QUEUED
:
1471 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
1473 return enumerator_create_empty();
1477 METHOD(task_manager_t
, destroy
, void,
1478 private_task_manager_t
*this)
1482 this->active_tasks
->destroy(this->active_tasks
);
1483 this->queued_tasks
->destroy(this->queued_tasks
);
1484 this->passive_tasks
->destroy(this->passive_tasks
);
1486 DESTROY_IF(this->responding
.packet
);
1487 DESTROY_IF(this->initiating
.packet
);
1494 task_manager_v2_t
*task_manager_v2_create(ike_sa_t
*ike_sa
)
1496 private_task_manager_t
*this;
1501 .process_message
= _process_message
,
1502 .queue_task
= _queue_task
,
1503 .queue_ike
= _queue_ike
,
1504 .queue_ike_rekey
= _queue_ike_rekey
,
1505 .queue_ike_reauth
= _queue_ike_reauth
,
1506 .queue_ike_delete
= _queue_ike_delete
,
1507 .queue_mobike
= _queue_mobike
,
1508 .queue_child
= _queue_child
,
1509 .queue_child_rekey
= _queue_child_rekey
,
1510 .queue_child_delete
= _queue_child_delete
,
1511 .queue_dpd
= _queue_dpd
,
1512 .initiate
= _initiate
,
1513 .retransmit
= _retransmit
,
1514 .incr_mid
= _incr_mid
,
1516 .adopt_tasks
= _adopt_tasks
,
1518 .create_task_enumerator
= _create_task_enumerator
,
1519 .flush_queue
= _flush_queue
,
1520 .destroy
= _destroy
,
1524 .initiating
.type
= EXCHANGE_TYPE_UNDEFINED
,
1525 .queued_tasks
= linked_list_create(),
1526 .active_tasks
= linked_list_create(),
1527 .passive_tasks
= linked_list_create(),
1528 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
1529 "%s.retransmit_tries", RETRANSMIT_TRIES
, charon
->name
),
1530 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
1531 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT
, charon
->name
),
1532 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
1533 "%s.retransmit_base", RETRANSMIT_BASE
, charon
->name
),
1536 return &this->public;