Defer CHILD_SA rekeying if allocating an SPI fails
[strongswan.git] / src / libcharon / sa / ikev2 / task_manager_v2.c
1 /*
2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2010 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_v2.h"
18
19 #include <math.h>
20
21 #include <daemon.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>
42
43 #ifdef ME
44 #include <sa/ikev2/tasks/ike_me.h>
45 #endif
46
47 typedef struct exchange_t exchange_t;
48
49 /**
50 * An exchange in the air, used do detect and handle retransmission
51 */
52 struct exchange_t {
53
54 /**
55 * Message ID used for this transaction
56 */
57 u_int32_t mid;
58
59 /**
60 * generated packet for retransmission
61 */
62 packet_t *packet;
63 };
64
65 typedef struct private_task_manager_t private_task_manager_t;
66
67 /**
68 * private data of the task manager
69 */
70 struct private_task_manager_t {
71
72 /**
73 * public functions
74 */
75 task_manager_v2_t public;
76
77 /**
78 * associated IKE_SA we are serving
79 */
80 ike_sa_t *ike_sa;
81
82 /**
83 * Exchange we are currently handling as responder
84 */
85 struct {
86 /**
87 * Message ID of the exchange
88 */
89 u_int32_t mid;
90
91 /**
92 * packet for retransmission
93 */
94 packet_t *packet;
95
96 } responding;
97
98 /**
99 * Exchange we are currently handling as initiator
100 */
101 struct {
102 /**
103 * Message ID of the exchange
104 */
105 u_int32_t mid;
106
107 /**
108 * how many times we have retransmitted so far
109 */
110 u_int retransmitted;
111
112 /**
113 * packet for retransmission
114 */
115 packet_t *packet;
116
117 /**
118 * type of the initated exchange
119 */
120 exchange_type_t type;
121
122 } initiating;
123
124 /**
125 * List of queued tasks not yet in action
126 */
127 linked_list_t *queued_tasks;
128
129 /**
130 * List of active tasks, initiated by ourselve
131 */
132 linked_list_t *active_tasks;
133
134 /**
135 * List of tasks initiated by peer
136 */
137 linked_list_t *passive_tasks;
138
139 /**
140 * the task manager has been reset
141 */
142 bool reset;
143
144 /**
145 * Number of times we retransmit messages before giving up
146 */
147 u_int retransmit_tries;
148
149 /**
150 * Retransmission timeout
151 */
152 double retransmit_timeout;
153
154 /**
155 * Base to calculate retransmission timeout
156 */
157 double retransmit_base;
158 };
159
160 METHOD(task_manager_t, flush_queue, void,
161 private_task_manager_t *this, task_queue_t queue)
162 {
163 linked_list_t *list;
164 task_t *task;
165
166 switch (queue)
167 {
168 case TASK_QUEUE_ACTIVE:
169 list = this->active_tasks;
170 break;
171 case TASK_QUEUE_PASSIVE:
172 list = this->passive_tasks;
173 break;
174 case TASK_QUEUE_QUEUED:
175 list = this->queued_tasks;
176 break;
177 default:
178 return;
179 }
180 while (list->remove_last(list, (void**)&task) == SUCCESS)
181 {
182 task->destroy(task);
183 }
184 }
185
186 /**
187 * flush all tasks in the task manager
188 */
189 static void flush(private_task_manager_t *this)
190 {
191 flush_queue(this, TASK_QUEUE_QUEUED);
192 flush_queue(this, TASK_QUEUE_PASSIVE);
193 flush_queue(this, TASK_QUEUE_ACTIVE);
194 }
195
196 /**
197 * move a task of a specific type from the queue to the active list
198 */
199 static bool activate_task(private_task_manager_t *this, task_type_t type)
200 {
201 enumerator_t *enumerator;
202 task_t *task;
203 bool found = FALSE;
204
205 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
206 while (enumerator->enumerate(enumerator, (void**)&task))
207 {
208 if (task->get_type(task) == type)
209 {
210 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
211 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
212 this->active_tasks->insert_last(this->active_tasks, task);
213 found = TRUE;
214 break;
215 }
216 }
217 enumerator->destroy(enumerator);
218 return found;
219 }
220
221 METHOD(task_manager_t, retransmit, status_t,
222 private_task_manager_t *this, u_int32_t message_id)
223 {
224 if (this->initiating.packet && message_id == this->initiating.mid)
225 {
226 u_int32_t timeout;
227 job_t *job;
228 enumerator_t *enumerator;
229 packet_t *packet;
230 task_t *task;
231 ike_mobike_t *mobike = NULL;
232
233 /* check if we are retransmitting a MOBIKE routability check */
234 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
235 while (enumerator->enumerate(enumerator, (void*)&task))
236 {
237 if (task->get_type(task) == TASK_IKE_MOBIKE)
238 {
239 mobike = (ike_mobike_t*)task;
240 if (!mobike->is_probing(mobike))
241 {
242 mobike = NULL;
243 }
244 break;
245 }
246 }
247 enumerator->destroy(enumerator);
248
249 if (mobike == NULL)
250 {
251 if (this->initiating.retransmitted <= this->retransmit_tries)
252 {
253 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
254 pow(this->retransmit_base, this->initiating.retransmitted));
255 }
256 else
257 {
258 DBG1(DBG_IKE, "giving up after %d retransmits",
259 this->initiating.retransmitted - 1);
260 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND_TIMEOUT,
261 this->initiating.packet);
262 return DESTROY_ME;
263 }
264
265 if (this->initiating.retransmitted)
266 {
267 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
268 this->initiating.retransmitted, message_id);
269 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND,
270 this->initiating.packet);
271 }
272 packet = this->initiating.packet->clone(this->initiating.packet);
273 charon->sender->send(charon->sender, packet);
274 }
275 else
276 { /* for routeability checks, we use a more aggressive behavior */
277 if (this->initiating.retransmitted <= ROUTEABILITY_CHECK_TRIES)
278 {
279 timeout = ROUTEABILITY_CHECK_INTERVAL;
280 }
281 else
282 {
283 DBG1(DBG_IKE, "giving up after %d path probings",
284 this->initiating.retransmitted - 1);
285 return DESTROY_ME;
286 }
287
288 if (this->initiating.retransmitted)
289 {
290 DBG1(DBG_IKE, "path probing attempt %d",
291 this->initiating.retransmitted);
292 }
293 mobike->transmit(mobike, this->initiating.packet);
294 }
295
296 this->initiating.retransmitted++;
297 job = (job_t*)retransmit_job_create(this->initiating.mid,
298 this->ike_sa->get_id(this->ike_sa));
299 lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
300 }
301 return SUCCESS;
302 }
303
304 METHOD(task_manager_t, initiate, status_t,
305 private_task_manager_t *this)
306 {
307 enumerator_t *enumerator;
308 task_t *task;
309 message_t *message;
310 host_t *me, *other;
311 status_t status;
312 exchange_type_t exchange = 0;
313
314 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
315 {
316 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
317 exchange_type_names, this->initiating.type);
318 /* do not initiate if we already have a message in the air */
319 return SUCCESS;
320 }
321
322 if (this->active_tasks->get_count(this->active_tasks) == 0)
323 {
324 DBG2(DBG_IKE, "activating new tasks");
325 switch (this->ike_sa->get_state(this->ike_sa))
326 {
327 case IKE_CREATED:
328 activate_task(this, TASK_IKE_VENDOR);
329 if (activate_task(this, TASK_IKE_INIT))
330 {
331 this->initiating.mid = 0;
332 exchange = IKE_SA_INIT;
333 activate_task(this, TASK_IKE_NATD);
334 activate_task(this, TASK_IKE_CERT_PRE);
335 #ifdef ME
336 /* this task has to be activated before the TASK_IKE_AUTH
337 * task, because that task pregenerates the packet after
338 * which no payloads can be added to the message anymore.
339 */
340 activate_task(this, TASK_IKE_ME);
341 #endif /* ME */
342 activate_task(this, TASK_IKE_AUTH);
343 activate_task(this, TASK_IKE_CERT_POST);
344 activate_task(this, TASK_IKE_CONFIG);
345 activate_task(this, TASK_CHILD_CREATE);
346 activate_task(this, TASK_IKE_AUTH_LIFETIME);
347 activate_task(this, TASK_IKE_MOBIKE);
348 }
349 break;
350 case IKE_ESTABLISHED:
351 if (activate_task(this, TASK_CHILD_CREATE))
352 {
353 exchange = CREATE_CHILD_SA;
354 break;
355 }
356 if (activate_task(this, TASK_CHILD_DELETE))
357 {
358 exchange = INFORMATIONAL;
359 break;
360 }
361 if (activate_task(this, TASK_CHILD_REKEY))
362 {
363 exchange = CREATE_CHILD_SA;
364 break;
365 }
366 if (activate_task(this, TASK_IKE_DELETE))
367 {
368 exchange = INFORMATIONAL;
369 break;
370 }
371 if (activate_task(this, TASK_IKE_REKEY))
372 {
373 exchange = CREATE_CHILD_SA;
374 break;
375 }
376 if (activate_task(this, TASK_IKE_REAUTH))
377 {
378 exchange = INFORMATIONAL;
379 break;
380 }
381 if (activate_task(this, TASK_IKE_MOBIKE))
382 {
383 exchange = INFORMATIONAL;
384 break;
385 }
386 if (activate_task(this, TASK_IKE_DPD))
387 {
388 exchange = INFORMATIONAL;
389 break;
390 }
391 if (activate_task(this, TASK_IKE_AUTH_LIFETIME))
392 {
393 exchange = INFORMATIONAL;
394 break;
395 }
396 #ifdef ME
397 if (activate_task(this, TASK_IKE_ME))
398 {
399 exchange = ME_CONNECT;
400 break;
401 }
402 #endif /* ME */
403 case IKE_REKEYING:
404 if (activate_task(this, TASK_IKE_DELETE))
405 {
406 exchange = INFORMATIONAL;
407 break;
408 }
409 case IKE_DELETING:
410 default:
411 break;
412 }
413 }
414 else
415 {
416 DBG2(DBG_IKE, "reinitiating already active tasks");
417 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
418 while (enumerator->enumerate(enumerator, (void**)&task))
419 {
420 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
421 switch (task->get_type(task))
422 {
423 case TASK_IKE_INIT:
424 exchange = IKE_SA_INIT;
425 break;
426 case TASK_IKE_AUTH:
427 exchange = IKE_AUTH;
428 break;
429 case TASK_CHILD_CREATE:
430 case TASK_CHILD_REKEY:
431 case TASK_IKE_REKEY:
432 exchange = CREATE_CHILD_SA;
433 break;
434 case TASK_IKE_MOBIKE:
435 exchange = INFORMATIONAL;
436 break;
437 default:
438 continue;
439 }
440 break;
441 }
442 enumerator->destroy(enumerator);
443 }
444
445 if (exchange == 0)
446 {
447 DBG2(DBG_IKE, "nothing to initiate");
448 /* nothing to do yet... */
449 return SUCCESS;
450 }
451
452 me = this->ike_sa->get_my_host(this->ike_sa);
453 other = this->ike_sa->get_other_host(this->ike_sa);
454
455 message = message_create(IKEV2_MAJOR_VERSION, IKEV2_MINOR_VERSION);
456 message->set_message_id(message, this->initiating.mid);
457 message->set_source(message, me->clone(me));
458 message->set_destination(message, other->clone(other));
459 message->set_exchange_type(message, exchange);
460 this->initiating.type = exchange;
461 this->initiating.retransmitted = 0;
462
463 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
464 while (enumerator->enumerate(enumerator, (void*)&task))
465 {
466 switch (task->build(task, message))
467 {
468 case SUCCESS:
469 /* task completed, remove it */
470 this->active_tasks->remove_at(this->active_tasks, enumerator);
471 task->destroy(task);
472 break;
473 case NEED_MORE:
474 /* processed, but task needs another exchange */
475 break;
476 case FAILED:
477 default:
478 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
479 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
480 {
481 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
482 }
483 /* FALL */
484 case DESTROY_ME:
485 /* critical failure, destroy IKE_SA */
486 enumerator->destroy(enumerator);
487 message->destroy(message);
488 flush(this);
489 return DESTROY_ME;
490 }
491 }
492 enumerator->destroy(enumerator);
493
494 /* update exchange type if a task changed it */
495 this->initiating.type = message->get_exchange_type(message);
496
497 status = this->ike_sa->generate_message(this->ike_sa, message,
498 &this->initiating.packet);
499 if (status != SUCCESS)
500 {
501 /* message generation failed. There is nothing more to do than to
502 * close the SA */
503 message->destroy(message);
504 flush(this);
505 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
506 return DESTROY_ME;
507 }
508 message->destroy(message);
509
510 return retransmit(this, this->initiating.mid);
511 }
512
513 /**
514 * handle an incoming response message
515 */
516 static status_t process_response(private_task_manager_t *this,
517 message_t *message)
518 {
519 enumerator_t *enumerator;
520 task_t *task;
521
522 if (message->get_exchange_type(message) != this->initiating.type)
523 {
524 DBG1(DBG_IKE, "received %N response, but expected %N",
525 exchange_type_names, message->get_exchange_type(message),
526 exchange_type_names, this->initiating.type);
527 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
528 return DESTROY_ME;
529 }
530
531 /* catch if we get resetted while processing */
532 this->reset = FALSE;
533 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
534 while (enumerator->enumerate(enumerator, (void*)&task))
535 {
536 switch (task->process(task, message))
537 {
538 case SUCCESS:
539 /* task completed, remove it */
540 this->active_tasks->remove_at(this->active_tasks, enumerator);
541 task->destroy(task);
542 break;
543 case NEED_MORE:
544 /* processed, but task needs another exchange */
545 break;
546 case FAILED:
547 default:
548 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
549 /* FALL */
550 case DESTROY_ME:
551 /* critical failure, destroy IKE_SA */
552 this->active_tasks->remove_at(this->active_tasks, enumerator);
553 enumerator->destroy(enumerator);
554 task->destroy(task);
555 return DESTROY_ME;
556 }
557 if (this->reset)
558 { /* start all over again if we were reset */
559 this->reset = FALSE;
560 enumerator->destroy(enumerator);
561 return initiate(this);
562 }
563 }
564 enumerator->destroy(enumerator);
565
566 this->initiating.mid++;
567 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
568 this->initiating.packet->destroy(this->initiating.packet);
569 this->initiating.packet = NULL;
570
571 return initiate(this);
572 }
573
574 /**
575 * handle exchange collisions
576 */
577 static bool handle_collisions(private_task_manager_t *this, task_t *task)
578 {
579 enumerator_t *enumerator;
580 task_t *active;
581 task_type_t type;
582
583 type = task->get_type(task);
584
585 /* do we have to check */
586 if (type == TASK_IKE_REKEY || type == TASK_CHILD_REKEY ||
587 type == TASK_CHILD_DELETE || type == TASK_IKE_DELETE ||
588 type == TASK_IKE_REAUTH)
589 {
590 /* find an exchange collision, and notify these tasks */
591 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
592 while (enumerator->enumerate(enumerator, (void**)&active))
593 {
594 switch (active->get_type(active))
595 {
596 case TASK_IKE_REKEY:
597 if (type == TASK_IKE_REKEY || type == TASK_IKE_DELETE ||
598 type == TASK_IKE_REAUTH)
599 {
600 ike_rekey_t *rekey = (ike_rekey_t*)active;
601 rekey->collide(rekey, task);
602 break;
603 }
604 continue;
605 case TASK_CHILD_REKEY:
606 if (type == TASK_CHILD_REKEY || type == TASK_CHILD_DELETE)
607 {
608 child_rekey_t *rekey = (child_rekey_t*)active;
609 rekey->collide(rekey, task);
610 break;
611 }
612 continue;
613 default:
614 continue;
615 }
616 enumerator->destroy(enumerator);
617 return TRUE;
618 }
619 enumerator->destroy(enumerator);
620 }
621 return FALSE;
622 }
623
624 /**
625 * build a response depending on the "passive" task list
626 */
627 static status_t build_response(private_task_manager_t *this, message_t *request)
628 {
629 enumerator_t *enumerator;
630 task_t *task;
631 message_t *message;
632 host_t *me, *other;
633 bool delete = FALSE, hook = FALSE;
634 ike_sa_id_t *id = NULL;
635 u_int64_t responder_spi;
636 status_t status;
637
638 me = request->get_destination(request);
639 other = request->get_source(request);
640
641 message = message_create(IKEV2_MAJOR_VERSION, IKEV2_MINOR_VERSION);
642 message->set_exchange_type(message, request->get_exchange_type(request));
643 /* send response along the path the request came in */
644 message->set_source(message, me->clone(me));
645 message->set_destination(message, other->clone(other));
646 message->set_message_id(message, this->responding.mid);
647 message->set_request(message, FALSE);
648
649 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
650 while (enumerator->enumerate(enumerator, (void*)&task))
651 {
652 switch (task->build(task, message))
653 {
654 case SUCCESS:
655 /* task completed, remove it */
656 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
657 if (!handle_collisions(this, task))
658 {
659 task->destroy(task);
660 }
661 break;
662 case NEED_MORE:
663 /* processed, but task needs another exchange */
664 if (handle_collisions(this, task))
665 {
666 this->passive_tasks->remove_at(this->passive_tasks,
667 enumerator);
668 }
669 break;
670 case FAILED:
671 default:
672 hook = TRUE;
673 /* FALL */
674 case DESTROY_ME:
675 /* destroy IKE_SA, but SEND response first */
676 delete = TRUE;
677 break;
678 }
679 if (delete)
680 {
681 break;
682 }
683 }
684 enumerator->destroy(enumerator);
685
686 /* RFC 5996, section 2.6 mentions that in the event of a failure during
687 * IKE_SA_INIT the responder's SPI will be 0 in the response, while it
688 * actually explicitly allows it to be non-zero. Since we use the responder
689 * SPI to create hashes in the IKE_SA manager we can only set the SPI to
690 * zero temporarily, otherwise checking the SA in would fail. */
691 if (delete && request->get_exchange_type(request) == IKE_SA_INIT)
692 {
693 id = this->ike_sa->get_id(this->ike_sa);
694 responder_spi = id->get_responder_spi(id);
695 id->set_responder_spi(id, 0);
696 }
697
698 /* message complete, send it */
699 DESTROY_IF(this->responding.packet);
700 this->responding.packet = NULL;
701 status = this->ike_sa->generate_message(this->ike_sa, message,
702 &this->responding.packet);
703 message->destroy(message);
704 if (id)
705 {
706 id->set_responder_spi(id, responder_spi);
707 }
708 if (status != SUCCESS)
709 {
710 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
711 return DESTROY_ME;
712 }
713
714 charon->sender->send(charon->sender,
715 this->responding.packet->clone(this->responding.packet));
716 if (delete)
717 {
718 if (hook)
719 {
720 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
721 }
722 return DESTROY_ME;
723 }
724 return SUCCESS;
725 }
726
727 /**
728 * handle an incoming request message
729 */
730 static status_t process_request(private_task_manager_t *this,
731 message_t *message)
732 {
733 enumerator_t *enumerator;
734 task_t *task = NULL;
735 payload_t *payload;
736 notify_payload_t *notify;
737 delete_payload_t *delete;
738
739 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
740 { /* create tasks depending on request type, if not already some queued */
741 switch (message->get_exchange_type(message))
742 {
743 case IKE_SA_INIT:
744 {
745 task = (task_t*)ike_vendor_create(this->ike_sa, FALSE);
746 this->passive_tasks->insert_last(this->passive_tasks, task);
747 task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL);
748 this->passive_tasks->insert_last(this->passive_tasks, task);
749 task = (task_t*)ike_natd_create(this->ike_sa, FALSE);
750 this->passive_tasks->insert_last(this->passive_tasks, task);
751 task = (task_t*)ike_cert_pre_create(this->ike_sa, FALSE);
752 this->passive_tasks->insert_last(this->passive_tasks, task);
753 #ifdef ME
754 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
755 this->passive_tasks->insert_last(this->passive_tasks, task);
756 #endif /* ME */
757 task = (task_t*)ike_auth_create(this->ike_sa, FALSE);
758 this->passive_tasks->insert_last(this->passive_tasks, task);
759 task = (task_t*)ike_cert_post_create(this->ike_sa, FALSE);
760 this->passive_tasks->insert_last(this->passive_tasks, task);
761 task = (task_t*)ike_config_create(this->ike_sa, FALSE);
762 this->passive_tasks->insert_last(this->passive_tasks, task);
763 task = (task_t*)child_create_create(this->ike_sa, NULL, FALSE,
764 NULL, NULL);
765 this->passive_tasks->insert_last(this->passive_tasks, task);
766 task = (task_t*)ike_auth_lifetime_create(this->ike_sa, FALSE);
767 this->passive_tasks->insert_last(this->passive_tasks, task);
768 task = (task_t*)ike_mobike_create(this->ike_sa, FALSE);
769 this->passive_tasks->insert_last(this->passive_tasks, task);
770 break;
771 }
772 case CREATE_CHILD_SA:
773 { /* FIXME: we should prevent this on mediation connections */
774 bool notify_found = FALSE, ts_found = FALSE;
775 enumerator = message->create_payload_enumerator(message);
776 while (enumerator->enumerate(enumerator, &payload))
777 {
778 switch (payload->get_type(payload))
779 {
780 case NOTIFY:
781 { /* if we find a rekey notify, its CHILD_SA rekeying */
782 notify = (notify_payload_t*)payload;
783 if (notify->get_notify_type(notify) == REKEY_SA &&
784 (notify->get_protocol_id(notify) == PROTO_AH ||
785 notify->get_protocol_id(notify) == PROTO_ESP))
786 {
787 notify_found = TRUE;
788 }
789 break;
790 }
791 case TRAFFIC_SELECTOR_INITIATOR:
792 case TRAFFIC_SELECTOR_RESPONDER:
793 { /* if we don't find a TS, its IKE rekeying */
794 ts_found = TRUE;
795 break;
796 }
797 default:
798 break;
799 }
800 }
801 enumerator->destroy(enumerator);
802
803 if (ts_found)
804 {
805 if (notify_found)
806 {
807 task = (task_t*)child_rekey_create(this->ike_sa,
808 PROTO_NONE, 0);
809 }
810 else
811 {
812 task = (task_t*)child_create_create(this->ike_sa, NULL,
813 FALSE, NULL, NULL);
814 }
815 }
816 else
817 {
818 task = (task_t*)ike_rekey_create(this->ike_sa, FALSE);
819 }
820 this->passive_tasks->insert_last(this->passive_tasks, task);
821 break;
822 }
823 case INFORMATIONAL:
824 {
825 enumerator = message->create_payload_enumerator(message);
826 while (enumerator->enumerate(enumerator, &payload))
827 {
828 switch (payload->get_type(payload))
829 {
830 case NOTIFY:
831 {
832 notify = (notify_payload_t*)payload;
833 switch (notify->get_notify_type(notify))
834 {
835 case ADDITIONAL_IP4_ADDRESS:
836 case ADDITIONAL_IP6_ADDRESS:
837 case NO_ADDITIONAL_ADDRESSES:
838 case UPDATE_SA_ADDRESSES:
839 case NO_NATS_ALLOWED:
840 case UNACCEPTABLE_ADDRESSES:
841 case UNEXPECTED_NAT_DETECTED:
842 case COOKIE2:
843 case NAT_DETECTION_SOURCE_IP:
844 case NAT_DETECTION_DESTINATION_IP:
845 task = (task_t*)ike_mobike_create(
846 this->ike_sa, FALSE);
847 break;
848 case AUTH_LIFETIME:
849 task = (task_t*)ike_auth_lifetime_create(
850 this->ike_sa, FALSE);
851 break;
852 default:
853 break;
854 }
855 break;
856 }
857 case DELETE:
858 {
859 delete = (delete_payload_t*)payload;
860 if (delete->get_protocol_id(delete) == PROTO_IKE)
861 {
862 task = (task_t*)ike_delete_create(this->ike_sa,
863 FALSE);
864 }
865 else
866 {
867 task = (task_t*)child_delete_create(this->ike_sa,
868 PROTO_NONE, 0, FALSE);
869 }
870 break;
871 }
872 default:
873 break;
874 }
875 if (task)
876 {
877 break;
878 }
879 }
880 enumerator->destroy(enumerator);
881
882 if (task == NULL)
883 {
884 task = (task_t*)ike_dpd_create(FALSE);
885 }
886 this->passive_tasks->insert_last(this->passive_tasks, task);
887 break;
888 }
889 #ifdef ME
890 case ME_CONNECT:
891 {
892 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
893 this->passive_tasks->insert_last(this->passive_tasks, task);
894 }
895 #endif /* ME */
896 default:
897 break;
898 }
899 }
900
901 /* let the tasks process the message */
902 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
903 while (enumerator->enumerate(enumerator, (void*)&task))
904 {
905 switch (task->process(task, message))
906 {
907 case SUCCESS:
908 /* task completed, remove it */
909 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
910 task->destroy(task);
911 break;
912 case NEED_MORE:
913 /* processed, but task needs at least another call to build() */
914 break;
915 case FAILED:
916 default:
917 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
918 /* FALL */
919 case DESTROY_ME:
920 /* critical failure, destroy IKE_SA */
921 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
922 enumerator->destroy(enumerator);
923 task->destroy(task);
924 return DESTROY_ME;
925 }
926 }
927 enumerator->destroy(enumerator);
928
929 return build_response(this, message);
930 }
931
932 METHOD(task_manager_t, incr_mid, void,
933 private_task_manager_t *this, bool initiate)
934 {
935 if (initiate)
936 {
937 this->initiating.mid++;
938 }
939 else
940 {
941 this->responding.mid++;
942 }
943 }
944
945 /**
946 * Send a notify back to the sender
947 */
948 static void send_notify_response(private_task_manager_t *this,
949 message_t *request, notify_type_t type,
950 chunk_t data)
951 {
952 message_t *response;
953 packet_t *packet;
954 host_t *me, *other;
955
956 response = message_create(IKEV2_MAJOR_VERSION, IKEV2_MINOR_VERSION);
957 response->set_exchange_type(response, request->get_exchange_type(request));
958 response->set_request(response, FALSE);
959 response->set_message_id(response, request->get_message_id(request));
960 response->add_notify(response, FALSE, type, data);
961 me = this->ike_sa->get_my_host(this->ike_sa);
962 if (me->is_anyaddr(me))
963 {
964 me = request->get_destination(request);
965 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
966 }
967 other = this->ike_sa->get_other_host(this->ike_sa);
968 if (other->is_anyaddr(other))
969 {
970 other = request->get_source(request);
971 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
972 }
973 response->set_source(response, me->clone(me));
974 response->set_destination(response, other->clone(other));
975 if (this->ike_sa->generate_message(this->ike_sa, response,
976 &packet) == SUCCESS)
977 {
978 charon->sender->send(charon->sender, packet);
979 }
980 response->destroy(response);
981 }
982
983 /**
984 * Parse the given message and verify that it is valid.
985 */
986 static status_t parse_message(private_task_manager_t *this, message_t *msg)
987 {
988 status_t status;
989 u_int8_t type = 0;
990
991 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
992
993 if (status == SUCCESS)
994 { /* check for unsupported critical payloads */
995 enumerator_t *enumerator;
996 unknown_payload_t *unknown;
997 payload_t *payload;
998
999 enumerator = msg->create_payload_enumerator(msg);
1000 while (enumerator->enumerate(enumerator, &payload))
1001 {
1002 unknown = (unknown_payload_t*)payload;
1003 type = payload->get_type(payload);
1004 if (!payload_is_known(type) &&
1005 unknown->is_critical(unknown))
1006 {
1007 DBG1(DBG_ENC, "payload type %N is not supported, "
1008 "but its critical!", payload_type_names, type);
1009 status = NOT_SUPPORTED;
1010 break;
1011 }
1012 }
1013 enumerator->destroy(enumerator);
1014 }
1015
1016 if (status != SUCCESS)
1017 {
1018 bool is_request = msg->get_request(msg);
1019
1020 switch (status)
1021 {
1022 case NOT_SUPPORTED:
1023 DBG1(DBG_IKE, "critical unknown payloads found");
1024 if (is_request)
1025 {
1026 send_notify_response(this, msg,
1027 UNSUPPORTED_CRITICAL_PAYLOAD,
1028 chunk_from_thing(type));
1029 incr_mid(this, FALSE);
1030 }
1031 break;
1032 case PARSE_ERROR:
1033 DBG1(DBG_IKE, "message parsing failed");
1034 if (is_request)
1035 {
1036 send_notify_response(this, msg,
1037 INVALID_SYNTAX, chunk_empty);
1038 incr_mid(this, FALSE);
1039 }
1040 break;
1041 case VERIFY_ERROR:
1042 DBG1(DBG_IKE, "message verification failed");
1043 if (is_request)
1044 {
1045 send_notify_response(this, msg,
1046 INVALID_SYNTAX, chunk_empty);
1047 incr_mid(this, FALSE);
1048 }
1049 break;
1050 case FAILED:
1051 DBG1(DBG_IKE, "integrity check failed");
1052 /* ignored */
1053 break;
1054 case INVALID_STATE:
1055 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1056 default:
1057 break;
1058 }
1059 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
1060 exchange_type_names, msg->get_exchange_type(msg),
1061 is_request ? "request" : "response",
1062 msg->get_message_id(msg));
1063
1064 charon->bus->alert(charon->bus, ALERT_PARSE_ERROR_BODY, msg, status);
1065
1066 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
1067 { /* invalid initiation attempt, close SA */
1068 return DESTROY_ME;
1069 }
1070 }
1071 return status;
1072 }
1073
1074
1075 METHOD(task_manager_t, process_message, status_t,
1076 private_task_manager_t *this, message_t *msg)
1077 {
1078 host_t *me, *other;
1079 status_t status;
1080 u_int32_t mid;
1081
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
1089 me = msg->get_destination(msg);
1090 other = msg->get_source(msg);
1091
1092 /* if this IKE_SA is virgin, we check for a config */
1093 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
1094 {
1095 ike_sa_id_t *ike_sa_id;
1096 ike_cfg_t *ike_cfg;
1097 job_t *job;
1098 ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1099 me, other, IKEV2);
1100 if (ike_cfg == NULL)
1101 {
1102 /* no config found for these hosts, destroy */
1103 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1104 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1105 send_notify_response(this, msg,
1106 NO_PROPOSAL_CHOSEN, chunk_empty);
1107 return DESTROY_ME;
1108 }
1109 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
1110 ike_cfg->destroy(ike_cfg);
1111 /* add a timeout if peer does not establish it completely */
1112 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
1113 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
1114 lib->scheduler->schedule_job(lib->scheduler, job,
1115 lib->settings->get_int(lib->settings,
1116 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT,
1117 charon->name));
1118 }
1119 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
1120 time_monotonic(NULL));
1121
1122 mid = msg->get_message_id(msg);
1123 if (msg->get_request(msg))
1124 {
1125 if (mid == this->responding.mid)
1126 {
1127 /* reject initial messages once established */
1128 if (msg->get_exchange_type(msg) == IKE_SA_INIT ||
1129 msg->get_exchange_type(msg) == IKE_AUTH)
1130 {
1131 if (this->ike_sa->get_state(this->ike_sa) != IKE_CREATED &&
1132 this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
1133 {
1134 DBG1(DBG_IKE, "ignoring %N in established IKE_SA state",
1135 exchange_type_names, msg->get_exchange_type(msg));
1136 return FAILED;
1137 }
1138 }
1139 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED ||
1140 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING ||
1141 msg->get_exchange_type(msg) != IKE_SA_INIT)
1142 { /* only do host updates based on verified messages */
1143 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
1144 { /* with MOBIKE, we do no implicit updates */
1145 this->ike_sa->update_hosts(this->ike_sa, me, other, mid == 1);
1146 }
1147 }
1148 charon->bus->message(charon->bus, msg, TRUE, TRUE);
1149 if (msg->get_exchange_type(msg) == EXCHANGE_TYPE_UNDEFINED)
1150 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1151 return SUCCESS;
1152 }
1153 if (process_request(this, msg) != SUCCESS)
1154 {
1155 flush(this);
1156 return DESTROY_ME;
1157 }
1158 this->responding.mid++;
1159 }
1160 else if ((mid == this->responding.mid - 1) && this->responding.packet)
1161 {
1162 packet_t *clone;
1163 host_t *host;
1164
1165 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
1166 "retransmitting response", mid);
1167 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_RECEIVE, msg);
1168 clone = this->responding.packet->clone(this->responding.packet);
1169 host = msg->get_destination(msg);
1170 clone->set_source(clone, host->clone(host));
1171 host = msg->get_source(msg);
1172 clone->set_destination(clone, host->clone(host));
1173 charon->sender->send(charon->sender, clone);
1174 }
1175 else
1176 {
1177 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
1178 mid, this->responding.mid);
1179 if (msg->get_exchange_type(msg) == IKE_SA_INIT)
1180 { /* clean up IKE_SA state if IKE_SA_INIT has invalid msg ID */
1181 return DESTROY_ME;
1182 }
1183 }
1184 }
1185 else
1186 {
1187 if (mid == this->initiating.mid)
1188 {
1189 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED ||
1190 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING ||
1191 msg->get_exchange_type(msg) != IKE_SA_INIT)
1192 { /* only do host updates based on verified messages */
1193 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
1194 { /* with MOBIKE, we do no implicit updates */
1195 this->ike_sa->update_hosts(this->ike_sa, me, other, FALSE);
1196 }
1197 }
1198 charon->bus->message(charon->bus, msg, TRUE, TRUE);
1199 if (msg->get_exchange_type(msg) == EXCHANGE_TYPE_UNDEFINED)
1200 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
1201 return SUCCESS;
1202 }
1203 if (process_response(this, msg) != SUCCESS)
1204 {
1205 flush(this);
1206 return DESTROY_ME;
1207 }
1208 }
1209 else
1210 {
1211 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
1212 mid, this->initiating.mid);
1213 return SUCCESS;
1214 }
1215 }
1216 return SUCCESS;
1217 }
1218
1219 METHOD(task_manager_t, queue_task, void,
1220 private_task_manager_t *this, task_t *task)
1221 {
1222 if (task->get_type(task) == TASK_IKE_MOBIKE)
1223 { /* there is no need to queue more than one mobike task */
1224 enumerator_t *enumerator;
1225 task_t *current;
1226
1227 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1228 while (enumerator->enumerate(enumerator, (void**)&current))
1229 {
1230 if (current->get_type(current) == TASK_IKE_MOBIKE)
1231 {
1232 enumerator->destroy(enumerator);
1233 task->destroy(task);
1234 return;
1235 }
1236 }
1237 enumerator->destroy(enumerator);
1238 }
1239 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
1240 this->queued_tasks->insert_last(this->queued_tasks, task);
1241 }
1242
1243 /**
1244 * Check if a given task has been queued already
1245 */
1246 static bool has_queued(private_task_manager_t *this, task_type_t type)
1247 {
1248 enumerator_t *enumerator;
1249 bool found = FALSE;
1250 task_t *task;
1251
1252 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1253 while (enumerator->enumerate(enumerator, &task))
1254 {
1255 if (task->get_type(task) == type)
1256 {
1257 found = TRUE;
1258 break;
1259 }
1260 }
1261 enumerator->destroy(enumerator);
1262 return found;
1263 }
1264
1265 METHOD(task_manager_t, queue_ike, void,
1266 private_task_manager_t *this)
1267 {
1268 if (!has_queued(this, TASK_IKE_VENDOR))
1269 {
1270 queue_task(this, (task_t*)ike_vendor_create(this->ike_sa, TRUE));
1271 }
1272 if (!has_queued(this, TASK_IKE_INIT))
1273 {
1274 queue_task(this, (task_t*)ike_init_create(this->ike_sa, TRUE, NULL));
1275 }
1276 if (!has_queued(this, TASK_IKE_NATD))
1277 {
1278 queue_task(this, (task_t*)ike_natd_create(this->ike_sa, TRUE));
1279 }
1280 if (!has_queued(this, TASK_IKE_CERT_PRE))
1281 {
1282 queue_task(this, (task_t*)ike_cert_pre_create(this->ike_sa, TRUE));
1283 }
1284 if (!has_queued(this, TASK_IKE_AUTH))
1285 {
1286 queue_task(this, (task_t*)ike_auth_create(this->ike_sa, TRUE));
1287 }
1288 if (!has_queued(this, TASK_IKE_CERT_POST))
1289 {
1290 queue_task(this, (task_t*)ike_cert_post_create(this->ike_sa, TRUE));
1291 }
1292 if (!has_queued(this, TASK_IKE_CONFIG))
1293 {
1294 queue_task(this, (task_t*)ike_config_create(this->ike_sa, TRUE));
1295 }
1296 if (!has_queued(this, TASK_IKE_AUTH_LIFETIME))
1297 {
1298 queue_task(this, (task_t*)ike_auth_lifetime_create(this->ike_sa, TRUE));
1299 }
1300 if (!has_queued(this, TASK_IKE_MOBIKE))
1301 {
1302 peer_cfg_t *peer_cfg;
1303
1304 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1305 if (peer_cfg->use_mobike(peer_cfg))
1306 {
1307 queue_task(this, (task_t*)ike_mobike_create(this->ike_sa, TRUE));
1308 }
1309 }
1310 #ifdef ME
1311 if (!has_queued(this, TASK_IKE_ME))
1312 {
1313 queue_task(this, (task_t*)ike_me_create(this->ike_sa, TRUE));
1314 }
1315 #endif /* ME */
1316 }
1317
1318 METHOD(task_manager_t, queue_ike_rekey, void,
1319 private_task_manager_t *this)
1320 {
1321 queue_task(this, (task_t*)ike_rekey_create(this->ike_sa, TRUE));
1322 }
1323
1324 METHOD(task_manager_t, queue_ike_reauth, void,
1325 private_task_manager_t *this)
1326 {
1327 queue_task(this, (task_t*)ike_reauth_create(this->ike_sa));
1328 }
1329
1330 METHOD(task_manager_t, queue_ike_delete, void,
1331 private_task_manager_t *this)
1332 {
1333 queue_task(this, (task_t*)ike_delete_create(this->ike_sa, TRUE));
1334 }
1335
1336 METHOD(task_manager_t, queue_mobike, void,
1337 private_task_manager_t *this, bool roam, bool address)
1338 {
1339 ike_mobike_t *mobike;
1340
1341 mobike = ike_mobike_create(this->ike_sa, TRUE);
1342 if (roam)
1343 {
1344 mobike->roam(mobike, address);
1345 }
1346 else
1347 {
1348 mobike->addresses(mobike);
1349 }
1350 queue_task(this, &mobike->task);
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 child_create_t *task;
1358
1359 task = child_create_create(this->ike_sa, cfg, FALSE, tsi, tsr);
1360 if (reqid)
1361 {
1362 task->use_reqid(task, reqid);
1363 }
1364 queue_task(this, &task->task);
1365 }
1366
1367 METHOD(task_manager_t, queue_child_rekey, void,
1368 private_task_manager_t *this, protocol_id_t protocol, u_int32_t spi)
1369 {
1370 queue_task(this, (task_t*)child_rekey_create(this->ike_sa, protocol, spi));
1371 }
1372
1373 METHOD(task_manager_t, queue_child_delete, void,
1374 private_task_manager_t *this, protocol_id_t protocol, u_int32_t spi,
1375 bool expired)
1376 {
1377 queue_task(this, (task_t*)child_delete_create(this->ike_sa,
1378 protocol, spi, expired));
1379 }
1380
1381 METHOD(task_manager_t, queue_dpd, void,
1382 private_task_manager_t *this)
1383 {
1384 ike_mobike_t *mobike;
1385
1386 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE) &&
1387 this->ike_sa->has_condition(this->ike_sa, COND_NAT_HERE))
1388 {
1389 /* use mobike enabled DPD to detect NAT mapping changes */
1390 mobike = ike_mobike_create(this->ike_sa, TRUE);
1391 mobike->dpd(mobike);
1392 queue_task(this, &mobike->task);
1393 }
1394 else
1395 {
1396 queue_task(this, (task_t*)ike_dpd_create(TRUE));
1397 }
1398 }
1399
1400 METHOD(task_manager_t, adopt_tasks, void,
1401 private_task_manager_t *this, task_manager_t *other_public)
1402 {
1403 private_task_manager_t *other = (private_task_manager_t*)other_public;
1404 task_t *task;
1405
1406 /* move queued tasks from other to this */
1407 while (other->queued_tasks->remove_last(other->queued_tasks,
1408 (void**)&task) == SUCCESS)
1409 {
1410 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
1411 task->migrate(task, this->ike_sa);
1412 this->queued_tasks->insert_first(this->queued_tasks, task);
1413 }
1414 }
1415
1416 METHOD(task_manager_t, busy, bool,
1417 private_task_manager_t *this)
1418 {
1419 return (this->active_tasks->get_count(this->active_tasks) > 0);
1420 }
1421
1422 METHOD(task_manager_t, reset, void,
1423 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
1424 {
1425 enumerator_t *enumerator;
1426 task_t *task;
1427
1428 /* reset message counters and retransmit packets */
1429 DESTROY_IF(this->responding.packet);
1430 DESTROY_IF(this->initiating.packet);
1431 this->responding.packet = NULL;
1432 this->initiating.packet = NULL;
1433 if (initiate != UINT_MAX)
1434 {
1435 this->initiating.mid = initiate;
1436 }
1437 if (respond != UINT_MAX)
1438 {
1439 this->responding.mid = respond;
1440 }
1441 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1442
1443 /* reset queued tasks */
1444 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1445 while (enumerator->enumerate(enumerator, &task))
1446 {
1447 task->migrate(task, this->ike_sa);
1448 }
1449 enumerator->destroy(enumerator);
1450
1451 /* reset active tasks */
1452 while (this->active_tasks->remove_last(this->active_tasks,
1453 (void**)&task) == SUCCESS)
1454 {
1455 task->migrate(task, this->ike_sa);
1456 this->queued_tasks->insert_first(this->queued_tasks, task);
1457 }
1458
1459 this->reset = TRUE;
1460 }
1461
1462 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
1463 private_task_manager_t *this, task_queue_t queue)
1464 {
1465 switch (queue)
1466 {
1467 case TASK_QUEUE_ACTIVE:
1468 return this->active_tasks->create_enumerator(this->active_tasks);
1469 case TASK_QUEUE_PASSIVE:
1470 return this->passive_tasks->create_enumerator(this->passive_tasks);
1471 case TASK_QUEUE_QUEUED:
1472 return this->queued_tasks->create_enumerator(this->queued_tasks);
1473 default:
1474 return enumerator_create_empty();
1475 }
1476 }
1477
1478 METHOD(task_manager_t, destroy, void,
1479 private_task_manager_t *this)
1480 {
1481 flush(this);
1482
1483 this->active_tasks->destroy(this->active_tasks);
1484 this->queued_tasks->destroy(this->queued_tasks);
1485 this->passive_tasks->destroy(this->passive_tasks);
1486
1487 DESTROY_IF(this->responding.packet);
1488 DESTROY_IF(this->initiating.packet);
1489 free(this);
1490 }
1491
1492 /*
1493 * see header file
1494 */
1495 task_manager_v2_t *task_manager_v2_create(ike_sa_t *ike_sa)
1496 {
1497 private_task_manager_t *this;
1498
1499 INIT(this,
1500 .public = {
1501 .task_manager = {
1502 .process_message = _process_message,
1503 .queue_task = _queue_task,
1504 .queue_ike = _queue_ike,
1505 .queue_ike_rekey = _queue_ike_rekey,
1506 .queue_ike_reauth = _queue_ike_reauth,
1507 .queue_ike_delete = _queue_ike_delete,
1508 .queue_mobike = _queue_mobike,
1509 .queue_child = _queue_child,
1510 .queue_child_rekey = _queue_child_rekey,
1511 .queue_child_delete = _queue_child_delete,
1512 .queue_dpd = _queue_dpd,
1513 .initiate = _initiate,
1514 .retransmit = _retransmit,
1515 .incr_mid = _incr_mid,
1516 .reset = _reset,
1517 .adopt_tasks = _adopt_tasks,
1518 .busy = _busy,
1519 .create_task_enumerator = _create_task_enumerator,
1520 .flush_queue = _flush_queue,
1521 .destroy = _destroy,
1522 },
1523 },
1524 .ike_sa = ike_sa,
1525 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
1526 .queued_tasks = linked_list_create(),
1527 .active_tasks = linked_list_create(),
1528 .passive_tasks = linked_list_create(),
1529 .retransmit_tries = lib->settings->get_int(lib->settings,
1530 "%s.retransmit_tries", RETRANSMIT_TRIES, charon->name),
1531 .retransmit_timeout = lib->settings->get_double(lib->settings,
1532 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT, charon->name),
1533 .retransmit_base = lib->settings->get_double(lib->settings,
1534 "%s.retransmit_base", RETRANSMIT_BASE, charon->name),
1535 );
1536
1537 return &this->public;
1538 }