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