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->queued_tasks
->destroy_offset(this->queued_tasks
,
166 offsetof(task_t
, destroy
));
167 this->queued_tasks
= linked_list_create();
168 this->passive_tasks
->destroy_offset(this->passive_tasks
,
169 offsetof(task_t
, destroy
));
170 this->passive_tasks
= linked_list_create();
171 this->active_tasks
->destroy_offset(this->active_tasks
,
172 offsetof(task_t
, destroy
));
173 this->active_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 (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
;
373 if (activate_task(this, TASK_IKE_ME
))
375 exchange
= ME_CONNECT
;
380 if (activate_task(this, TASK_IKE_DELETE
))
382 exchange
= INFORMATIONAL
;
392 DBG2(DBG_IKE
, "reinitiating already active tasks");
393 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
394 while (enumerator
->enumerate(enumerator
, (void**)&task
))
396 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
397 switch (task
->get_type(task
))
400 exchange
= IKE_SA_INIT
;
405 case TASK_CHILD_CREATE
:
406 case TASK_CHILD_REKEY
:
408 exchange
= CREATE_CHILD_SA
;
410 case TASK_IKE_MOBIKE
:
411 exchange
= INFORMATIONAL
;
418 enumerator
->destroy(enumerator
);
423 DBG2(DBG_IKE
, "nothing to initiate");
424 /* nothing to do yet... */
428 me
= this->ike_sa
->get_my_host(this->ike_sa
);
429 other
= this->ike_sa
->get_other_host(this->ike_sa
);
431 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
432 message
->set_message_id(message
, this->initiating
.mid
);
433 message
->set_source(message
, me
->clone(me
));
434 message
->set_destination(message
, other
->clone(other
));
435 message
->set_exchange_type(message
, exchange
);
436 this->initiating
.type
= exchange
;
437 this->initiating
.retransmitted
= 0;
439 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
440 while (enumerator
->enumerate(enumerator
, (void*)&task
))
442 switch (task
->build(task
, message
))
445 /* task completed, remove it */
446 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
450 /* processed, but task needs another exchange */
454 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
456 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
460 /* critical failure, destroy IKE_SA */
461 enumerator
->destroy(enumerator
);
462 message
->destroy(message
);
467 enumerator
->destroy(enumerator
);
469 /* update exchange type if a task changed it */
470 this->initiating
.type
= message
->get_exchange_type(message
);
472 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
473 &this->initiating
.packet
);
474 if (status
!= SUCCESS
)
476 /* message generation failed. There is nothing more to do than to
478 message
->destroy(message
);
480 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
483 message
->destroy(message
);
485 return retransmit(this, this->initiating
.mid
);
489 * handle an incoming response message
491 static status_t
process_response(private_task_manager_t
*this,
494 enumerator_t
*enumerator
;
497 if (message
->get_exchange_type(message
) != this->initiating
.type
)
499 DBG1(DBG_IKE
, "received %N response, but expected %N",
500 exchange_type_names
, message
->get_exchange_type(message
),
501 exchange_type_names
, this->initiating
.type
);
502 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
506 /* catch if we get resetted while processing */
508 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
509 while (enumerator
->enumerate(enumerator
, (void*)&task
))
511 switch (task
->process(task
, message
))
514 /* task completed, remove it */
515 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
519 /* processed, but task needs another exchange */
523 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
526 /* critical failure, destroy IKE_SA */
527 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
528 enumerator
->destroy(enumerator
);
533 { /* start all over again if we were reset */
535 enumerator
->destroy(enumerator
);
536 return initiate(this);
539 enumerator
->destroy(enumerator
);
541 this->initiating
.mid
++;
542 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
543 this->initiating
.packet
->destroy(this->initiating
.packet
);
544 this->initiating
.packet
= NULL
;
546 return initiate(this);
550 * handle exchange collisions
552 static bool handle_collisions(private_task_manager_t
*this, task_t
*task
)
554 enumerator_t
*enumerator
;
558 type
= task
->get_type(task
);
560 /* do we have to check */
561 if (type
== TASK_IKE_REKEY
|| type
== TASK_CHILD_REKEY
||
562 type
== TASK_CHILD_DELETE
|| type
== TASK_IKE_DELETE
||
563 type
== TASK_IKE_REAUTH
)
565 /* find an exchange collision, and notify these tasks */
566 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
567 while (enumerator
->enumerate(enumerator
, (void**)&active
))
569 switch (active
->get_type(active
))
572 if (type
== TASK_IKE_REKEY
|| type
== TASK_IKE_DELETE
||
573 type
== TASK_IKE_REAUTH
)
575 ike_rekey_t
*rekey
= (ike_rekey_t
*)active
;
576 rekey
->collide(rekey
, task
);
580 case TASK_CHILD_REKEY
:
581 if (type
== TASK_CHILD_REKEY
|| type
== TASK_CHILD_DELETE
)
583 child_rekey_t
*rekey
= (child_rekey_t
*)active
;
584 rekey
->collide(rekey
, task
);
591 enumerator
->destroy(enumerator
);
594 enumerator
->destroy(enumerator
);
600 * build a response depending on the "passive" task list
602 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
604 enumerator_t
*enumerator
;
611 me
= request
->get_destination(request
);
612 other
= request
->get_source(request
);
614 message
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
615 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
616 /* send response along the path the request came in */
617 message
->set_source(message
, me
->clone(me
));
618 message
->set_destination(message
, other
->clone(other
));
619 message
->set_message_id(message
, this->responding
.mid
);
620 message
->set_request(message
, FALSE
);
622 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
623 while (enumerator
->enumerate(enumerator
, (void*)&task
))
625 switch (task
->build(task
, message
))
628 /* task completed, remove it */
629 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
630 if (!handle_collisions(this, task
))
636 /* processed, but task needs another exchange */
637 if (handle_collisions(this, task
))
639 this->passive_tasks
->remove_at(this->passive_tasks
,
645 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
648 /* destroy IKE_SA, but SEND response first */
657 enumerator
->destroy(enumerator
);
659 /* remove resonder SPI if IKE_SA_INIT failed */
660 if (delete && request
->get_exchange_type(request
) == IKE_SA_INIT
)
662 ike_sa_id_t
*id
= this->ike_sa
->get_id(this->ike_sa
);
663 id
->set_responder_spi(id
, 0);
666 /* message complete, send it */
667 DESTROY_IF(this->responding
.packet
);
668 this->responding
.packet
= NULL
;
669 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
670 &this->responding
.packet
);
671 message
->destroy(message
);
672 if (status
!= SUCCESS
)
674 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
678 charon
->sender
->send(charon
->sender
,
679 this->responding
.packet
->clone(this->responding
.packet
));
688 * handle an incoming request message
690 static status_t
process_request(private_task_manager_t
*this,
693 enumerator_t
*enumerator
;
696 notify_payload_t
*notify
;
697 delete_payload_t
*delete;
699 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0)
700 { /* create tasks depending on request type, if not already some queued */
701 switch (message
->get_exchange_type(message
))
705 task
= (task_t
*)ike_vendor_create(this->ike_sa
, FALSE
);
706 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
707 task
= (task_t
*)ike_init_create(this->ike_sa
, FALSE
, NULL
);
708 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
709 task
= (task_t
*)ike_natd_create(this->ike_sa
, FALSE
);
710 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
711 task
= (task_t
*)ike_cert_pre_create(this->ike_sa
, FALSE
);
712 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
714 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
715 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
717 task
= (task_t
*)ike_auth_create(this->ike_sa
, FALSE
);
718 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
719 task
= (task_t
*)ike_cert_post_create(this->ike_sa
, FALSE
);
720 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
721 task
= (task_t
*)ike_config_create(this->ike_sa
, FALSE
);
722 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
723 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
, FALSE
,
725 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
726 task
= (task_t
*)ike_auth_lifetime_create(this->ike_sa
, FALSE
);
727 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
728 task
= (task_t
*)ike_mobike_create(this->ike_sa
, FALSE
);
729 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
732 case CREATE_CHILD_SA
:
733 { /* FIXME: we should prevent this on mediation connections */
734 bool notify_found
= FALSE
, ts_found
= FALSE
;
735 enumerator
= message
->create_payload_enumerator(message
);
736 while (enumerator
->enumerate(enumerator
, &payload
))
738 switch (payload
->get_type(payload
))
741 { /* if we find a rekey notify, its CHILD_SA rekeying */
742 notify
= (notify_payload_t
*)payload
;
743 if (notify
->get_notify_type(notify
) == REKEY_SA
&&
744 (notify
->get_protocol_id(notify
) == PROTO_AH
||
745 notify
->get_protocol_id(notify
) == PROTO_ESP
))
751 case TRAFFIC_SELECTOR_INITIATOR
:
752 case TRAFFIC_SELECTOR_RESPONDER
:
753 { /* if we don't find a TS, its IKE rekeying */
761 enumerator
->destroy(enumerator
);
767 task
= (task_t
*)child_rekey_create(this->ike_sa
,
772 task
= (task_t
*)child_create_create(this->ike_sa
, NULL
,
778 task
= (task_t
*)ike_rekey_create(this->ike_sa
, FALSE
);
780 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
785 enumerator
= message
->create_payload_enumerator(message
);
786 while (enumerator
->enumerate(enumerator
, &payload
))
788 switch (payload
->get_type(payload
))
792 notify
= (notify_payload_t
*)payload
;
793 switch (notify
->get_notify_type(notify
))
795 case ADDITIONAL_IP4_ADDRESS
:
796 case ADDITIONAL_IP6_ADDRESS
:
797 case NO_ADDITIONAL_ADDRESSES
:
798 case UPDATE_SA_ADDRESSES
:
799 case NO_NATS_ALLOWED
:
800 case UNACCEPTABLE_ADDRESSES
:
801 case UNEXPECTED_NAT_DETECTED
:
803 case NAT_DETECTION_SOURCE_IP
:
804 case NAT_DETECTION_DESTINATION_IP
:
805 task
= (task_t
*)ike_mobike_create(
806 this->ike_sa
, FALSE
);
809 task
= (task_t
*)ike_auth_lifetime_create(
810 this->ike_sa
, FALSE
);
819 delete = (delete_payload_t
*)payload
;
820 if (delete->get_protocol_id(delete) == PROTO_IKE
)
822 task
= (task_t
*)ike_delete_create(this->ike_sa
,
827 task
= (task_t
*)child_delete_create(this->ike_sa
,
840 enumerator
->destroy(enumerator
);
844 task
= (task_t
*)ike_dpd_create(FALSE
);
846 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
852 task
= (task_t
*)ike_me_create(this->ike_sa
, FALSE
);
853 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
861 /* let the tasks process the message */
862 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
863 while (enumerator
->enumerate(enumerator
, (void*)&task
))
865 switch (task
->process(task
, message
))
868 /* task completed, remove it */
869 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
873 /* processed, but task needs at least another call to build() */
877 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
880 /* critical failure, destroy IKE_SA */
881 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
882 enumerator
->destroy(enumerator
);
887 enumerator
->destroy(enumerator
);
889 return build_response(this, message
);
892 METHOD(task_manager_t
, incr_mid
, void,
893 private_task_manager_t
*this, bool initiate
)
897 this->initiating
.mid
++;
901 this->responding
.mid
++;
906 * Send a notify back to the sender
908 static void send_notify_response(private_task_manager_t
*this,
909 message_t
*request
, notify_type_t type
,
916 response
= message_create(IKEV2_MAJOR_VERSION
, IKEV2_MINOR_VERSION
);
917 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
918 response
->set_request(response
, FALSE
);
919 response
->set_message_id(response
, request
->get_message_id(request
));
920 response
->add_notify(response
, FALSE
, type
, data
);
921 me
= this->ike_sa
->get_my_host(this->ike_sa
);
922 if (me
->is_anyaddr(me
))
924 me
= request
->get_destination(request
);
925 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
927 other
= this->ike_sa
->get_other_host(this->ike_sa
);
928 if (other
->is_anyaddr(other
))
930 other
= request
->get_source(request
);
931 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
933 response
->set_source(response
, me
->clone(me
));
934 response
->set_destination(response
, other
->clone(other
));
935 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
938 charon
->sender
->send(charon
->sender
, packet
);
940 response
->destroy(response
);
944 * Parse the given message and verify that it is valid.
946 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
951 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
953 if (status
== SUCCESS
)
954 { /* check for unsupported critical payloads */
955 enumerator_t
*enumerator
;
956 unknown_payload_t
*unknown
;
959 enumerator
= msg
->create_payload_enumerator(msg
);
960 while (enumerator
->enumerate(enumerator
, &payload
))
962 unknown
= (unknown_payload_t
*)payload
;
963 type
= payload
->get_type(payload
);
964 if (!payload_is_known(type
) &&
965 unknown
->is_critical(unknown
))
967 DBG1(DBG_ENC
, "payload type %N is not supported, "
968 "but its critical!", payload_type_names
, type
);
969 status
= NOT_SUPPORTED
;
972 enumerator
->destroy(enumerator
);
975 if (status
!= SUCCESS
)
977 bool is_request
= msg
->get_request(msg
);
982 DBG1(DBG_IKE
, "critical unknown payloads found");
985 send_notify_response(this, msg
,
986 UNSUPPORTED_CRITICAL_PAYLOAD
,
987 chunk_from_thing(type
));
988 incr_mid(this, FALSE
);
992 DBG1(DBG_IKE
, "message parsing failed");
995 send_notify_response(this, msg
,
996 INVALID_SYNTAX
, chunk_empty
);
997 incr_mid(this, FALSE
);
1001 DBG1(DBG_IKE
, "message verification failed");
1004 send_notify_response(this, msg
,
1005 INVALID_SYNTAX
, chunk_empty
);
1006 incr_mid(this, FALSE
);
1010 DBG1(DBG_IKE
, "integrity check failed");
1014 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
1018 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
1019 exchange_type_names
, msg
->get_exchange_type(msg
),
1020 is_request ?
"request" : "response",
1021 msg
->get_message_id(msg
));
1023 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
1024 { /* invalid initiation attempt, close SA */
1032 METHOD(task_manager_t
, process_message
, status_t
,
1033 private_task_manager_t
*this, message_t
*msg
)
1039 status
= parse_message(this, msg
);
1040 if (status
!= SUCCESS
)
1045 me
= msg
->get_destination(msg
);
1046 other
= msg
->get_source(msg
);
1048 /* if this IKE_SA is virgin, we check for a config */
1049 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
1051 ike_sa_id_t
*ike_sa_id
;
1054 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
, me
, other
);
1055 if (ike_cfg
== NULL
)
1057 /* no config found for these hosts, destroy */
1058 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
1059 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
1060 send_notify_response(this, msg
,
1061 NO_PROPOSAL_CHOSEN
, chunk_empty
);
1064 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
1065 ike_cfg
->destroy(ike_cfg
);
1066 /* add a timeout if peer does not establish it completely */
1067 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
1068 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
1069 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
1070 lib
->settings
->get_int(lib
->settings
,
1071 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
));
1073 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
1074 time_monotonic(NULL
));
1076 mid
= msg
->get_message_id(msg
);
1077 if (msg
->get_request(msg
))
1079 if (mid
== this->responding
.mid
)
1081 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1082 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1083 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1084 { /* only do host updates based on verified messages */
1085 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1086 { /* with MOBIKE, we do no implicit updates */
1087 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, mid
== 1);
1090 charon
->bus
->message(charon
->bus
, msg
, TRUE
);
1091 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1092 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1095 if (process_request(this, msg
) != SUCCESS
)
1100 this->responding
.mid
++;
1102 else if ((mid
== this->responding
.mid
- 1) && this->responding
.packet
)
1107 DBG1(DBG_IKE
, "received retransmit of request with ID %d, "
1108 "retransmitting response", mid
);
1109 clone
= this->responding
.packet
->clone(this->responding
.packet
);
1110 host
= msg
->get_destination(msg
);
1111 clone
->set_source(clone
, host
->clone(host
));
1112 host
= msg
->get_source(msg
);
1113 clone
->set_destination(clone
, host
->clone(host
));
1114 charon
->sender
->send(charon
->sender
, clone
);
1118 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1119 mid
, this->responding
.mid
);
1124 if (mid
== this->initiating
.mid
)
1126 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
||
1127 this->ike_sa
->get_state(this->ike_sa
) == IKE_CONNECTING
||
1128 msg
->get_exchange_type(msg
) != IKE_SA_INIT
)
1129 { /* only do host updates based on verified messages */
1130 if (!this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
))
1131 { /* with MOBIKE, we do no implicit updates */
1132 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, FALSE
);
1135 charon
->bus
->message(charon
->bus
, msg
, TRUE
);
1136 if (msg
->get_exchange_type(msg
) == EXCHANGE_TYPE_UNDEFINED
)
1137 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1140 if (process_response(this, msg
) != SUCCESS
)
1148 DBG1(DBG_IKE
, "received message ID %d, expected %d. Ignored",
1149 mid
, this->initiating
.mid
);
1156 METHOD(task_manager_t
, queue_task
, void,
1157 private_task_manager_t
*this, task_t
*task
)
1159 if (task
->get_type(task
) == TASK_IKE_MOBIKE
)
1160 { /* there is no need to queue more than one mobike task */
1161 enumerator_t
*enumerator
;
1164 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1165 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
1167 if (current
->get_type(current
) == TASK_IKE_MOBIKE
)
1169 enumerator
->destroy(enumerator
);
1170 task
->destroy(task
);
1174 enumerator
->destroy(enumerator
);
1176 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
1177 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
1181 * Check if a given task has been queued already
1183 static bool has_queued(private_task_manager_t
*this, task_type_t type
)
1185 enumerator_t
*enumerator
;
1189 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1190 while (enumerator
->enumerate(enumerator
, &task
))
1192 if (task
->get_type(task
) == type
)
1198 enumerator
->destroy(enumerator
);
1202 METHOD(task_manager_t
, queue_ike
, void,
1203 private_task_manager_t
*this)
1205 if (!has_queued(this, TASK_IKE_VENDOR
))
1207 queue_task(this, (task_t
*)ike_vendor_create(this->ike_sa
, TRUE
));
1209 if (!has_queued(this, TASK_IKE_INIT
))
1211 queue_task(this, (task_t
*)ike_init_create(this->ike_sa
, TRUE
, NULL
));
1213 if (!has_queued(this, TASK_IKE_NATD
))
1215 queue_task(this, (task_t
*)ike_natd_create(this->ike_sa
, TRUE
));
1217 if (!has_queued(this, TASK_IKE_CERT_PRE
))
1219 queue_task(this, (task_t
*)ike_cert_pre_create(this->ike_sa
, TRUE
));
1221 if (!has_queued(this, TASK_IKE_AUTH
))
1223 queue_task(this, (task_t
*)ike_auth_create(this->ike_sa
, TRUE
));
1225 if (!has_queued(this, TASK_IKE_CERT_POST
))
1227 queue_task(this, (task_t
*)ike_cert_post_create(this->ike_sa
, TRUE
));
1229 if (!has_queued(this, TASK_IKE_CONFIG
))
1231 queue_task(this, (task_t
*)ike_config_create(this->ike_sa
, TRUE
));
1233 if (!has_queued(this, TASK_IKE_AUTH_LIFETIME
))
1235 queue_task(this, (task_t
*)ike_auth_lifetime_create(this->ike_sa
, TRUE
));
1237 if (!has_queued(this, TASK_IKE_MOBIKE
))
1239 peer_cfg_t
*peer_cfg
;
1241 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
1242 if (peer_cfg
->use_mobike(peer_cfg
))
1244 queue_task(this, (task_t
*)ike_mobike_create(this->ike_sa
, TRUE
));
1248 if (!has_queued(this, TASK_IKE_ME
))
1250 queue_task(this, (task_t
*)ike_me_create(this->ike_sa
, TRUE
));
1255 METHOD(task_manager_t
, queue_ike_rekey
, void,
1256 private_task_manager_t
*this)
1258 queue_task(this, (task_t
*)ike_rekey_create(this->ike_sa
, TRUE
));
1261 METHOD(task_manager_t
, queue_ike_reauth
, void,
1262 private_task_manager_t
*this)
1264 queue_task(this, (task_t
*)ike_reauth_create(this->ike_sa
));
1267 METHOD(task_manager_t
, queue_ike_delete
, void,
1268 private_task_manager_t
*this)
1270 queue_task(this, (task_t
*)ike_delete_create(this->ike_sa
, TRUE
));
1273 METHOD(task_manager_t
, queue_mobike
, void,
1274 private_task_manager_t
*this, bool roam
, bool address
)
1276 ike_mobike_t
*mobike
;
1278 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1281 mobike
->roam(mobike
, address
);
1285 mobike
->addresses(mobike
);
1287 queue_task(this, &mobike
->task
);
1290 METHOD(task_manager_t
, queue_child
, void,
1291 private_task_manager_t
*this, child_cfg_t
*cfg
, u_int32_t reqid
,
1292 traffic_selector_t
*tsi
, traffic_selector_t
*tsr
)
1294 child_create_t
*task
;
1296 task
= child_create_create(this->ike_sa
, cfg
, FALSE
, tsi
, tsr
);
1299 task
->use_reqid(task
, reqid
);
1301 queue_task(this, &task
->task
);
1304 METHOD(task_manager_t
, queue_child_rekey
, void,
1305 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1307 queue_task(this, (task_t
*)child_rekey_create(this->ike_sa
, protocol
, spi
));
1310 METHOD(task_manager_t
, queue_child_delete
, void,
1311 private_task_manager_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1313 queue_task(this, (task_t
*)child_delete_create(this->ike_sa
, protocol
, spi
));
1316 METHOD(task_manager_t
, queue_dpd
, void,
1317 private_task_manager_t
*this)
1319 ike_mobike_t
*mobike
;
1321 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_MOBIKE
) &&
1322 this->ike_sa
->has_condition(this->ike_sa
, COND_NAT_HERE
))
1324 /* use mobike enabled DPD to detect NAT mapping changes */
1325 mobike
= ike_mobike_create(this->ike_sa
, TRUE
);
1326 mobike
->dpd(mobike
);
1327 queue_task(this, &mobike
->task
);
1331 queue_task(this, (task_t
*)ike_dpd_create(TRUE
));
1335 METHOD(task_manager_t
, adopt_tasks
, void,
1336 private_task_manager_t
*this, task_manager_t
*other_public
)
1338 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
1341 /* move queued tasks from other to this */
1342 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
1343 (void**)&task
) == SUCCESS
)
1345 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
1346 task
->migrate(task
, this->ike_sa
);
1347 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1351 METHOD(task_manager_t
, busy
, bool,
1352 private_task_manager_t
*this)
1354 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
1357 METHOD(task_manager_t
, reset
, void,
1358 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
1360 enumerator_t
*enumerator
;
1363 /* reset message counters and retransmit packets */
1364 DESTROY_IF(this->responding
.packet
);
1365 DESTROY_IF(this->initiating
.packet
);
1366 this->responding
.packet
= NULL
;
1367 this->initiating
.packet
= NULL
;
1368 if (initiate
!= UINT_MAX
)
1370 this->initiating
.mid
= initiate
;
1372 if (respond
!= UINT_MAX
)
1374 this->responding
.mid
= respond
;
1376 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
1378 /* reset queued tasks */
1379 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
1380 while (enumerator
->enumerate(enumerator
, &task
))
1382 task
->migrate(task
, this->ike_sa
);
1384 enumerator
->destroy(enumerator
);
1386 /* reset active tasks */
1387 while (this->active_tasks
->remove_last(this->active_tasks
,
1388 (void**)&task
) == SUCCESS
)
1390 task
->migrate(task
, this->ike_sa
);
1391 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
1397 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
1398 private_task_manager_t
*this, task_queue_t queue
)
1402 case TASK_QUEUE_ACTIVE
:
1403 return this->active_tasks
->create_enumerator(this->active_tasks
);
1404 case TASK_QUEUE_PASSIVE
:
1405 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
1406 case TASK_QUEUE_QUEUED
:
1407 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
1409 return enumerator_create_empty();
1413 METHOD(task_manager_t
, destroy
, void,
1414 private_task_manager_t
*this)
1418 this->active_tasks
->destroy(this->active_tasks
);
1419 this->queued_tasks
->destroy(this->queued_tasks
);
1420 this->passive_tasks
->destroy(this->passive_tasks
);
1422 DESTROY_IF(this->responding
.packet
);
1423 DESTROY_IF(this->initiating
.packet
);
1430 task_manager_v2_t
*task_manager_v2_create(ike_sa_t
*ike_sa
)
1432 private_task_manager_t
*this;
1437 .process_message
= _process_message
,
1438 .queue_task
= _queue_task
,
1439 .queue_ike
= _queue_ike
,
1440 .queue_ike_rekey
= _queue_ike_rekey
,
1441 .queue_ike_reauth
= _queue_ike_reauth
,
1442 .queue_ike_delete
= _queue_ike_delete
,
1443 .queue_mobike
= _queue_mobike
,
1444 .queue_child
= _queue_child
,
1445 .queue_child_rekey
= _queue_child_rekey
,
1446 .queue_child_delete
= _queue_child_delete
,
1447 .queue_dpd
= _queue_dpd
,
1448 .initiate
= _initiate
,
1449 .retransmit
= _retransmit
,
1450 .incr_mid
= _incr_mid
,
1452 .adopt_tasks
= _adopt_tasks
,
1454 .create_task_enumerator
= _create_task_enumerator
,
1455 .destroy
= _destroy
,
1459 .initiating
.type
= EXCHANGE_TYPE_UNDEFINED
,
1460 .queued_tasks
= linked_list_create(),
1461 .active_tasks
= linked_list_create(),
1462 .passive_tasks
= linked_list_create(),
1463 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
1464 "charon.retransmit_tries", RETRANSMIT_TRIES
),
1465 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
1466 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT
),
1467 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
1468 "charon.retransmit_base", RETRANSMIT_BASE
),
1471 return &this->public;