2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "task_manager_v1.h"
22 #include <sa/tasks/ike_vendor.h>
23 #include <sa/tasks/main_mode.h>
24 #include <sa/tasks/quick_mode.h>
25 #include <sa/tasks/xauth_request.h>
26 #include <sa/tasks/ike_vendor_v1.h>
27 #include <processing/jobs/retransmit_job.h>
28 #include <processing/jobs/delete_ike_sa_job.h>
30 typedef struct exchange_t exchange_t
;
33 * An exchange in the air, used do detect and handle retransmission
38 * Message ID used for this transaction
43 * generated packet for retransmission
48 typedef struct private_task_manager_t private_task_manager_t
;
51 * private data of the task manager
53 struct private_task_manager_t
{
58 task_manager_v1_t
public;
61 * associated IKE_SA we are serving
66 * RNG to create message IDs
71 * Exchange we are currently handling as responder
75 * Message ID of the exchange
80 * Hash of a previously received message
85 * packet for retransmission
92 * Exchange we are currently handling as initiator
96 * Message ID of the exchange
101 * Hash of a previously received message
106 * how many times we have retransmitted so far
111 * packet for retransmission
116 * type of the initated exchange
118 exchange_type_t type
;
123 * List of queued tasks not yet in action
125 linked_list_t
*queued_tasks
;
128 * List of active tasks, initiated by ourselve
130 linked_list_t
*active_tasks
;
133 * List of tasks initiated by peer
135 linked_list_t
*passive_tasks
;
138 * Number of times we retransmit messages before giving up
140 u_int retransmit_tries
;
143 * Retransmission timeout
145 double retransmit_timeout
;
148 * Base to calculate retransmission timeout
150 double retransmit_base
;
154 * flush all tasks in the task manager
156 static void flush(private_task_manager_t
*this)
158 this->queued_tasks
->destroy_offset(this->queued_tasks
,
159 offsetof(task_t
, destroy
));
160 this->queued_tasks
= linked_list_create();
161 this->passive_tasks
->destroy_offset(this->passive_tasks
,
162 offsetof(task_t
, destroy
));
163 this->passive_tasks
= linked_list_create();
164 this->active_tasks
->destroy_offset(this->active_tasks
,
165 offsetof(task_t
, destroy
));
166 this->active_tasks
= linked_list_create();
170 * move a task of a specific type from the queue to the active list
172 static bool activate_task(private_task_manager_t
*this, task_type_t type
)
174 enumerator_t
*enumerator
;
178 enumerator
= this->queued_tasks
->create_enumerator(this->queued_tasks
);
179 while (enumerator
->enumerate(enumerator
, (void**)&task
))
181 if (task
->get_type(task
) == type
)
183 DBG2(DBG_IKE
, " activating %N task", task_type_names
, type
);
184 this->queued_tasks
->remove_at(this->queued_tasks
, enumerator
);
185 this->active_tasks
->insert_last(this->active_tasks
, task
);
190 enumerator
->destroy(enumerator
);
194 METHOD(task_manager_t
, retransmit
, status_t
,
195 private_task_manager_t
*this, u_int32_t message_id
)
197 if (message_id
== this->initiating
.mid
)
203 if (this->initiating
.retransmitted
<= this->retransmit_tries
)
205 timeout
= (u_int32_t
)(this->retransmit_timeout
* 1000.0 *
206 pow(this->retransmit_base
, this->initiating
.retransmitted
));
210 DBG1(DBG_IKE
, "giving up after %d retransmits",
211 this->initiating
.retransmitted
- 1);
212 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
214 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
219 if (this->initiating
.retransmitted
)
221 DBG1(DBG_IKE
, "retransmit %d of request with message ID %d",
222 this->initiating
.retransmitted
, message_id
);
224 packet
= this->initiating
.packet
->clone(this->initiating
.packet
);
225 charon
->sender
->send(charon
->sender
, packet
);
227 this->initiating
.retransmitted
++;
228 job
= (job_t
*)retransmit_job_create(this->initiating
.mid
,
229 this->ike_sa
->get_id(this->ike_sa
));
230 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, timeout
);
235 METHOD(task_manager_t
, initiate
, status_t
,
236 private_task_manager_t
*this)
238 enumerator_t
*enumerator
;
243 exchange_type_t exchange
= EXCHANGE_TYPE_UNDEFINED
;
244 bool new_mid
= FALSE
;
248 DBG1(DBG_IKE
, "no RNG supported");
252 if (this->initiating
.type
!= EXCHANGE_TYPE_UNDEFINED
)
254 DBG2(DBG_IKE
, "delaying task initiation, %N exchange in progress",
255 exchange_type_names
, this->initiating
.type
);
256 /* do not initiate if we already have a message in the air */
260 if (this->active_tasks
->get_count(this->active_tasks
) == 0)
262 DBG2(DBG_IKE
, "activating new tasks");
263 switch (this->ike_sa
->get_state(this->ike_sa
))
266 activate_task(this, TASK_VENDOR_V1
);
267 if (activate_task(this, TASK_MAIN_MODE
))
272 case IKE_ESTABLISHED
:
273 if (activate_task(this, TASK_QUICK_MODE
))
275 exchange
= QUICK_MODE
;
279 if (activate_task(this, TASK_XAUTH_REQUEST
))
281 exchange
= TRANSACTION
;
292 DBG2(DBG_IKE
, "reinitiating already active tasks");
293 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
294 while (enumerator
->enumerate(enumerator
, (void**)&task
))
296 DBG2(DBG_IKE
, " %N task", task_type_names
, task
->get_type(task
));
297 switch (task
->get_type(task
))
302 case TASK_QUICK_MODE
:
303 exchange
= QUICK_MODE
;
305 case TASK_XAUTH_REQUEST
:
306 exchange
= TRANSACTION
;
314 enumerator
->destroy(enumerator
);
317 if (exchange
== EXCHANGE_TYPE_UNDEFINED
)
319 DBG2(DBG_IKE
, "nothing to initiate");
320 /* nothing to do yet... */
324 me
= this->ike_sa
->get_my_host(this->ike_sa
);
325 other
= this->ike_sa
->get_other_host(this->ike_sa
);
327 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
330 this->rng
->get_bytes(this->rng
, sizeof(this->initiating
.mid
),
331 (void*)&this->initiating
.mid
);
333 message
->set_message_id(message
, this->initiating
.mid
);
334 message
->set_source(message
, me
->clone(me
));
335 message
->set_destination(message
, other
->clone(other
));
336 message
->set_exchange_type(message
, exchange
);
337 this->initiating
.type
= exchange
;
338 this->initiating
.retransmitted
= 0;
340 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
341 while (enumerator
->enumerate(enumerator
, (void*)&task
))
343 switch (task
->build(task
, message
))
346 /* task completed, remove it */
347 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
351 /* processed, but task needs another exchange */
355 if (this->ike_sa
->get_state(this->ike_sa
) != IKE_CONNECTING
)
357 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
361 /* critical failure, destroy IKE_SA */
362 enumerator
->destroy(enumerator
);
363 message
->destroy(message
);
368 enumerator
->destroy(enumerator
);
370 /* update exchange type if a task changed it */
371 this->initiating
.type
= message
->get_exchange_type(message
);
373 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
374 &this->initiating
.packet
);
375 if (status
!= SUCCESS
)
377 /* message generation failed. There is nothing more to do than to
379 message
->destroy(message
);
381 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
384 message
->destroy(message
);
386 charon
->sender
->send(charon
->sender
,
387 this->initiating
.packet
->clone(this->initiating
.packet
));
393 * handle exchange collisions
395 static bool handle_collisions(private_task_manager_t
*this, task_t
*task
)
401 * build a response depending on the "passive" task list
403 static status_t
build_response(private_task_manager_t
*this, message_t
*request
)
405 enumerator_t
*enumerator
;
412 me
= request
->get_destination(request
);
413 other
= request
->get_source(request
);
415 message
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
416 message
->set_exchange_type(message
, request
->get_exchange_type(request
));
417 /* send response along the path the request came in */
418 message
->set_source(message
, me
->clone(me
));
419 message
->set_destination(message
, other
->clone(other
));
420 message
->set_message_id(message
, request
->get_message_id(request
));
421 message
->set_request(message
, FALSE
);
423 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
424 while (enumerator
->enumerate(enumerator
, (void*)&task
))
426 switch (task
->build(task
, message
))
429 /* task completed, remove it */
430 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
431 if (!handle_collisions(this, task
))
437 /* processed, but task needs another exchange */
438 if (handle_collisions(this, task
))
440 this->passive_tasks
->remove_at(this->passive_tasks
,
446 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
449 /* destroy IKE_SA, but SEND response first */
458 enumerator
->destroy(enumerator
);
460 /* message complete, send it */
461 DESTROY_IF(this->responding
.packet
);
462 this->responding
.packet
= NULL
;
463 status
= this->ike_sa
->generate_message(this->ike_sa
, message
,
464 &this->responding
.packet
);
465 message
->destroy(message
);
466 if (status
!= SUCCESS
)
468 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
472 charon
->sender
->send(charon
->sender
,
473 this->responding
.packet
->clone(this->responding
.packet
));
482 * handle an incoming request message
484 static status_t
process_request(private_task_manager_t
*this,
487 enumerator_t
*enumerator
;
490 if (this->passive_tasks
->get_count(this->passive_tasks
) == 0)
491 { /* create tasks depending on request type, if not already some queued */
492 switch (message
->get_exchange_type(message
))
495 task
= (task_t
*)ike_vendor_v1_create(this->ike_sa
, FALSE
);
496 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
497 task
= (task_t
*)main_mode_create(this->ike_sa
, FALSE
);
498 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
499 task
= (task_t
*)xauth_request_create(this->ike_sa
, FALSE
);
500 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
503 /* TODO-IKEv1: agressive mode */
506 task
= (task_t
*)quick_mode_create(this->ike_sa
, NULL
,
508 this->passive_tasks
->insert_last(this->passive_tasks
, task
);
510 case INFORMATIONAL_V1
:
511 /* TODO-IKEv1: informational */
517 /* let the tasks process the message */
518 enumerator
= this->passive_tasks
->create_enumerator(this->passive_tasks
);
519 while (enumerator
->enumerate(enumerator
, (void*)&task
))
521 switch (task
->process(task
, message
))
524 /* task completed, remove it */
525 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
527 enumerator
->destroy(enumerator
);
530 /* processed, but task needs at least another call to build() */
534 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
537 /* critical failure, destroy IKE_SA */
538 this->passive_tasks
->remove_at(this->passive_tasks
, enumerator
);
539 enumerator
->destroy(enumerator
);
544 enumerator
->destroy(enumerator
);
546 return build_response(this, message
);
550 * handle an incoming response message
552 static status_t
process_response(private_task_manager_t
*this,
555 enumerator_t
*enumerator
;
558 if (message
->get_exchange_type(message
) != this->initiating
.type
)
560 DBG1(DBG_IKE
, "received %N response, but expected %N",
561 exchange_type_names
, message
->get_exchange_type(message
),
562 exchange_type_names
, this->initiating
.type
);
563 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
567 enumerator
= this->active_tasks
->create_enumerator(this->active_tasks
);
568 while (enumerator
->enumerate(enumerator
, (void*)&task
))
570 switch (task
->process(task
, message
))
573 /* task completed, remove it */
574 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
578 /* processed, but task needs another exchange */
582 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, FALSE
);
585 /* critical failure, destroy IKE_SA */
586 this->active_tasks
->remove_at(this->active_tasks
, enumerator
);
587 enumerator
->destroy(enumerator
);
592 enumerator
->destroy(enumerator
);
594 this->initiating
.type
= EXCHANGE_TYPE_UNDEFINED
;
595 this->initiating
.packet
->destroy(this->initiating
.packet
);
596 this->initiating
.packet
= NULL
;
598 return initiate(this);
602 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
604 static void send_notify_response(private_task_manager_t
*this,
605 message_t
*request
, notify_type_t type
,
613 if (request
->get_exchange_type(request
) == INFORMATIONAL_V1
)
614 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
615 DBG1(DBG_IKE
, "ignore malformed INFORMATIONAL request");
619 response
= message_create(IKEV1_MAJOR_VERSION
, IKEV1_MINOR_VERSION
);
620 response
->set_exchange_type(response
, INFORMATIONAL_V1
);
621 response
->set_request(response
, TRUE
);
622 this->rng
->get_bytes(this->rng
, sizeof(mid
), (void*)&mid
);
623 response
->set_message_id(response
, mid
);
624 response
->add_notify(response
, FALSE
, type
, data
);
625 me
= this->ike_sa
->get_my_host(this->ike_sa
);
626 if (me
->is_anyaddr(me
))
628 me
= request
->get_destination(request
);
629 this->ike_sa
->set_my_host(this->ike_sa
, me
->clone(me
));
631 other
= this->ike_sa
->get_other_host(this->ike_sa
);
632 if (other
->is_anyaddr(other
))
634 other
= request
->get_source(request
);
635 this->ike_sa
->set_other_host(this->ike_sa
, other
->clone(other
));
637 response
->set_source(response
, me
->clone(me
));
638 response
->set_destination(response
, other
->clone(other
));
639 if (this->ike_sa
->generate_message(this->ike_sa
, response
,
642 charon
->sender
->send(charon
->sender
, packet
);
644 response
->destroy(response
);
648 * Parse the given message and verify that it is valid.
650 static status_t
parse_message(private_task_manager_t
*this, message_t
*msg
)
654 status
= msg
->parse_body(msg
, this->ike_sa
->get_keymat(this->ike_sa
));
656 if (status
!= SUCCESS
)
661 DBG1(DBG_IKE
, "unsupported exchange type");
662 send_notify_response(this, msg
,
663 INVALID_EXCHANGE_TYPE
, chunk_empty
);
666 DBG1(DBG_IKE
, "message parsing failed");
667 send_notify_response(this, msg
,
668 PAYLOAD_MALFORMED
, chunk_empty
);
671 DBG1(DBG_IKE
, "message verification failed");
672 send_notify_response(this, msg
,
673 PAYLOAD_MALFORMED
, chunk_empty
);
676 DBG1(DBG_IKE
, "integrity check failed");
677 send_notify_response(this, msg
,
678 INVALID_HASH_INFORMATION
, chunk_empty
);
681 DBG1(DBG_IKE
, "found encrypted message, but no keys available");
682 send_notify_response(this, msg
,
683 PAYLOAD_MALFORMED
, chunk_empty
);
687 DBG1(DBG_IKE
, "%N %s with message ID %d processing failed",
688 exchange_type_names
, msg
->get_exchange_type(msg
),
689 msg
->get_request(msg
) ?
"request" : "response",
690 msg
->get_message_id(msg
));
692 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_CREATED
)
693 { /* invalid initiation attempt, close SA */
700 METHOD(task_manager_t
, process_message
, status_t
,
701 private_task_manager_t
*this, message_t
*msg
)
707 /* TODO-IKEv1: update hosts more selectively */
708 me
= msg
->get_destination(msg
);
709 other
= msg
->get_source(msg
);
710 mid
= msg
->get_message_id(msg
);
712 if ((mid
&& mid
== this->initiating
.mid
) ||
713 (this->initiating
.mid
== 0 &&
714 this->active_tasks
->get_count(this->active_tasks
)))
716 msg
->set_request(msg
, FALSE
);
717 status
= parse_message(this, msg
);
718 if (status
!= SUCCESS
)
722 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
723 time_monotonic(NULL
));
724 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
725 charon
->bus
->message(charon
->bus
, msg
, FALSE
);
726 if (process_response(this, msg
) != SUCCESS
)
734 hash
= chunk_hash(msg
->get_packet_data(msg
));
735 if (hash
== this->responding
.hash
)
737 DBG1(DBG_IKE
, "received retransmit of request with ID %d, "
738 "retransmitting response", mid
);
739 charon
->sender
->send(charon
->sender
,
740 this->responding
.packet
->clone(this->responding
.packet
));
743 msg
->set_request(msg
, TRUE
);
744 status
= parse_message(this, msg
);
745 if (status
!= SUCCESS
)
749 /* if this IKE_SA is virgin, we check for a config */
750 if (this->ike_sa
->get_ike_cfg(this->ike_sa
) == NULL
)
752 ike_sa_id_t
*ike_sa_id
;
755 ike_cfg
= charon
->backends
->get_ike_cfg(charon
->backends
, me
, other
);
758 /* no config found for these hosts, destroy */
759 DBG1(DBG_IKE
, "no IKE config found for %H...%H, sending %N",
760 me
, other
, notify_type_names
, NO_PROPOSAL_CHOSEN
);
761 send_notify_response(this, msg
,
762 NO_PROPOSAL_CHOSEN
, chunk_empty
);
765 this->ike_sa
->set_ike_cfg(this->ike_sa
, ike_cfg
);
766 ike_cfg
->destroy(ike_cfg
);
767 /* add a timeout if peer does not establish it completely */
768 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
769 job
= (job_t
*)delete_ike_sa_job_create(ike_sa_id
, FALSE
);
770 lib
->scheduler
->schedule_job(lib
->scheduler
, job
,
771 lib
->settings
->get_int(lib
->settings
,
772 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT
));
774 this->ike_sa
->set_statistic(this->ike_sa
, STAT_INBOUND
,
775 time_monotonic(NULL
));
776 this->ike_sa
->update_hosts(this->ike_sa
, me
, other
, TRUE
);
777 charon
->bus
->message(charon
->bus
, msg
, TRUE
);
778 if (process_request(this, msg
) != SUCCESS
)
784 this->responding
.mid
= mid
;
785 this->responding
.hash
= hash
;
790 METHOD(task_manager_t
, queue_task
, void,
791 private_task_manager_t
*this, task_t
*task
)
793 DBG2(DBG_IKE
, "queueing %N task", task_type_names
, task
->get_type(task
));
794 this->queued_tasks
->insert_last(this->queued_tasks
, task
);
797 METHOD(task_manager_t
, adopt_tasks
, void,
798 private_task_manager_t
*this, task_manager_t
*other_public
)
800 private_task_manager_t
*other
= (private_task_manager_t
*)other_public
;
803 /* move queued tasks from other to this */
804 while (other
->queued_tasks
->remove_last(other
->queued_tasks
,
805 (void**)&task
) == SUCCESS
)
807 DBG2(DBG_IKE
, "migrating %N task", task_type_names
, task
->get_type(task
));
808 task
->migrate(task
, this->ike_sa
);
809 this->queued_tasks
->insert_first(this->queued_tasks
, task
);
813 METHOD(task_manager_t
, busy
, bool,
814 private_task_manager_t
*this)
816 return (this->active_tasks
->get_count(this->active_tasks
) > 0);
819 METHOD(task_manager_t
, incr_mid
, void,
820 private_task_manager_t
*this, bool initiate
)
824 METHOD(task_manager_t
, reset
, void,
825 private_task_manager_t
*this, u_int32_t initiate
, u_int32_t respond
)
829 METHOD(task_manager_t
, create_task_enumerator
, enumerator_t
*,
830 private_task_manager_t
*this, task_queue_t queue
)
834 case TASK_QUEUE_ACTIVE
:
835 return this->active_tasks
->create_enumerator(this->active_tasks
);
836 case TASK_QUEUE_PASSIVE
:
837 return this->passive_tasks
->create_enumerator(this->passive_tasks
);
838 case TASK_QUEUE_QUEUED
:
839 return this->queued_tasks
->create_enumerator(this->queued_tasks
);
841 return enumerator_create_empty();
845 METHOD(task_manager_t
, destroy
, void,
846 private_task_manager_t
*this)
850 this->active_tasks
->destroy(this->active_tasks
);
851 this->queued_tasks
->destroy(this->queued_tasks
);
852 this->passive_tasks
->destroy(this->passive_tasks
);
854 DESTROY_IF(this->responding
.packet
);
855 DESTROY_IF(this->initiating
.packet
);
856 DESTROY_IF(this->rng
);
863 task_manager_v1_t
*task_manager_v1_create(ike_sa_t
*ike_sa
)
865 private_task_manager_t
*this;
870 .process_message
= _process_message
,
871 .queue_task
= _queue_task
,
872 .initiate
= _initiate
,
873 .retransmit
= _retransmit
,
874 .incr_mid
= _incr_mid
,
876 .adopt_tasks
= _adopt_tasks
,
878 .create_task_enumerator
= _create_task_enumerator
,
883 .initiating
.type
= EXCHANGE_TYPE_UNDEFINED
,
884 .rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
),
885 .queued_tasks
= linked_list_create(),
886 .active_tasks
= linked_list_create(),
887 .passive_tasks
= linked_list_create(),
888 .retransmit_tries
= lib
->settings
->get_int(lib
->settings
,
889 "charon.retransmit_tries", RETRANSMIT_TRIES
),
890 .retransmit_timeout
= lib
->settings
->get_double(lib
->settings
,
891 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT
),
892 .retransmit_base
= lib
->settings
->get_double(lib
->settings
,
893 "charon.retransmit_base", RETRANSMIT_BASE
),
896 return &this->public;