Flush task queues explicitly, not implicitly if task returns ALREADY_DONE
[strongswan.git] / src / libcharon / sa / ikev1 / task_manager_v1.c
1 /*
2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include "task_manager_v1.h"
18
19 #include <math.h>
20
21 #include <daemon.h>
22 #include <sa/ikev1/tasks/main_mode.h>
23 #include <sa/ikev1/tasks/aggressive_mode.h>
24 #include <sa/ikev1/tasks/quick_mode.h>
25 #include <sa/ikev1/tasks/quick_delete.h>
26 #include <sa/ikev1/tasks/xauth.h>
27 #include <sa/ikev1/tasks/mode_config.h>
28 #include <sa/ikev1/tasks/informational.h>
29 #include <sa/ikev1/tasks/isakmp_natd.h>
30 #include <sa/ikev1/tasks/isakmp_vendor.h>
31 #include <sa/ikev1/tasks/isakmp_cert_pre.h>
32 #include <sa/ikev1/tasks/isakmp_cert_post.h>
33 #include <sa/ikev1/tasks/isakmp_delete.h>
34 #include <sa/ikev1/tasks/isakmp_dpd.h>
35
36 #include <processing/jobs/retransmit_job.h>
37 #include <processing/jobs/delete_ike_sa_job.h>
38 #include <processing/jobs/dpd_timeout_job.h>
39
40 /**
41 * Number of old messages hashes we keep for retransmission.
42 *
43 * In Main Mode, we must ignore messages from a previous message pair if
44 * we already continued to the next. Otherwise a late retransmission
45 * could be considered as a reply to the newer request.
46 */
47 #define MAX_OLD_HASHES 2
48
49 /**
50 * First sequence number of responding packets.
51 *
52 * To distinguish retransmission jobs for initiating and responding packets,
53 * we split up the sequence counter and use the upper half for responding.
54 */
55 #define RESPONDING_SEQ INT_MAX
56
57 typedef struct exchange_t exchange_t;
58
59 /**
60 * An exchange in the air, used do detect and handle retransmission
61 */
62 struct exchange_t {
63
64 /**
65 * Message ID used for this transaction
66 */
67 u_int32_t mid;
68
69 /**
70 * generated packet for retransmission
71 */
72 packet_t *packet;
73 };
74
75 typedef struct private_task_manager_t private_task_manager_t;
76
77 /**
78 * private data of the task manager
79 */
80 struct private_task_manager_t {
81
82 /**
83 * public functions
84 */
85 task_manager_v1_t public;
86
87 /**
88 * associated IKE_SA we are serving
89 */
90 ike_sa_t *ike_sa;
91
92 /**
93 * RNG to create message IDs
94 */
95 rng_t *rng;
96
97 /**
98 * Exchange we are currently handling as responder
99 */
100 struct {
101 /**
102 * Message ID of the last response
103 */
104 u_int32_t mid;
105
106 /**
107 * Hash of a previously received message
108 */
109 u_int32_t hash;
110
111 /**
112 * packet for retransmission
113 */
114 packet_t *packet;
115
116 /**
117 * Sequence number of the last sent message
118 */
119 u_int32_t seqnr;
120
121 /**
122 * how many times we have retransmitted so far
123 */
124 u_int retransmitted;
125
126 } responding;
127
128 /**
129 * Exchange we are currently handling as initiator
130 */
131 struct {
132 /**
133 * Message ID of the exchange
134 */
135 u_int32_t mid;
136
137 /**
138 * Hashes of old responses we can ignore
139 */
140 u_int32_t old_hashes[MAX_OLD_HASHES];
141
142 /**
143 * Position in old hash array
144 */
145 int old_hash_pos;
146
147 /**
148 * Sequence number of the last sent message
149 */
150 u_int32_t seqnr;
151
152 /**
153 * how many times we have retransmitted so far
154 */
155 u_int retransmitted;
156
157 /**
158 * packet for retransmission
159 */
160 packet_t *packet;
161
162 /**
163 * type of the initated exchange
164 */
165 exchange_type_t type;
166
167 } initiating;
168
169 /**
170 * List of queued tasks not yet in action
171 */
172 linked_list_t *queued_tasks;
173
174 /**
175 * List of active tasks, initiated by ourselve
176 */
177 linked_list_t *active_tasks;
178
179 /**
180 * List of tasks initiated by peer
181 */
182 linked_list_t *passive_tasks;
183
184 /**
185 * Queued messages not yet ready to process
186 */
187 message_t *queued;
188
189 /**
190 * Number of times we retransmit messages before giving up
191 */
192 u_int retransmit_tries;
193
194 /**
195 * Retransmission timeout
196 */
197 double retransmit_timeout;
198
199 /**
200 * Base to calculate retransmission timeout
201 */
202 double retransmit_base;
203
204 /**
205 * Sequence number for sending DPD requests
206 */
207 u_int32_t dpd_send;
208
209 /**
210 * Sequence number for received DPD requests
211 */
212 u_int32_t dpd_recv;
213 };
214
215 METHOD(task_manager_t, flush_queue, void,
216 private_task_manager_t *this, task_queue_t queue)
217 {
218 linked_list_t *list;
219 task_t *task;
220
221 if (this->queued)
222 {
223 this->queued->destroy(this->queued);
224 this->queued = NULL;
225 }
226 switch (queue)
227 {
228 case TASK_QUEUE_ACTIVE:
229 list = this->active_tasks;
230 break;
231 case TASK_QUEUE_PASSIVE:
232 list = this->passive_tasks;
233 break;
234 case TASK_QUEUE_QUEUED:
235 list = this->queued_tasks;
236 break;
237 default:
238 return;
239 }
240 while (list->remove_last(list, (void**)&task) == SUCCESS)
241 {
242 task->destroy(task);
243 }
244 }
245
246 /**
247 * flush all tasks in the task manager
248 */
249 static void flush(private_task_manager_t *this)
250 {
251 flush_queue(this, TASK_QUEUE_QUEUED);
252 flush_queue(this, TASK_QUEUE_PASSIVE);
253 flush_queue(this, TASK_QUEUE_ACTIVE);
254 }
255
256 /**
257 * move a task of a specific type from the queue to the active list
258 */
259 static bool activate_task(private_task_manager_t *this, task_type_t type)
260 {
261 enumerator_t *enumerator;
262 task_t *task;
263 bool found = FALSE;
264
265 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
266 while (enumerator->enumerate(enumerator, (void**)&task))
267 {
268 if (task->get_type(task) == type)
269 {
270 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
271 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
272 this->active_tasks->insert_last(this->active_tasks, task);
273 found = TRUE;
274 break;
275 }
276 }
277 enumerator->destroy(enumerator);
278 return found;
279 }
280
281 /**
282 * Retransmit a packet, either as initiator or as responder
283 */
284 static status_t retransmit_packet(private_task_manager_t *this, u_int32_t seqnr,
285 u_int mid, u_int retransmitted, packet_t *packet)
286 {
287 u_int32_t t;
288
289 if (retransmitted > this->retransmit_tries)
290 {
291 DBG1(DBG_IKE, "giving up after %u retransmits", retransmitted - 1);
292 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
293 {
294 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
295 }
296 return DESTROY_ME;
297 }
298 t = (u_int32_t)(this->retransmit_timeout * 1000.0 *
299 pow(this->retransmit_base, retransmitted));
300 if (retransmitted)
301 {
302 DBG1(DBG_IKE, "sending retransmit %u of %s message ID %u, seq %u",
303 retransmitted, seqnr < RESPONDING_SEQ ? "request" : "response",
304 mid, seqnr < RESPONDING_SEQ ? seqnr : seqnr - RESPONDING_SEQ);
305 }
306 charon->sender->send(charon->sender, packet->clone(packet));
307 lib->scheduler->schedule_job_ms(lib->scheduler, (job_t*)
308 retransmit_job_create(seqnr, this->ike_sa->get_id(this->ike_sa)), t);
309 return NEED_MORE;
310 }
311
312 METHOD(task_manager_t, retransmit, status_t,
313 private_task_manager_t *this, u_int32_t seqnr)
314 {
315 status_t status = SUCCESS;
316
317 if (seqnr == this->initiating.seqnr && this->initiating.packet)
318 {
319 status = retransmit_packet(this, seqnr, this->initiating.mid,
320 this->initiating.retransmitted, this->initiating.packet);
321 if (status == NEED_MORE)
322 {
323 this->initiating.retransmitted++;
324 status = SUCCESS;
325 }
326 }
327 if (seqnr == this->responding.seqnr && this->responding.packet)
328 {
329 status = retransmit_packet(this, seqnr, this->responding.mid,
330 this->responding.retransmitted, this->responding.packet);
331 if (status == NEED_MORE)
332 {
333 this->responding.retransmitted++;
334 status = SUCCESS;
335 }
336 }
337 return status;
338 }
339
340 METHOD(task_manager_t, initiate, status_t,
341 private_task_manager_t *this)
342 {
343 enumerator_t *enumerator;
344 task_t *task;
345 message_t *message;
346 host_t *me, *other;
347 status_t status;
348 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
349 bool new_mid = FALSE, expect_response = FALSE, cancelled = FALSE, keep = FALSE;
350
351 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED &&
352 this->initiating.type != INFORMATIONAL_V1)
353 {
354 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
355 exchange_type_names, this->initiating.type);
356 /* do not initiate if we already have a message in the air */
357 return SUCCESS;
358 }
359
360 if (this->active_tasks->get_count(this->active_tasks) == 0)
361 {
362 DBG2(DBG_IKE, "activating new tasks");
363 switch (this->ike_sa->get_state(this->ike_sa))
364 {
365 case IKE_CREATED:
366 activate_task(this, TASK_ISAKMP_VENDOR);
367 activate_task(this, TASK_ISAKMP_CERT_PRE);
368 if (activate_task(this, TASK_MAIN_MODE))
369 {
370 exchange = ID_PROT;
371 }
372 else if (activate_task(this, TASK_AGGRESSIVE_MODE))
373 {
374 exchange = AGGRESSIVE;
375 }
376 activate_task(this, TASK_ISAKMP_CERT_POST);
377 activate_task(this, TASK_ISAKMP_NATD);
378 break;
379 case IKE_CONNECTING:
380 if (activate_task(this, TASK_ISAKMP_DELETE))
381 {
382 exchange = INFORMATIONAL_V1;
383 new_mid = TRUE;
384 break;
385 }
386 if (activate_task(this, TASK_XAUTH))
387 {
388 exchange = TRANSACTION;
389 new_mid = TRUE;
390 break;
391 }
392 if (activate_task(this, TASK_INFORMATIONAL))
393 {
394 exchange = INFORMATIONAL_V1;
395 new_mid = TRUE;
396 break;
397 }
398 break;
399 case IKE_ESTABLISHED:
400 if (activate_task(this, TASK_MODE_CONFIG))
401 {
402 exchange = TRANSACTION;
403 new_mid = TRUE;
404 break;
405 }
406 if (activate_task(this, TASK_QUICK_MODE))
407 {
408 exchange = QUICK_MODE;
409 new_mid = TRUE;
410 break;
411 }
412 if (activate_task(this, TASK_INFORMATIONAL))
413 {
414 exchange = INFORMATIONAL_V1;
415 new_mid = TRUE;
416 break;
417 }
418 if (activate_task(this, TASK_QUICK_DELETE))
419 {
420 exchange = INFORMATIONAL_V1;
421 new_mid = TRUE;
422 break;
423 }
424 if (activate_task(this, TASK_ISAKMP_DELETE))
425 {
426 exchange = INFORMATIONAL_V1;
427 new_mid = TRUE;
428 break;
429 }
430 if (activate_task(this, TASK_ISAKMP_DPD))
431 {
432 exchange = INFORMATIONAL_V1;
433 new_mid = TRUE;
434 break;
435 }
436 break;
437 default:
438 break;
439 }
440 }
441 else
442 {
443 DBG2(DBG_IKE, "reinitiating already active tasks");
444 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
445 while (enumerator->enumerate(enumerator, (void**)&task))
446 {
447 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
448 switch (task->get_type(task))
449 {
450 case TASK_MAIN_MODE:
451 exchange = ID_PROT;
452 break;
453 case TASK_AGGRESSIVE_MODE:
454 exchange = AGGRESSIVE;
455 break;
456 case TASK_QUICK_MODE:
457 exchange = QUICK_MODE;
458 break;
459 case TASK_XAUTH:
460 exchange = TRANSACTION;
461 new_mid = TRUE;
462 break;
463 default:
464 continue;
465 }
466 break;
467 }
468 enumerator->destroy(enumerator);
469 }
470
471 if (exchange == EXCHANGE_TYPE_UNDEFINED)
472 {
473 DBG2(DBG_IKE, "nothing to initiate");
474 /* nothing to do yet... */
475 return SUCCESS;
476 }
477
478 me = this->ike_sa->get_my_host(this->ike_sa);
479 other = this->ike_sa->get_other_host(this->ike_sa);
480
481 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
482 if (new_mid)
483 {
484 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
485 (void*)&this->initiating.mid);
486 }
487 message->set_message_id(message, this->initiating.mid);
488 message->set_source(message, me->clone(me));
489 message->set_destination(message, other->clone(other));
490 message->set_exchange_type(message, exchange);
491 this->initiating.type = exchange;
492 this->initiating.retransmitted = 0;
493
494 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
495 while (enumerator->enumerate(enumerator, (void*)&task))
496 {
497 switch (task->build(task, message))
498 {
499 case SUCCESS:
500 /* task completed, remove it */
501 this->active_tasks->remove_at(this->active_tasks, enumerator);
502 if (task->get_type(task) == TASK_AGGRESSIVE_MODE ||
503 task->get_type(task) == TASK_QUICK_MODE)
504 { /* last message of three message exchange */
505 keep = TRUE;
506 }
507 task->destroy(task);
508 continue;
509 case NEED_MORE:
510 expect_response = TRUE;
511 /* processed, but task needs another exchange */
512 continue;
513 case ALREADY_DONE:
514 cancelled = TRUE;
515 break;
516 case FAILED:
517 default:
518 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
519 {
520 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
521 }
522 /* FALL */
523 case DESTROY_ME:
524 /* critical failure, destroy IKE_SA */
525 enumerator->destroy(enumerator);
526 message->destroy(message);
527 flush(this);
528 return DESTROY_ME;
529 }
530 break;
531 }
532 enumerator->destroy(enumerator);
533
534 if (this->active_tasks->get_count(this->active_tasks) == 0 &&
535 (exchange == QUICK_MODE || exchange == AGGRESSIVE))
536 { /* tasks completed, no exchange active anymore */
537 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
538 }
539 if (cancelled)
540 {
541 message->destroy(message);
542 return initiate(this);
543 }
544
545 DESTROY_IF(this->initiating.packet);
546 status = this->ike_sa->generate_message(this->ike_sa, message,
547 &this->initiating.packet);
548 if (status != SUCCESS)
549 {
550 /* message generation failed. There is nothing more to do than to
551 * close the SA */
552 message->destroy(message);
553 flush(this);
554 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
555 return DESTROY_ME;
556 }
557
558 this->initiating.seqnr++;
559 if (expect_response )
560 {
561 message->destroy(message);
562 return retransmit(this, this->initiating.seqnr);
563 }
564 if (keep)
565 { /* keep the packet for retransmission, the responder might request it */
566 charon->sender->send(charon->sender,
567 this->initiating.packet->clone(this->initiating.packet));
568 }
569 else
570 {
571 charon->sender->send(charon->sender, this->initiating.packet);
572 this->initiating.packet = NULL;
573 }
574 message->destroy(message);
575
576 if (exchange == INFORMATIONAL_V1)
577 {
578 switch (this->ike_sa->get_state(this->ike_sa))
579 {
580 case IKE_CONNECTING:
581 /* close after sending an INFORMATIONAL when unestablished */
582 return FAILED;
583 case IKE_DELETING:
584 /* close after sending a DELETE */
585 return DESTROY_ME;
586 default:
587 break;
588 }
589 }
590 return initiate(this);
591 }
592
593 /**
594 * build a response depending on the "passive" task list
595 */
596 static status_t build_response(private_task_manager_t *this, message_t *request)
597 {
598 enumerator_t *enumerator;
599 task_t *task;
600 message_t *message;
601 host_t *me, *other;
602 bool delete = FALSE, cancelled = FALSE, expect_request = FALSE;
603 status_t status;
604
605 me = request->get_destination(request);
606 other = request->get_source(request);
607
608 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
609 message->set_exchange_type(message, request->get_exchange_type(request));
610 /* send response along the path the request came in */
611 message->set_source(message, me->clone(me));
612 message->set_destination(message, other->clone(other));
613 message->set_message_id(message, request->get_message_id(request));
614 message->set_request(message, FALSE);
615
616 this->responding.mid = request->get_message_id(request);
617 this->responding.retransmitted = 0;
618 this->responding.seqnr++;
619
620 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
621 while (enumerator->enumerate(enumerator, (void*)&task))
622 {
623 switch (task->build(task, message))
624 {
625 case SUCCESS:
626 /* task completed, remove it */
627 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
628 task->destroy(task);
629 continue;
630 case NEED_MORE:
631 /* processed, but task needs another exchange */
632 if (task->get_type(task) == TASK_QUICK_MODE ||
633 task->get_type(task) == TASK_AGGRESSIVE_MODE)
634 { /* we rely on initiator retransmission, except for
635 * three-message exchanges */
636 expect_request = TRUE;
637 }
638 continue;
639 case ALREADY_DONE:
640 cancelled = TRUE;
641 break;
642 case FAILED:
643 default:
644 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
645 /* FALL */
646 case DESTROY_ME:
647 /* destroy IKE_SA, but SEND response first */
648 delete = TRUE;
649 break;
650 }
651 break;
652 }
653 enumerator->destroy(enumerator);
654
655 DESTROY_IF(this->responding.packet);
656 this->responding.packet = NULL;
657 if (cancelled)
658 {
659 message->destroy(message);
660 return initiate(this);
661 }
662 status = this->ike_sa->generate_message(this->ike_sa, message,
663 &this->responding.packet);
664 message->destroy(message);
665 if (status != SUCCESS)
666 {
667 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
668 return DESTROY_ME;
669 }
670
671 if (expect_request && !delete)
672 {
673 return retransmit(this, this->responding.seqnr);
674 }
675 charon->sender->send(charon->sender,
676 this->responding.packet->clone(this->responding.packet));
677 if (delete)
678 {
679 return DESTROY_ME;
680 }
681 return SUCCESS;
682 }
683
684 /**
685 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
686 * The notify protocol_id is set to ISAKMP
687 */
688 static void send_notify(private_task_manager_t *this, message_t *request,
689 notify_type_t type)
690 {
691 message_t *response;
692 packet_t *packet;
693 host_t *me, *other;
694 u_int32_t mid;
695
696 if (request && request->get_exchange_type(request) == INFORMATIONAL_V1)
697 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
698 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
699 return;
700 }
701
702 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
703 response->set_exchange_type(response, INFORMATIONAL_V1);
704 response->set_request(response, TRUE);
705 this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid);
706 response->set_message_id(response, mid);
707 response->add_payload(response, (payload_t*)
708 notify_payload_create_from_protocol_and_type(NOTIFY_V1,
709 PROTO_IKE, type));
710
711 me = this->ike_sa->get_my_host(this->ike_sa);
712 if (me->is_anyaddr(me))
713 {
714 me = request->get_destination(request);
715 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
716 }
717 other = this->ike_sa->get_other_host(this->ike_sa);
718 if (other->is_anyaddr(other))
719 {
720 other = request->get_source(request);
721 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
722 }
723 response->set_source(response, me->clone(me));
724 response->set_destination(response, other->clone(other));
725 if (this->ike_sa->generate_message(this->ike_sa, response,
726 &packet) == SUCCESS)
727 {
728 charon->sender->send(charon->sender, packet);
729 }
730 response->destroy(response);
731 }
732
733 /**
734 * Process a DPD request/response
735 */
736 static bool process_dpd(private_task_manager_t *this, message_t *message)
737 {
738 notify_payload_t *notify;
739 notify_type_t type;
740 u_int32_t seq;
741 chunk_t data;
742
743 type = DPD_R_U_THERE;
744 notify = message->get_notify(message, type);
745 if (!notify)
746 {
747 type = DPD_R_U_THERE_ACK;
748 notify = message->get_notify(message, type);
749 }
750 if (!notify)
751 {
752 return FALSE;
753 }
754 data = notify->get_notification_data(notify);
755 if (data.len != 4)
756 {
757 return FALSE;
758 }
759 seq = untoh32(data.ptr);
760
761 if (type == DPD_R_U_THERE)
762 {
763 if (this->dpd_recv == 0 || seq == this->dpd_recv)
764 { /* check sequence validity */
765 this->dpd_recv = seq + 1;
766 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
767 time_monotonic(NULL));
768 }
769 /* but respond anyway */
770 this->ike_sa->queue_task(this->ike_sa,
771 &isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE_ACK, seq)->task);
772 }
773 else /* DPD_R_U_THERE_ACK */
774 {
775 if (seq == this->dpd_send - 1)
776 {
777 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
778 time_monotonic(NULL));
779 }
780 else
781 {
782 DBG1(DBG_IKE, "received invalid DPD sequence number %u "
783 "(expected %u), ignored", seq, this->dpd_send - 1);
784 }
785 }
786 return TRUE;
787 }
788
789 /**
790 * handle an incoming request message
791 */
792 static status_t process_request(private_task_manager_t *this,
793 message_t *message)
794 {
795 enumerator_t *enumerator;
796 task_t *task = NULL;
797 bool send_response = FALSE, dpd = FALSE;
798
799 if (message->get_exchange_type(message) == INFORMATIONAL_V1 ||
800 this->passive_tasks->get_count(this->passive_tasks) == 0)
801 { /* create tasks depending on request type, if not already some queued */
802 switch (message->get_exchange_type(message))
803 {
804 case ID_PROT:
805 task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE);
806 this->passive_tasks->insert_last(this->passive_tasks, task);
807 task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE);
808 this->passive_tasks->insert_last(this->passive_tasks, task);
809 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
810 this->passive_tasks->insert_last(this->passive_tasks, task);
811 task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE);
812 this->passive_tasks->insert_last(this->passive_tasks, task);
813 task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE);
814 this->passive_tasks->insert_last(this->passive_tasks, task);
815 break;
816 case AGGRESSIVE:
817 task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE);
818 this->passive_tasks->insert_last(this->passive_tasks, task);
819 task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE);
820 this->passive_tasks->insert_last(this->passive_tasks, task);
821 task = (task_t *)aggressive_mode_create(this->ike_sa, FALSE);
822 this->passive_tasks->insert_last(this->passive_tasks, task);
823 task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE);
824 this->passive_tasks->insert_last(this->passive_tasks, task);
825 task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE);
826 this->passive_tasks->insert_last(this->passive_tasks, task);
827 break;
828 case QUICK_MODE:
829 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
830 {
831 DBG1(DBG_IKE, "received quick mode request for "
832 "unestablished IKE_SA, ignored");
833 return FAILED;
834 }
835 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
836 NULL, NULL);
837 this->passive_tasks->insert_last(this->passive_tasks, task);
838 break;
839 case INFORMATIONAL_V1:
840 if (process_dpd(this, message))
841 {
842 dpd = TRUE;
843 }
844 else
845 {
846 task = (task_t *)informational_create(this->ike_sa, NULL);
847 this->passive_tasks->insert_first(this->passive_tasks, task);
848 }
849 break;
850 case TRANSACTION:
851 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
852 {
853 task = (task_t *)mode_config_create(this->ike_sa, FALSE);
854 }
855 else
856 {
857 task = (task_t *)xauth_create(this->ike_sa, FALSE);
858 }
859 this->passive_tasks->insert_last(this->passive_tasks, task);
860 break;
861 default:
862 return FAILED;
863 }
864 }
865 if (dpd)
866 {
867 return initiate(this);
868 }
869 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, time_monotonic(NULL));
870
871 /* let the tasks process the message */
872 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
873 while (enumerator->enumerate(enumerator, (void*)&task))
874 {
875 switch (task->process(task, message))
876 {
877 case SUCCESS:
878 /* task completed, remove it */
879 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
880 task->destroy(task);
881 continue;
882 case NEED_MORE:
883 /* processed, but task needs at least another call to build() */
884 send_response = TRUE;
885 continue;
886 case ALREADY_DONE:
887 send_response = FALSE;
888 break;
889 case FAILED:
890 default:
891 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
892 /* FALL */
893 case DESTROY_ME:
894 /* critical failure, destroy IKE_SA */
895 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
896 enumerator->destroy(enumerator);
897 task->destroy(task);
898 return DESTROY_ME;
899 }
900 break;
901 }
902 enumerator->destroy(enumerator);
903
904 if (send_response)
905 {
906 if (build_response(this, message) != SUCCESS)
907 {
908 return DESTROY_ME;
909 }
910 }
911 else
912 { /* We don't send a response, so don't retransmit one if we get
913 * the same message again. */
914 DESTROY_IF(this->responding.packet);
915 this->responding.packet = NULL;
916 }
917 if (this->passive_tasks->get_count(this->passive_tasks) == 0 &&
918 this->queued_tasks->get_count(this->queued_tasks) > 0)
919 {
920 /* passive tasks completed, check if an active task has been queued,
921 * such as XAUTH or modeconfig push */
922 return initiate(this);
923 }
924 return SUCCESS;
925 }
926
927 /**
928 * handle an incoming response message
929 */
930 static status_t process_response(private_task_manager_t *this,
931 message_t *message)
932 {
933 enumerator_t *enumerator;
934 status_t status;
935 task_t *task;
936
937 if (message->get_exchange_type(message) != this->initiating.type)
938 {
939 DBG1(DBG_IKE, "received %N response, but expected %N",
940 exchange_type_names, message->get_exchange_type(message),
941 exchange_type_names, this->initiating.type);
942 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
943 return DESTROY_ME;
944 }
945
946 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
947 while (enumerator->enumerate(enumerator, (void*)&task))
948 {
949 switch (task->process(task, message))
950 {
951 case SUCCESS:
952 /* task completed, remove it */
953 this->active_tasks->remove_at(this->active_tasks, enumerator);
954 task->destroy(task);
955 continue;
956 case NEED_MORE:
957 /* processed, but task needs another exchange */
958 continue;
959 case ALREADY_DONE:
960 break;
961 case FAILED:
962 default:
963 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
964 /* FALL */
965 case DESTROY_ME:
966 /* critical failure, destroy IKE_SA */
967 this->active_tasks->remove_at(this->active_tasks, enumerator);
968 enumerator->destroy(enumerator);
969 task->destroy(task);
970 return DESTROY_ME;
971 }
972 break;
973 }
974 enumerator->destroy(enumerator);
975
976 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
977 this->initiating.packet->destroy(this->initiating.packet);
978 this->initiating.packet = NULL;
979
980 if (this->queued && this->active_tasks->get_count(this->active_tasks) == 0)
981 {
982 status = this->public.task_manager.process_message(
983 &this->public.task_manager, this->queued);
984 this->queued->destroy(this->queued);
985 this->queued = NULL;
986 if (status == DESTROY_ME)
987 {
988 return status;
989 }
990 }
991
992 return initiate(this);
993 }
994
995 /**
996 * Parse the given message and verify that it is valid.
997 */
998 static status_t parse_message(private_task_manager_t *this, message_t *msg)
999 {
1000 status_t status;
1001
1002 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
1003
1004 if (status != SUCCESS)
1005 {
1006 switch (status)
1007 {
1008 case NOT_SUPPORTED:
1009 DBG1(DBG_IKE, "unsupported exchange type");
1010 send_notify(this, msg, INVALID_EXCHANGE_TYPE);
1011 break;
1012 case PARSE_ERROR:
1013 DBG1(DBG_IKE, "message parsing failed");
1014 send_notify(this, msg, PAYLOAD_MALFORMED);
1015 break;
1016 case VERIFY_ERROR:
1017 DBG1(DBG_IKE, "message verification failed");
1018 send_notify(this, msg, PAYLOAD_MALFORMED);
1019 break;
1020 case FAILED:
1021 DBG1(DBG_IKE, "integrity check failed");
1022 send_notify(this, msg, INVALID_HASH_INFORMATION);
1023 break;
1024 case INVALID_STATE:
1025 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1026 send_notify(this, msg, PAYLOAD_MALFORMED);
1027 default:
1028 break;
1029 }
1030 DBG1(DBG_IKE, "%N %s with message ID %u processing failed",
1031 exchange_type_names, msg->get_exchange_type(msg),
1032 msg->get_request(msg) ? "request" : "response",
1033 msg->get_message_id(msg));
1034
1035 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
1036 { /* invalid initiation attempt, close SA */
1037 return DESTROY_ME;
1038 }
1039 }
1040 return status;
1041 }
1042
1043 METHOD(task_manager_t, process_message, status_t,
1044 private_task_manager_t *this, message_t *msg)
1045 {
1046 u_int32_t hash, mid, i;
1047 host_t *me, *other;
1048 status_t status;
1049
1050 /* TODO-IKEv1: update hosts more selectively */
1051 me = msg->get_destination(msg);
1052 other = msg->get_source(msg);
1053 mid = msg->get_message_id(msg);
1054 hash = chunk_hash(msg->get_packet_data(msg));
1055 for (i = 0; i < MAX_OLD_HASHES; i++)
1056 {
1057 if (this->initiating.old_hashes[i] == hash)
1058 {
1059 if (this->initiating.packet &&
1060 i == (this->initiating.old_hash_pos % MAX_OLD_HASHES) &&
1061 (msg->get_exchange_type(msg) == QUICK_MODE ||
1062 msg->get_exchange_type(msg) == AGGRESSIVE))
1063 {
1064 DBG1(DBG_IKE, "received retransmit of response with ID %u, "
1065 "resending last request", mid);
1066 charon->sender->send(charon->sender,
1067 this->initiating.packet->clone(this->initiating.packet));
1068 return SUCCESS;
1069 }
1070 DBG1(DBG_IKE, "received retransmit of response with ID %u, "
1071 "but next request already sent", mid);
1072 return SUCCESS;
1073 }
1074 }
1075
1076 if ((mid && mid == this->initiating.mid) ||
1077 (this->initiating.mid == 0 &&
1078 msg->get_exchange_type(msg) == this->initiating.type &&
1079 this->active_tasks->get_count(this->active_tasks)))
1080 {
1081 msg->set_request(msg, FALSE);
1082 charon->bus->message(charon->bus, msg, TRUE, FALSE);
1083 status = parse_message(this, msg);
1084 if (status != SUCCESS)
1085 {
1086 return status;
1087 }
1088 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
1089 time_monotonic(NULL));
1090 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
1091 charon->bus->message(charon->bus, msg, TRUE, TRUE);
1092 if (process_response(this, msg) != SUCCESS)
1093 {
1094 flush(this);
1095 return DESTROY_ME;
1096 }
1097 this->initiating.old_hashes[(++this->initiating.old_hash_pos) %
1098 MAX_OLD_HASHES] = hash;
1099 }
1100 else
1101 {
1102 if (hash == this->responding.hash)
1103 {
1104 if (this->responding.packet)
1105 {
1106 DBG1(DBG_IKE, "received retransmit of request with ID %u, "
1107 "retransmitting response", mid);
1108 charon->sender->send(charon->sender,
1109 this->responding.packet->clone(this->responding.packet));
1110 }
1111 else if (this->initiating.packet &&
1112 this->initiating.type == INFORMATIONAL_V1)
1113 {
1114 DBG1(DBG_IKE, "received retransmit of DPD request, "
1115 "retransmitting response");
1116 charon->sender->send(charon->sender,
1117 this->initiating.packet->clone(this->initiating.packet));
1118 }
1119 else
1120 {
1121 DBG1(DBG_IKE, "received retransmit of request with ID %u, "
1122 "but no response to retransmit", mid);
1123 }
1124 return SUCCESS;
1125 }
1126 if (msg->get_exchange_type(msg) == TRANSACTION &&
1127 this->active_tasks->get_count(this->active_tasks))
1128 { /* main mode not yet complete, queue XAuth/Mode config tasks */
1129 if (this->queued)
1130 {
1131 DBG1(DBG_IKE, "ignoring additional %N request, queue full",
1132 exchange_type_names, TRANSACTION);
1133 return SUCCESS;
1134 }
1135 this->queued = message_create_from_packet(msg->get_packet(msg));
1136 if (this->queued->parse_header(this->queued) != SUCCESS)
1137 {
1138 this->queued->destroy(this->queued);
1139 this->queued = NULL;
1140 return FAILED;
1141 }
1142 DBG1(DBG_IKE, "queueing %N request as tasks still active",
1143 exchange_type_names, TRANSACTION);
1144 return SUCCESS;
1145 }
1146
1147 msg->set_request(msg, TRUE);
1148 charon->bus->message(charon->bus, msg, TRUE, FALSE);
1149 status = parse_message(this, msg);
1150 if (status != SUCCESS)
1151 {
1152 return status;
1153 }
1154 /* if this IKE_SA is virgin, we check for a config */
1155 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
1156 {
1157 ike_sa_id_t *ike_sa_id;
1158 ike_cfg_t *ike_cfg;
1159 job_t *job;
1160
1161 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
1162 if (ike_cfg == NULL)
1163 {
1164 /* no config found for these hosts, destroy */
1165 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1166 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1167 send_notify(this, msg, NO_PROPOSAL_CHOSEN);
1168 return DESTROY_ME;
1169 }
1170 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
1171 ike_cfg->destroy(ike_cfg);
1172 /* add a timeout if peer does not establish it completely */
1173 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
1174 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
1175 lib->scheduler->schedule_job(lib->scheduler, job,
1176 lib->settings->get_int(lib->settings,
1177 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT,
1178 charon->name));
1179 }
1180 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
1181 charon->bus->message(charon->bus, msg, TRUE, TRUE);
1182 if (process_request(this, msg) != SUCCESS)
1183 {
1184 flush(this);
1185 return DESTROY_ME;
1186 }
1187 this->responding.hash = hash;
1188 }
1189 return SUCCESS;
1190 }
1191
1192 METHOD(task_manager_t, queue_task, void,
1193 private_task_manager_t *this, task_t *task)
1194 {
1195 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
1196 this->queued_tasks->insert_last(this->queued_tasks, task);
1197 }
1198
1199 /**
1200 * Check if a given task has been queued already
1201 */
1202 static bool has_queued(private_task_manager_t *this, task_type_t type)
1203 {
1204 enumerator_t *enumerator;
1205 bool found = FALSE;
1206 task_t *task;
1207
1208 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1209 while (enumerator->enumerate(enumerator, &task))
1210 {
1211 if (task->get_type(task) == type)
1212 {
1213 found = TRUE;
1214 break;
1215 }
1216 }
1217 enumerator->destroy(enumerator);
1218 return found;
1219 }
1220
1221 METHOD(task_manager_t, queue_ike, void,
1222 private_task_manager_t *this)
1223 {
1224 peer_cfg_t *peer_cfg;
1225
1226 if (!has_queued(this, TASK_ISAKMP_VENDOR))
1227 {
1228 queue_task(this, (task_t*)isakmp_vendor_create(this->ike_sa, TRUE));
1229 }
1230 if (!has_queued(this, TASK_ISAKMP_CERT_PRE))
1231 {
1232 queue_task(this, (task_t*)isakmp_cert_pre_create(this->ike_sa, TRUE));
1233 }
1234 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1235 if (peer_cfg->use_aggressive(peer_cfg))
1236 {
1237 if (!has_queued(this, TASK_AGGRESSIVE_MODE))
1238 {
1239 queue_task(this, (task_t*)aggressive_mode_create(this->ike_sa, TRUE));
1240 }
1241 }
1242 else
1243 {
1244 if (!has_queued(this, TASK_MAIN_MODE))
1245 {
1246 queue_task(this, (task_t*)main_mode_create(this->ike_sa, TRUE));
1247 }
1248 }
1249 if (!has_queued(this, TASK_ISAKMP_CERT_POST))
1250 {
1251 queue_task(this, (task_t*)isakmp_cert_post_create(this->ike_sa, TRUE));
1252 }
1253 if (!has_queued(this, TASK_ISAKMP_NATD))
1254 {
1255 queue_task(this, (task_t*)isakmp_natd_create(this->ike_sa, TRUE));
1256 }
1257 }
1258
1259 METHOD(task_manager_t, queue_ike_reauth, void,
1260 private_task_manager_t *this)
1261 {
1262 enumerator_t *enumerator;
1263 child_sa_t *child_sa;
1264 ike_sa_t *new;
1265 host_t *host;
1266
1267 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1268 this->ike_sa->get_version(this->ike_sa), TRUE);
1269 if (!new)
1270 { /* shouldn't happen */
1271 return;
1272 }
1273
1274 new->set_peer_cfg(new, this->ike_sa->get_peer_cfg(this->ike_sa));
1275 host = this->ike_sa->get_other_host(this->ike_sa);
1276 new->set_other_host(new, host->clone(host));
1277 host = this->ike_sa->get_my_host(this->ike_sa);
1278 new->set_my_host(new, host->clone(host));
1279 host = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
1280 if (host)
1281 {
1282 new->set_virtual_ip(new, TRUE, host);
1283 }
1284
1285 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1286 while (enumerator->enumerate(enumerator, &child_sa))
1287 {
1288 this->ike_sa->remove_child_sa(this->ike_sa, enumerator);
1289 new->add_child_sa(new, child_sa);
1290 }
1291 enumerator->destroy(enumerator);
1292
1293 if (!new->get_child_count(new))
1294 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1295 task_t *task;
1296
1297 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1298 while (enumerator->enumerate(enumerator, &task))
1299 {
1300 if (task->get_type(task) == TASK_QUICK_MODE)
1301 {
1302 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
1303 task->migrate(task, new);
1304 new->queue_task(new, task);
1305 }
1306 }
1307 enumerator->destroy(enumerator);
1308 }
1309
1310 if (new->initiate(new, NULL, 0, NULL, NULL) != DESTROY_ME)
1311 {
1312 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1313 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
1314 }
1315 else
1316 {
1317 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1318 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
1319 }
1320 charon->bus->set_sa(charon->bus, this->ike_sa);
1321 }
1322
1323 METHOD(task_manager_t, queue_ike_rekey, void,
1324 private_task_manager_t *this)
1325 {
1326 queue_ike_reauth(this);
1327 }
1328
1329 METHOD(task_manager_t, queue_ike_delete, void,
1330 private_task_manager_t *this)
1331 {
1332 enumerator_t *enumerator;
1333 child_sa_t *child_sa;
1334
1335 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1336 while (enumerator->enumerate(enumerator, &child_sa))
1337 {
1338 queue_task(this, (task_t*)
1339 quick_delete_create(this->ike_sa, child_sa->get_protocol(child_sa),
1340 child_sa->get_spi(child_sa, TRUE), FALSE, FALSE));
1341 }
1342 enumerator->destroy(enumerator);
1343
1344 queue_task(this, (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
1345 }
1346
1347 METHOD(task_manager_t, queue_mobike, void,
1348 private_task_manager_t *this, bool roam, bool address)
1349 {
1350 /* Not supported in IKEv1 */
1351 }
1352
1353 METHOD(task_manager_t, queue_child, void,
1354 private_task_manager_t *this, child_cfg_t *cfg, u_int32_t reqid,
1355 traffic_selector_t *tsi, traffic_selector_t *tsr)
1356 {
1357 quick_mode_t *task;
1358
1359 task = quick_mode_create(this->ike_sa, cfg, tsi, tsr);
1360 task->use_reqid(task, reqid);
1361
1362 queue_task(this, &task->task);
1363 }
1364
1365 METHOD(task_manager_t, queue_child_rekey, void,
1366 private_task_manager_t *this, protocol_id_t protocol, u_int32_t spi)
1367 {
1368 child_sa_t *child_sa;
1369 child_cfg_t *cfg;
1370 quick_mode_t *task;
1371
1372 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE);
1373 if (!child_sa)
1374 {
1375 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE);
1376 }
1377 if (child_sa && child_sa->get_state(child_sa) == CHILD_INSTALLED)
1378 {
1379 child_sa->set_state(child_sa, CHILD_REKEYING);
1380 cfg = child_sa->get_config(child_sa);
1381 task = quick_mode_create(this->ike_sa, cfg->get_ref(cfg), NULL, NULL);
1382 task->use_reqid(task, child_sa->get_reqid(child_sa));
1383 task->rekey(task, child_sa->get_spi(child_sa, TRUE));
1384
1385 queue_task(this, &task->task);
1386 }
1387 }
1388
1389 METHOD(task_manager_t, queue_child_delete, void,
1390 private_task_manager_t *this, protocol_id_t protocol, u_int32_t spi,
1391 bool expired)
1392 {
1393 queue_task(this, (task_t*)quick_delete_create(this->ike_sa, protocol,
1394 spi, FALSE, expired));
1395 }
1396
1397 METHOD(task_manager_t, queue_dpd, void,
1398 private_task_manager_t *this)
1399 {
1400 peer_cfg_t *peer_cfg;
1401 u_int32_t t, retransmit;
1402
1403 queue_task(this, (task_t*)isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE,
1404 this->dpd_send++));
1405 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1406
1407 /* compute timeout in milliseconds */
1408 t = 1000 * peer_cfg->get_dpd_timeout(peer_cfg);
1409 if (t == 0)
1410 {
1411 /* use the same timeout as a retransmitting IKE message would have */
1412 for (retransmit = 0; retransmit <= this->retransmit_tries; retransmit++)
1413 {
1414 t += (u_int32_t)(this->retransmit_timeout * 1000.0 *
1415 pow(this->retransmit_base, retransmit));
1416 }
1417 }
1418
1419 /* schedule DPD timeout job */
1420 lib->scheduler->schedule_job_ms(lib->scheduler,
1421 (job_t*)dpd_timeout_job_create(this->ike_sa->get_id(this->ike_sa)), t);
1422 }
1423
1424 METHOD(task_manager_t, adopt_tasks, void,
1425 private_task_manager_t *this, task_manager_t *other_public)
1426 {
1427 private_task_manager_t *other = (private_task_manager_t*)other_public;
1428 task_t *task;
1429
1430 /* move queued tasks from other to this */
1431 while (other->queued_tasks->remove_last(other->queued_tasks,
1432 (void**)&task) == SUCCESS)
1433 {
1434 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
1435 task->migrate(task, this->ike_sa);
1436 this->queued_tasks->insert_first(this->queued_tasks, task);
1437 }
1438 }
1439
1440 METHOD(task_manager_t, busy, bool,
1441 private_task_manager_t *this)
1442 {
1443 return (this->active_tasks->get_count(this->active_tasks) > 0);
1444 }
1445
1446 METHOD(task_manager_t, incr_mid, void,
1447 private_task_manager_t *this, bool initiate)
1448 {
1449 }
1450
1451 METHOD(task_manager_t, reset, void,
1452 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
1453 {
1454 enumerator_t *enumerator;
1455 task_t *task;
1456
1457 /* reset message counters and retransmit packets */
1458 DESTROY_IF(this->responding.packet);
1459 DESTROY_IF(this->initiating.packet);
1460 this->responding.packet = NULL;
1461 this->responding.seqnr = RESPONDING_SEQ;
1462 this->responding.retransmitted = 0;
1463 this->initiating.packet = NULL;
1464 this->initiating.mid = 0;
1465 this->initiating.seqnr = 0;
1466 this->initiating.retransmitted = 0;
1467 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1468 if (initiate != UINT_MAX)
1469 {
1470 this->dpd_send = initiate;
1471 }
1472 if (respond != UINT_MAX)
1473 {
1474 this->dpd_recv = respond;
1475 }
1476
1477 /* reset queued tasks */
1478 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1479 while (enumerator->enumerate(enumerator, &task))
1480 {
1481 task->migrate(task, this->ike_sa);
1482 }
1483 enumerator->destroy(enumerator);
1484
1485 /* reset active tasks */
1486 while (this->active_tasks->remove_last(this->active_tasks,
1487 (void**)&task) == SUCCESS)
1488 {
1489 task->migrate(task, this->ike_sa);
1490 this->queued_tasks->insert_first(this->queued_tasks, task);
1491 }
1492 }
1493
1494 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
1495 private_task_manager_t *this, task_queue_t queue)
1496 {
1497 switch (queue)
1498 {
1499 case TASK_QUEUE_ACTIVE:
1500 return this->active_tasks->create_enumerator(this->active_tasks);
1501 case TASK_QUEUE_PASSIVE:
1502 return this->passive_tasks->create_enumerator(this->passive_tasks);
1503 case TASK_QUEUE_QUEUED:
1504 return this->queued_tasks->create_enumerator(this->queued_tasks);
1505 default:
1506 return enumerator_create_empty();
1507 }
1508 }
1509
1510 METHOD(task_manager_t, destroy, void,
1511 private_task_manager_t *this)
1512 {
1513 flush(this);
1514
1515 this->active_tasks->destroy(this->active_tasks);
1516 this->queued_tasks->destroy(this->queued_tasks);
1517 this->passive_tasks->destroy(this->passive_tasks);
1518
1519 DESTROY_IF(this->queued);
1520 DESTROY_IF(this->responding.packet);
1521 DESTROY_IF(this->initiating.packet);
1522 DESTROY_IF(this->rng);
1523 free(this);
1524 }
1525
1526 /*
1527 * see header file
1528 */
1529 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
1530 {
1531 private_task_manager_t *this;
1532
1533 INIT(this,
1534 .public = {
1535 .task_manager = {
1536 .process_message = _process_message,
1537 .queue_task = _queue_task,
1538 .queue_ike = _queue_ike,
1539 .queue_ike_rekey = _queue_ike_rekey,
1540 .queue_ike_reauth = _queue_ike_reauth,
1541 .queue_ike_delete = _queue_ike_delete,
1542 .queue_mobike = _queue_mobike,
1543 .queue_child = _queue_child,
1544 .queue_child_rekey = _queue_child_rekey,
1545 .queue_child_delete = _queue_child_delete,
1546 .queue_dpd = _queue_dpd,
1547 .initiate = _initiate,
1548 .retransmit = _retransmit,
1549 .incr_mid = _incr_mid,
1550 .reset = _reset,
1551 .adopt_tasks = _adopt_tasks,
1552 .busy = _busy,
1553 .create_task_enumerator = _create_task_enumerator,
1554 .flush_queue = _flush_queue,
1555 .destroy = _destroy,
1556 },
1557 },
1558 .initiating = {
1559 .type = EXCHANGE_TYPE_UNDEFINED,
1560 },
1561 .responding = {
1562 .seqnr = RESPONDING_SEQ,
1563 },
1564 .ike_sa = ike_sa,
1565 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
1566 .queued_tasks = linked_list_create(),
1567 .active_tasks = linked_list_create(),
1568 .passive_tasks = linked_list_create(),
1569 .retransmit_tries = lib->settings->get_int(lib->settings,
1570 "%s.retransmit_tries", RETRANSMIT_TRIES, charon->name),
1571 .retransmit_timeout = lib->settings->get_double(lib->settings,
1572 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT, charon->name),
1573 .retransmit_base = lib->settings->get_double(lib->settings,
1574 "%s.retransmit_base", RETRANSMIT_BASE, charon->name),
1575 );
1576
1577 if (!this->rng)
1578 {
1579 DBG1(DBG_IKE, "no RNG found, unable to create IKE_SA");
1580 destroy(this);
1581 return NULL;
1582 }
1583
1584 this->rng->get_bytes(this->rng, sizeof(this->dpd_send),
1585 (void*)&this->dpd_send);
1586 this->dpd_send &= 0x7FFFFFFF;
1587
1588 return &this->public;
1589 }
1590