IKEv1 XAuth: Remove XAuth task from the passive task list for ID_PROT.
[strongswan.git] / src / libcharon / sa / 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/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>
29
30 typedef struct exchange_t exchange_t;
31
32 /**
33 * An exchange in the air, used do detect and handle retransmission
34 */
35 struct exchange_t {
36
37 /**
38 * Message ID used for this transaction
39 */
40 u_int32_t mid;
41
42 /**
43 * generated packet for retransmission
44 */
45 packet_t *packet;
46 };
47
48 typedef struct private_task_manager_t private_task_manager_t;
49
50 /**
51 * private data of the task manager
52 */
53 struct private_task_manager_t {
54
55 /**
56 * public functions
57 */
58 task_manager_v1_t public;
59
60 /**
61 * associated IKE_SA we are serving
62 */
63 ike_sa_t *ike_sa;
64
65 /**
66 * RNG to create message IDs
67 */
68 rng_t *rng;
69
70 /**
71 * Exchange we are currently handling as responder
72 */
73 struct {
74 /**
75 * Message ID of the exchange
76 */
77 u_int32_t mid;
78
79 /**
80 * Hash of a previously received message
81 */
82 u_int32_t hash;
83
84 /**
85 * packet for retransmission
86 */
87 packet_t *packet;
88
89 } responding;
90
91 /**
92 * Exchange we are currently handling as initiator
93 */
94 struct {
95 /**
96 * Message ID of the exchange
97 */
98 u_int32_t mid;
99
100 /**
101 * Hash of a previously received message
102 */
103 u_int32_t hash;
104
105 /**
106 * how many times we have retransmitted so far
107 */
108 u_int retransmitted;
109
110 /**
111 * packet for retransmission
112 */
113 packet_t *packet;
114
115 /**
116 * type of the initated exchange
117 */
118 exchange_type_t type;
119
120 } initiating;
121
122 /**
123 * List of queued tasks not yet in action
124 */
125 linked_list_t *queued_tasks;
126
127 /**
128 * List of active tasks, initiated by ourselve
129 */
130 linked_list_t *active_tasks;
131
132 /**
133 * List of tasks initiated by peer
134 */
135 linked_list_t *passive_tasks;
136
137 /**
138 * Number of times we retransmit messages before giving up
139 */
140 u_int retransmit_tries;
141
142 /**
143 * Retransmission timeout
144 */
145 double retransmit_timeout;
146
147 /**
148 * Base to calculate retransmission timeout
149 */
150 double retransmit_base;
151 };
152
153 /**
154 * flush all tasks in the task manager
155 */
156 static void flush(private_task_manager_t *this)
157 {
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();
167 }
168
169 /**
170 * move a task of a specific type from the queue to the active list
171 */
172 static bool activate_task(private_task_manager_t *this, task_type_t type)
173 {
174 enumerator_t *enumerator;
175 task_t *task;
176 bool found = FALSE;
177
178 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
179 while (enumerator->enumerate(enumerator, (void**)&task))
180 {
181 if (task->get_type(task) == type)
182 {
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);
186 found = TRUE;
187 break;
188 }
189 }
190 enumerator->destroy(enumerator);
191 return found;
192 }
193
194 METHOD(task_manager_t, retransmit, status_t,
195 private_task_manager_t *this, u_int32_t message_id)
196 {
197 if (message_id == this->initiating.mid)
198 {
199 u_int32_t timeout;
200 packet_t *packet;
201 job_t *job;
202
203 if (this->initiating.retransmitted <= this->retransmit_tries)
204 {
205 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
206 pow(this->retransmit_base, this->initiating.retransmitted));
207 }
208 else
209 {
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)
213 {
214 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
215 }
216 return DESTROY_ME;
217 }
218
219 if (this->initiating.retransmitted)
220 {
221 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
222 this->initiating.retransmitted, message_id);
223 }
224 packet = this->initiating.packet->clone(this->initiating.packet);
225 charon->sender->send(charon->sender, packet);
226
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);
231 }
232 return SUCCESS;
233 }
234
235 METHOD(task_manager_t, initiate, status_t,
236 private_task_manager_t *this)
237 {
238 enumerator_t *enumerator;
239 task_t *task;
240 message_t *message;
241 host_t *me, *other;
242 status_t status;
243 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
244 bool new_mid = FALSE;
245
246 if (!this->rng)
247 {
248 DBG1(DBG_IKE, "no RNG supported");
249 return FAILED;
250 }
251
252 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
253 {
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 */
257 return SUCCESS;
258 }
259
260 if (this->active_tasks->get_count(this->active_tasks) == 0)
261 {
262 DBG2(DBG_IKE, "activating new tasks");
263 switch (this->ike_sa->get_state(this->ike_sa))
264 {
265 case IKE_CREATED:
266 activate_task(this, TASK_VENDOR_V1);
267 if (activate_task(this, TASK_MAIN_MODE))
268 {
269 exchange = ID_PROT;
270 }
271 break;
272 case IKE_ESTABLISHED:
273 if (activate_task(this, TASK_QUICK_MODE))
274 {
275 exchange = QUICK_MODE;
276 new_mid = TRUE;
277 break;
278 }
279 if (activate_task(this, TASK_XAUTH_REQUEST))
280 {
281 exchange = TRANSACTION;
282 new_mid = TRUE;
283 break;
284 }
285 break;
286 default:
287 break;
288 }
289 }
290 else
291 {
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))
295 {
296 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
297 switch (task->get_type(task))
298 {
299 case TASK_MAIN_MODE:
300 exchange = ID_PROT;
301 break;
302 case TASK_QUICK_MODE:
303 exchange = QUICK_MODE;
304 break;
305 case TASK_XAUTH_REQUEST:
306 exchange = TRANSACTION;
307 new_mid = TRUE;
308 break;
309 default:
310 continue;
311 }
312 break;
313 }
314 enumerator->destroy(enumerator);
315 }
316
317 if (exchange == EXCHANGE_TYPE_UNDEFINED)
318 {
319 DBG2(DBG_IKE, "nothing to initiate");
320 /* nothing to do yet... */
321 return SUCCESS;
322 }
323
324 me = this->ike_sa->get_my_host(this->ike_sa);
325 other = this->ike_sa->get_other_host(this->ike_sa);
326
327 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
328 if (new_mid)
329 {
330 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
331 (void*)&this->initiating.mid);
332 }
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;
339
340 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
341 while (enumerator->enumerate(enumerator, (void*)&task))
342 {
343 switch (task->build(task, message))
344 {
345 case SUCCESS:
346 /* task completed, remove it */
347 this->active_tasks->remove_at(this->active_tasks, enumerator);
348 task->destroy(task);
349 break;
350 case NEED_MORE:
351 /* processed, but task needs another exchange */
352 break;
353 case FAILED:
354 default:
355 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
356 {
357 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
358 }
359 /* FALL */
360 case DESTROY_ME:
361 /* critical failure, destroy IKE_SA */
362 enumerator->destroy(enumerator);
363 message->destroy(message);
364 flush(this);
365 return DESTROY_ME;
366 }
367 }
368 enumerator->destroy(enumerator);
369
370 /* update exchange type if a task changed it */
371 this->initiating.type = message->get_exchange_type(message);
372
373 status = this->ike_sa->generate_message(this->ike_sa, message,
374 &this->initiating.packet);
375 if (status != SUCCESS)
376 {
377 /* message generation failed. There is nothing more to do than to
378 * close the SA */
379 message->destroy(message);
380 flush(this);
381 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
382 return DESTROY_ME;
383 }
384 message->destroy(message);
385
386 charon->sender->send(charon->sender,
387 this->initiating.packet->clone(this->initiating.packet));
388
389 return SUCCESS;
390 }
391
392 /**
393 * handle exchange collisions
394 */
395 static bool handle_collisions(private_task_manager_t *this, task_t *task)
396 {
397 return FALSE;
398 }
399
400 /**
401 * build a response depending on the "passive" task list
402 */
403 static status_t build_response(private_task_manager_t *this, message_t *request)
404 {
405 enumerator_t *enumerator;
406 task_t *task;
407 message_t *message;
408 host_t *me, *other;
409 bool delete = FALSE;
410 status_t status;
411
412 me = request->get_destination(request);
413 other = request->get_source(request);
414
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);
422
423 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
424 while (enumerator->enumerate(enumerator, (void*)&task))
425 {
426 switch (task->build(task, message))
427 {
428 case SUCCESS:
429 /* task completed, remove it */
430 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
431 if (!handle_collisions(this, task))
432 {
433 task->destroy(task);
434 }
435 break;
436 case NEED_MORE:
437 /* processed, but task needs another exchange */
438 if (handle_collisions(this, task))
439 {
440 this->passive_tasks->remove_at(this->passive_tasks,
441 enumerator);
442 }
443 break;
444 case FAILED:
445 default:
446 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
447 /* FALL */
448 case DESTROY_ME:
449 /* destroy IKE_SA, but SEND response first */
450 delete = TRUE;
451 break;
452 }
453 if (delete)
454 {
455 break;
456 }
457 }
458 enumerator->destroy(enumerator);
459
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)
467 {
468 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
469 return DESTROY_ME;
470 }
471
472 charon->sender->send(charon->sender,
473 this->responding.packet->clone(this->responding.packet));
474 if (delete)
475 {
476 return DESTROY_ME;
477 }
478 return SUCCESS;
479 }
480
481 /**
482 * handle an incoming request message
483 */
484 static status_t process_request(private_task_manager_t *this,
485 message_t *message)
486 {
487 enumerator_t *enumerator;
488 task_t *task = NULL;
489
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))
493 {
494 case ID_PROT:
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 break;
500 case AGGRESSIVE:
501 /* TODO-IKEv1: agressive mode */
502 return FAILED;
503 case QUICK_MODE:
504 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
505 NULL, NULL);
506 this->passive_tasks->insert_last(this->passive_tasks, task);
507 break;
508 case INFORMATIONAL_V1:
509 /* TODO-IKEv1: informational */
510 return FAILED;
511 default:
512 return FAILED;
513 }
514 }
515 /* let the tasks process the message */
516 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
517 while (enumerator->enumerate(enumerator, (void*)&task))
518 {
519 switch (task->process(task, message))
520 {
521 case SUCCESS:
522 /* task completed, remove it */
523 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
524 task->destroy(task);
525 enumerator->destroy(enumerator);
526 return SUCCESS;
527 case NEED_MORE:
528 /* processed, but task needs at least another call to build() */
529 break;
530 case FAILED:
531 default:
532 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
533 /* FALL */
534 case DESTROY_ME:
535 /* critical failure, destroy IKE_SA */
536 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
537 enumerator->destroy(enumerator);
538 task->destroy(task);
539 return DESTROY_ME;
540 }
541 }
542 enumerator->destroy(enumerator);
543
544 return build_response(this, message);
545 }
546
547 /**
548 * handle an incoming response message
549 */
550 static status_t process_response(private_task_manager_t *this,
551 message_t *message)
552 {
553 enumerator_t *enumerator;
554 task_t *task;
555
556 if (message->get_exchange_type(message) != this->initiating.type)
557 {
558 DBG1(DBG_IKE, "received %N response, but expected %N",
559 exchange_type_names, message->get_exchange_type(message),
560 exchange_type_names, this->initiating.type);
561 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
562 return DESTROY_ME;
563 }
564
565 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
566 while (enumerator->enumerate(enumerator, (void*)&task))
567 {
568 switch (task->process(task, message))
569 {
570 case SUCCESS:
571 /* task completed, remove it */
572 this->active_tasks->remove_at(this->active_tasks, enumerator);
573 task->destroy(task);
574 break;
575 case NEED_MORE:
576 /* processed, but task needs another exchange */
577 break;
578 case FAILED:
579 default:
580 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
581 /* FALL */
582 case DESTROY_ME:
583 /* critical failure, destroy IKE_SA */
584 this->active_tasks->remove_at(this->active_tasks, enumerator);
585 enumerator->destroy(enumerator);
586 task->destroy(task);
587 return DESTROY_ME;
588 }
589 }
590 enumerator->destroy(enumerator);
591
592 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
593 this->initiating.packet->destroy(this->initiating.packet);
594 this->initiating.packet = NULL;
595
596 return initiate(this);
597 }
598
599 /**
600 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
601 */
602 static void send_notify_response(private_task_manager_t *this,
603 message_t *request, notify_type_t type,
604 chunk_t data)
605 {
606 message_t *response;
607 packet_t *packet;
608 host_t *me, *other;
609 u_int32_t mid;
610
611 if (request->get_exchange_type(request) == INFORMATIONAL_V1)
612 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
613 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
614 return;
615 }
616
617 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
618 response->set_exchange_type(response, INFORMATIONAL_V1);
619 response->set_request(response, TRUE);
620 this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid);
621 response->set_message_id(response, mid);
622 response->add_notify(response, FALSE, type, data);
623 me = this->ike_sa->get_my_host(this->ike_sa);
624 if (me->is_anyaddr(me))
625 {
626 me = request->get_destination(request);
627 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
628 }
629 other = this->ike_sa->get_other_host(this->ike_sa);
630 if (other->is_anyaddr(other))
631 {
632 other = request->get_source(request);
633 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
634 }
635 response->set_source(response, me->clone(me));
636 response->set_destination(response, other->clone(other));
637 if (this->ike_sa->generate_message(this->ike_sa, response,
638 &packet) == SUCCESS)
639 {
640 charon->sender->send(charon->sender, packet);
641 }
642 response->destroy(response);
643 }
644
645 /**
646 * Parse the given message and verify that it is valid.
647 */
648 static status_t parse_message(private_task_manager_t *this, message_t *msg)
649 {
650 status_t status;
651
652 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
653
654 if (status != SUCCESS)
655 {
656 switch (status)
657 {
658 case NOT_SUPPORTED:
659 DBG1(DBG_IKE, "unsupported exchange type");
660 send_notify_response(this, msg,
661 INVALID_EXCHANGE_TYPE, chunk_empty);
662 break;
663 case PARSE_ERROR:
664 DBG1(DBG_IKE, "message parsing failed");
665 send_notify_response(this, msg,
666 PAYLOAD_MALFORMED, chunk_empty);
667 break;
668 case VERIFY_ERROR:
669 DBG1(DBG_IKE, "message verification failed");
670 send_notify_response(this, msg,
671 PAYLOAD_MALFORMED, chunk_empty);
672 break;
673 case FAILED:
674 DBG1(DBG_IKE, "integrity check failed");
675 send_notify_response(this, msg,
676 INVALID_HASH_INFORMATION, chunk_empty);
677 break;
678 case INVALID_STATE:
679 DBG1(DBG_IKE, "found encrypted message, but no keys available");
680 send_notify_response(this, msg,
681 PAYLOAD_MALFORMED, chunk_empty);
682 default:
683 break;
684 }
685 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
686 exchange_type_names, msg->get_exchange_type(msg),
687 msg->get_request(msg) ? "request" : "response",
688 msg->get_message_id(msg));
689
690 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
691 { /* invalid initiation attempt, close SA */
692 return DESTROY_ME;
693 }
694 }
695 return status;
696 }
697
698 METHOD(task_manager_t, process_message, status_t,
699 private_task_manager_t *this, message_t *msg)
700 {
701 u_int32_t hash, mid;
702 host_t *me, *other;
703 status_t status;
704
705 /* TODO-IKEv1: update hosts more selectively */
706 me = msg->get_destination(msg);
707 other = msg->get_source(msg);
708 mid = msg->get_message_id(msg);
709
710 if ((mid && mid == this->initiating.mid) ||
711 (this->initiating.mid == 0 &&
712 this->active_tasks->get_count(this->active_tasks)))
713 {
714 msg->set_request(msg, FALSE);
715 status = parse_message(this, msg);
716 if (status != SUCCESS)
717 {
718 return status;
719 }
720 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
721 time_monotonic(NULL));
722 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
723 charon->bus->message(charon->bus, msg, FALSE);
724 if (process_response(this, msg) != SUCCESS)
725 {
726 flush(this);
727 return DESTROY_ME;
728 }
729 }
730 else
731 {
732 hash = chunk_hash(msg->get_packet_data(msg));
733 if (hash == this->responding.hash)
734 {
735 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
736 "retransmitting response", mid);
737 charon->sender->send(charon->sender,
738 this->responding.packet->clone(this->responding.packet));
739 return SUCCESS;
740 }
741 msg->set_request(msg, TRUE);
742 status = parse_message(this, msg);
743 if (status != SUCCESS)
744 {
745 return status;
746 }
747 /* if this IKE_SA is virgin, we check for a config */
748 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
749 {
750 ike_sa_id_t *ike_sa_id;
751 ike_cfg_t *ike_cfg;
752 job_t *job;
753 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
754 if (ike_cfg == NULL)
755 {
756 /* no config found for these hosts, destroy */
757 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
758 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
759 send_notify_response(this, msg,
760 NO_PROPOSAL_CHOSEN, chunk_empty);
761 return DESTROY_ME;
762 }
763 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
764 ike_cfg->destroy(ike_cfg);
765 /* add a timeout if peer does not establish it completely */
766 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
767 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
768 lib->scheduler->schedule_job(lib->scheduler, job,
769 lib->settings->get_int(lib->settings,
770 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
771 }
772 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
773 time_monotonic(NULL));
774 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
775 charon->bus->message(charon->bus, msg, TRUE);
776 if (process_request(this, msg) != SUCCESS)
777 {
778 flush(this);
779 return DESTROY_ME;
780 }
781
782 this->responding.mid = mid;
783 this->responding.hash = hash;
784 }
785 return SUCCESS;
786 }
787
788 METHOD(task_manager_t, queue_task, void,
789 private_task_manager_t *this, task_t *task)
790 {
791 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
792 this->queued_tasks->insert_last(this->queued_tasks, task);
793 }
794
795 METHOD(task_manager_t, adopt_tasks, void,
796 private_task_manager_t *this, task_manager_t *other_public)
797 {
798 private_task_manager_t *other = (private_task_manager_t*)other_public;
799 task_t *task;
800
801 /* move queued tasks from other to this */
802 while (other->queued_tasks->remove_last(other->queued_tasks,
803 (void**)&task) == SUCCESS)
804 {
805 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
806 task->migrate(task, this->ike_sa);
807 this->queued_tasks->insert_first(this->queued_tasks, task);
808 }
809 }
810
811 METHOD(task_manager_t, busy, bool,
812 private_task_manager_t *this)
813 {
814 return (this->active_tasks->get_count(this->active_tasks) > 0);
815 }
816
817 METHOD(task_manager_t, incr_mid, void,
818 private_task_manager_t *this, bool initiate)
819 {
820 }
821
822 METHOD(task_manager_t, reset, void,
823 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
824 {
825 }
826
827 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
828 private_task_manager_t *this, task_queue_t queue)
829 {
830 switch (queue)
831 {
832 case TASK_QUEUE_ACTIVE:
833 return this->active_tasks->create_enumerator(this->active_tasks);
834 case TASK_QUEUE_PASSIVE:
835 return this->passive_tasks->create_enumerator(this->passive_tasks);
836 case TASK_QUEUE_QUEUED:
837 return this->queued_tasks->create_enumerator(this->queued_tasks);
838 default:
839 return enumerator_create_empty();
840 }
841 }
842
843 METHOD(task_manager_t, destroy, void,
844 private_task_manager_t *this)
845 {
846 flush(this);
847
848 this->active_tasks->destroy(this->active_tasks);
849 this->queued_tasks->destroy(this->queued_tasks);
850 this->passive_tasks->destroy(this->passive_tasks);
851
852 DESTROY_IF(this->responding.packet);
853 DESTROY_IF(this->initiating.packet);
854 DESTROY_IF(this->rng);
855 free(this);
856 }
857
858 /*
859 * see header file
860 */
861 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
862 {
863 private_task_manager_t *this;
864
865 INIT(this,
866 .public = {
867 .task_manager = {
868 .process_message = _process_message,
869 .queue_task = _queue_task,
870 .initiate = _initiate,
871 .retransmit = _retransmit,
872 .incr_mid = _incr_mid,
873 .reset = _reset,
874 .adopt_tasks = _adopt_tasks,
875 .busy = _busy,
876 .create_task_enumerator = _create_task_enumerator,
877 .destroy = _destroy,
878 },
879 },
880 .ike_sa = ike_sa,
881 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
882 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
883 .queued_tasks = linked_list_create(),
884 .active_tasks = linked_list_create(),
885 .passive_tasks = linked_list_create(),
886 .retransmit_tries = lib->settings->get_int(lib->settings,
887 "charon.retransmit_tries", RETRANSMIT_TRIES),
888 .retransmit_timeout = lib->settings->get_double(lib->settings,
889 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
890 .retransmit_base = lib->settings->get_double(lib->settings,
891 "charon.retransmit_base", RETRANSMIT_BASE),
892 );
893
894 return &this->public;
895 }