f959cd7092966fcbc17fcc2de7edd6b0ea527242
[strongswan.git] / src / charon / sa / task_manager.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2007 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 * $Id$
17 */
18
19 #include "task_manager.h"
20
21 #include <math.h>
22
23 #include <daemon.h>
24 #include <sa/tasks/ike_init.h>
25 #include <sa/tasks/ike_natd.h>
26 #include <sa/tasks/ike_mobike.h>
27 #include <sa/tasks/ike_auth.h>
28 #include <sa/tasks/ike_auth_lifetime.h>
29 #include <sa/tasks/ike_cert_pre.h>
30 #include <sa/tasks/ike_cert_post.h>
31 #include <sa/tasks/ike_rekey.h>
32 #include <sa/tasks/ike_delete.h>
33 #include <sa/tasks/ike_config.h>
34 #include <sa/tasks/ike_dpd.h>
35 #include <sa/tasks/child_create.h>
36 #include <sa/tasks/child_rekey.h>
37 #include <sa/tasks/child_delete.h>
38 #include <encoding/payloads/delete_payload.h>
39 #include <processing/jobs/retransmit_job.h>
40
41 #ifdef ME
42 #include <sa/tasks/ike_me.h>
43 #endif
44
45 typedef struct exchange_t exchange_t;
46
47 /**
48 * An exchange in the air, used do detect and handle retransmission
49 */
50 struct exchange_t {
51
52 /**
53 * Message ID used for this transaction
54 */
55 u_int32_t mid;
56
57 /**
58 * generated packet for retransmission
59 */
60 packet_t *packet;
61 };
62
63 typedef struct private_task_manager_t private_task_manager_t;
64
65 /**
66 * private data of the task manager
67 */
68 struct private_task_manager_t {
69
70 /**
71 * public functions
72 */
73 task_manager_t public;
74
75 /**
76 * associated IKE_SA we are serving
77 */
78 ike_sa_t *ike_sa;
79
80 /**
81 * Exchange we are currently handling as responder
82 */
83 struct {
84 /**
85 * Message ID of the exchange
86 */
87 u_int32_t mid;
88
89 /**
90 * packet for retransmission
91 */
92 packet_t *packet;
93
94 } responding;
95
96 /**
97 * Exchange we are currently handling as initiator
98 */
99 struct {
100 /**
101 * Message ID of the exchange
102 */
103 u_int32_t mid;
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 * the task manager has been reset
139 */
140 bool reset;
141 };
142
143 /**
144 * flush all tasks in the task manager
145 */
146 static void flush(private_task_manager_t *this)
147 {
148 this->queued_tasks->destroy_offset(this->queued_tasks,
149 offsetof(task_t, destroy));
150 this->passive_tasks->destroy_offset(this->passive_tasks,
151 offsetof(task_t, destroy));
152 this->active_tasks->destroy_offset(this->active_tasks,
153 offsetof(task_t, destroy));
154 this->queued_tasks = linked_list_create();
155 this->passive_tasks = linked_list_create();
156 this->active_tasks = linked_list_create();
157 }
158
159 /**
160 * move a task of a specific type from the queue to the active list
161 */
162 static bool activate_task(private_task_manager_t *this, task_type_t type)
163 {
164 iterator_t *iterator;
165 task_t *task;
166 bool found = FALSE;
167
168 iterator = this->queued_tasks->create_iterator(this->queued_tasks, TRUE);
169 while (iterator->iterate(iterator, (void**)&task))
170 {
171 if (task->get_type(task) == type)
172 {
173 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
174 iterator->remove(iterator);
175 this->active_tasks->insert_last(this->active_tasks, task);
176 found = TRUE;
177 break;
178 }
179 }
180 iterator->destroy(iterator);
181 return found;
182 }
183
184 /**
185 * Implementation of task_manager_t.retransmit
186 */
187 static status_t retransmit(private_task_manager_t *this, u_int32_t message_id)
188 {
189 if (message_id == this->initiating.mid)
190 {
191 u_int32_t timeout;
192 job_t *job;
193 iterator_t *iterator;
194 packet_t *packet;
195 task_t *task;
196 ike_mobike_t *mobike = NULL;
197
198 /* check if we are retransmitting a MOBIKE routability check */
199 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
200 while (iterator->iterate(iterator, (void*)&task))
201 {
202 if (task->get_type(task) == IKE_MOBIKE)
203 {
204 mobike = (ike_mobike_t*)task;
205 if (!mobike->is_probing(mobike))
206 {
207 mobike = NULL;
208 }
209 break;
210 }
211 }
212 iterator->destroy(iterator);
213
214 if (mobike == NULL)
215 {
216 if (this->initiating.retransmitted <= RETRANSMIT_TRIES)
217 {
218 timeout = (u_int32_t)(RETRANSMIT_TIMEOUT *
219 pow(RETRANSMIT_BASE, this->initiating.retransmitted));
220 }
221 else
222 {
223 DBG1(DBG_IKE, "giving up after %d retransmits",
224 this->initiating.retransmitted - 1);
225 return DESTROY_ME;
226 }
227
228 if (this->initiating.retransmitted)
229 {
230 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
231 this->initiating.retransmitted, message_id);
232 }
233 packet = this->initiating.packet->clone(this->initiating.packet);
234 }
235 else
236 { /* for routeability checks, we use a more aggressive behavior */
237 if (this->initiating.retransmitted <= ROUTEABILITY_CHECK_TRIES)
238 {
239 timeout = ROUTEABILITY_CHECK_INTERVAL;
240 }
241 else
242 {
243 DBG1(DBG_IKE, "giving up after %d path probings",
244 this->initiating.retransmitted - 1);
245 return DESTROY_ME;
246 }
247
248 if (this->initiating.retransmitted)
249 {
250 DBG1(DBG_IKE, "path probing attempt %d",
251 this->initiating.retransmitted);
252 }
253 packet = this->initiating.packet->clone(this->initiating.packet);
254 mobike->transmit(mobike, packet);
255 }
256
257 charon->sender->send(charon->sender, packet);
258
259 this->initiating.retransmitted++;
260 job = (job_t*)retransmit_job_create(this->initiating.mid,
261 this->ike_sa->get_id(this->ike_sa));
262 charon->scheduler->schedule_job(charon->scheduler, job, timeout);
263 }
264 return SUCCESS;
265 }
266
267 /**
268 * build a request using the active task list
269 * Implementation of task_manager_t.initiate
270 */
271 static status_t build_request(private_task_manager_t *this)
272 {
273 iterator_t *iterator;
274 task_t *task;
275 message_t *message;
276 host_t *me, *other;
277 status_t status;
278 exchange_type_t exchange = 0;
279
280 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
281 {
282 DBG2(DBG_IKE, "delaying task initiation, exchange in progress");
283 /* do not initiate if we already have a message in the air */
284 return SUCCESS;
285 }
286
287 if (this->active_tasks->get_count(this->active_tasks) == 0)
288 {
289 DBG2(DBG_IKE, "activating new tasks");
290 switch (this->ike_sa->get_state(this->ike_sa))
291 {
292 case IKE_CREATED:
293 if (activate_task(this, IKE_INIT))
294 {
295 this->initiating.mid = 0;
296 exchange = IKE_SA_INIT;
297 activate_task(this, IKE_NATD);
298 activate_task(this, IKE_CERT_PRE);
299 #ifdef ME
300 /* this task has to be activated before the IKE_AUTHENTICATE
301 * task, because that task pregenerates the packet after
302 * which no payloads can be added to the message anymore.
303 */
304 activate_task(this, IKE_ME);
305 #endif /* ME */
306 activate_task(this, IKE_AUTHENTICATE);
307 activate_task(this, IKE_CERT_POST);
308 activate_task(this, IKE_CONFIG);
309 activate_task(this, CHILD_CREATE);
310 activate_task(this, IKE_AUTH_LIFETIME);
311 activate_task(this, IKE_MOBIKE);
312 }
313 break;
314 case IKE_ESTABLISHED:
315 if (activate_task(this, CHILD_CREATE))
316 {
317 exchange = CREATE_CHILD_SA;
318 break;
319 }
320 if (activate_task(this, CHILD_DELETE))
321 {
322 exchange = INFORMATIONAL;
323 break;
324 }
325 if (activate_task(this, CHILD_REKEY))
326 {
327 exchange = CREATE_CHILD_SA;
328 break;
329 }
330 if (activate_task(this, IKE_DELETE))
331 {
332 exchange = INFORMATIONAL;
333 break;
334 }
335 if (activate_task(this, IKE_REKEY))
336 {
337 exchange = CREATE_CHILD_SA;
338 break;
339 }
340 if (activate_task(this, IKE_REAUTH))
341 {
342 exchange = INFORMATIONAL;
343 break;
344 }
345 if (activate_task(this, IKE_MOBIKE))
346 {
347 exchange = INFORMATIONAL;
348 break;
349 }
350 if (activate_task(this, IKE_DPD))
351 {
352 exchange = INFORMATIONAL;
353 break;
354 }
355 #ifdef ME
356 if (activate_task(this, IKE_ME))
357 {
358 exchange = ME_CONNECT;
359 break;
360 }
361 #endif /* ME */
362 case IKE_REKEYING:
363 if (activate_task(this, IKE_DELETE))
364 {
365 exchange = INFORMATIONAL;
366 break;
367 }
368 case IKE_DELETING:
369 default:
370 break;
371 }
372 }
373 else
374 {
375 DBG2(DBG_IKE, "reinitiating already active tasks");
376 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
377 while (iterator->iterate(iterator, (void**)&task))
378 {
379 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
380 switch (task->get_type(task))
381 {
382 case IKE_INIT:
383 exchange = IKE_SA_INIT;
384 break;
385 case IKE_AUTHENTICATE:
386 exchange = IKE_AUTH;
387 break;
388 case CHILD_CREATE:
389 case CHILD_REKEY:
390 case IKE_REKEY:
391 exchange = CREATE_CHILD_SA;
392 break;
393 case IKE_MOBIKE:
394 exchange = INFORMATIONAL;
395 default:
396 continue;
397 }
398 break;
399 }
400 iterator->destroy(iterator);
401 }
402
403 if (exchange == 0)
404 {
405 DBG2(DBG_IKE, "nothing to initiate");
406 /* nothing to do yet... */
407 return SUCCESS;
408 }
409
410 me = this->ike_sa->get_my_host(this->ike_sa);
411 other = this->ike_sa->get_other_host(this->ike_sa);
412
413 message = message_create();
414 message->set_message_id(message, this->initiating.mid);
415 message->set_source(message, me->clone(me));
416 message->set_destination(message, other->clone(other));
417 message->set_exchange_type(message, exchange);
418 this->initiating.type = exchange;
419 this->initiating.retransmitted = 0;
420
421 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
422 while (iterator->iterate(iterator, (void*)&task))
423 {
424 switch (task->build(task, message))
425 {
426 case SUCCESS:
427 /* task completed, remove it */
428 iterator->remove(iterator);
429 task->destroy(task);
430 break;
431 case NEED_MORE:
432 /* processed, but task needs another exchange */
433 break;
434 case FAILED:
435 default:
436 /* critical failure, destroy IKE_SA */
437 iterator->destroy(iterator);
438 message->destroy(message);
439 flush(this);
440 return DESTROY_ME;
441 }
442 }
443 iterator->destroy(iterator);
444
445 /* update exchange type if a task changed it */
446 this->initiating.type = message->get_exchange_type(message);
447
448 status = this->ike_sa->generate_message(this->ike_sa, message,
449 &this->initiating.packet);
450 if (status != SUCCESS)
451 {
452 /* message generation failed. There is nothing more to do than to
453 * close the SA */
454 message->destroy(message);
455 flush(this);
456 return DESTROY_ME;
457 }
458
459 charon->bus->message(charon->bus, message, FALSE);
460 message->destroy(message);
461
462 return retransmit(this, this->initiating.mid);
463 }
464
465 /**
466 * handle an incoming response message
467 */
468 static status_t process_response(private_task_manager_t *this,
469 message_t *message)
470 {
471 iterator_t *iterator;
472 task_t *task;
473
474 if (message->get_exchange_type(message) != this->initiating.type)
475 {
476 DBG1(DBG_IKE, "received %N response, but expected %N",
477 exchange_type_names, message->get_exchange_type(message),
478 exchange_type_names, this->initiating.type);
479 return DESTROY_ME;
480 }
481
482 /* catch if we get resetted while processing */
483 this->reset = FALSE;
484 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
485 while (iterator->iterate(iterator, (void*)&task))
486 {
487 switch (task->process(task, message))
488 {
489 case SUCCESS:
490 /* task completed, remove it */
491 iterator->remove(iterator);
492 task->destroy(task);
493 break;
494 case NEED_MORE:
495 /* processed, but task needs another exchange */
496 break;
497 case FAILED:
498 default:
499 /* critical failure, destroy IKE_SA */
500 iterator->remove(iterator);
501 iterator->destroy(iterator);
502 task->destroy(task);
503 return DESTROY_ME;
504 }
505 if (this->reset)
506 { /* start all over again if we were reset */
507 this->reset = FALSE;
508 iterator->destroy(iterator);
509 return build_request(this);
510 }
511 }
512 iterator->destroy(iterator);
513
514 this->initiating.mid++;
515 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
516 this->initiating.packet->destroy(this->initiating.packet);
517 this->initiating.packet = NULL;
518
519 return build_request(this);
520 }
521
522 /**
523 * handle exchange collisions
524 */
525 static void handle_collisions(private_task_manager_t *this, task_t *task)
526 {
527 iterator_t *iterator;
528 task_t *active;
529 task_type_t type;
530
531 type = task->get_type(task);
532
533 /* do we have to check */
534 if (type == IKE_REKEY || type == CHILD_REKEY ||
535 type == CHILD_DELETE || type == IKE_DELETE || type == IKE_REAUTH)
536 {
537 /* find an exchange collision, and notify these tasks */
538 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
539 while (iterator->iterate(iterator, (void**)&active))
540 {
541 switch (active->get_type(active))
542 {
543 case IKE_REKEY:
544 if (type == IKE_REKEY || type == IKE_DELETE ||
545 type == IKE_REAUTH)
546 {
547 ike_rekey_t *rekey = (ike_rekey_t*)active;
548 rekey->collide(rekey, task);
549 break;
550 }
551 continue;
552 case CHILD_REKEY:
553 if (type == CHILD_REKEY || type == CHILD_DELETE)
554 {
555 child_rekey_t *rekey = (child_rekey_t*)active;
556 rekey->collide(rekey, task);
557 break;
558 }
559 continue;
560 default:
561 continue;
562 }
563 iterator->destroy(iterator);
564 return;
565 }
566 iterator->destroy(iterator);
567 }
568 /* destroy task if not registered in any active task */
569 task->destroy(task);
570 }
571
572 /**
573 * build a response depending on the "passive" task list
574 */
575 static status_t build_response(private_task_manager_t *this, message_t *request)
576 {
577 iterator_t *iterator;
578 task_t *task;
579 message_t *message;
580 host_t *me, *other;
581 bool delete = FALSE;
582 status_t status;
583
584 me = request->get_destination(request);
585 other = request->get_source(request);
586
587 message = message_create();
588 message->set_exchange_type(message, request->get_exchange_type(request));
589 /* send response along the path the request came in */
590 message->set_source(message, me->clone(me));
591 message->set_destination(message, other->clone(other));
592 message->set_message_id(message, this->responding.mid);
593 message->set_request(message, FALSE);
594
595 iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE);
596 while (iterator->iterate(iterator, (void*)&task))
597 {
598 switch (task->build(task, message))
599 {
600 case SUCCESS:
601 /* task completed, remove it */
602 iterator->remove(iterator);
603 handle_collisions(this, task);
604 case NEED_MORE:
605 /* processed, but task needs another exchange */
606 break;
607 case FAILED:
608 default:
609 /* destroy IKE_SA, but SEND response first */
610 delete = TRUE;
611 break;
612 }
613 if (delete)
614 {
615 break;
616 }
617 }
618 iterator->destroy(iterator);
619
620 /* remove resonder SPI if IKE_SA_INIT failed */
621 if (delete && request->get_exchange_type(request) == IKE_SA_INIT)
622 {
623 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
624 id->set_responder_spi(id, 0);
625 }
626
627 /* message complete, send it */
628 DESTROY_IF(this->responding.packet);
629 status = this->ike_sa->generate_message(this->ike_sa, message,
630 &this->responding.packet);
631 charon->bus->message(charon->bus, message, FALSE);
632 message->destroy(message);
633 if (status != SUCCESS)
634 {
635 return DESTROY_ME;
636 }
637
638 charon->sender->send(charon->sender,
639 this->responding.packet->clone(this->responding.packet));
640 if (delete)
641 {
642 return DESTROY_ME;
643 }
644 return SUCCESS;
645 }
646
647 /**
648 * handle an incoming request message
649 */
650 static status_t process_request(private_task_manager_t *this,
651 message_t *message)
652 {
653 enumerator_t *enumerator;
654 iterator_t *iterator;
655 task_t *task = NULL;
656 payload_t *payload;
657 notify_payload_t *notify;
658 delete_payload_t *delete;
659
660 /* create tasks depending on request type */
661 switch (message->get_exchange_type(message))
662 {
663 case IKE_SA_INIT:
664 {
665 task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL);
666 this->passive_tasks->insert_last(this->passive_tasks, task);
667 task = (task_t*)ike_natd_create(this->ike_sa, FALSE);
668 this->passive_tasks->insert_last(this->passive_tasks, task);
669 task = (task_t*)ike_cert_pre_create(this->ike_sa, FALSE);
670 this->passive_tasks->insert_last(this->passive_tasks, task);
671 #ifdef ME
672 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
673 this->passive_tasks->insert_last(this->passive_tasks, task);
674 #endif /* ME */
675 task = (task_t*)ike_auth_create(this->ike_sa, FALSE);
676 this->passive_tasks->insert_last(this->passive_tasks, task);
677 task = (task_t*)ike_cert_post_create(this->ike_sa, FALSE);
678 this->passive_tasks->insert_last(this->passive_tasks, task);
679 task = (task_t*)ike_config_create(this->ike_sa, FALSE);
680 this->passive_tasks->insert_last(this->passive_tasks, task);
681 task = (task_t*)child_create_create(this->ike_sa, NULL);
682 this->passive_tasks->insert_last(this->passive_tasks, task);
683 task = (task_t*)ike_auth_lifetime_create(this->ike_sa, FALSE);
684 this->passive_tasks->insert_last(this->passive_tasks, task);
685 task = (task_t*)ike_mobike_create(this->ike_sa, FALSE);
686 this->passive_tasks->insert_last(this->passive_tasks, task);
687 break;
688 }
689 case CREATE_CHILD_SA:
690 { /* FIXME: we should prevent this on mediation connections */
691 bool notify_found = FALSE, ts_found = FALSE;
692 enumerator = message->create_payload_enumerator(message);
693 while (enumerator->enumerate(enumerator, &payload))
694 {
695 switch (payload->get_type(payload))
696 {
697 case NOTIFY:
698 {
699 /* if we find a rekey notify, its CHILD_SA rekeying */
700 notify = (notify_payload_t*)payload;
701 if (notify->get_notify_type(notify) == REKEY_SA &&
702 (notify->get_protocol_id(notify) == PROTO_AH ||
703 notify->get_protocol_id(notify) == PROTO_ESP))
704 {
705 notify_found = TRUE;
706 }
707 break;
708 }
709 case TRAFFIC_SELECTOR_INITIATOR:
710 case TRAFFIC_SELECTOR_RESPONDER:
711 {
712 /* if we don't find a TS, its IKE rekeying */
713 ts_found = TRUE;
714 break;
715 }
716 default:
717 break;
718 }
719 }
720 enumerator->destroy(enumerator);
721
722 if (ts_found)
723 {
724 if (notify_found)
725 {
726 task = (task_t*)child_rekey_create(this->ike_sa,
727 PROTO_NONE, 0);
728 }
729 else
730 {
731 task = (task_t*)child_create_create(this->ike_sa, NULL);
732 }
733 }
734 else
735 {
736 task = (task_t*)ike_rekey_create(this->ike_sa, FALSE);
737 }
738 this->passive_tasks->insert_last(this->passive_tasks, task);
739 break;
740 }
741 case INFORMATIONAL:
742 {
743 enumerator = message->create_payload_enumerator(message);
744 while (enumerator->enumerate(enumerator, &payload))
745 {
746 switch (payload->get_type(payload))
747 {
748 case NOTIFY:
749 {
750 notify = (notify_payload_t*)payload;
751 switch (notify->get_notify_type(notify))
752 {
753 case ADDITIONAL_IP4_ADDRESS:
754 case ADDITIONAL_IP6_ADDRESS:
755 case NO_ADDITIONAL_ADDRESSES:
756 case UPDATE_SA_ADDRESSES:
757 case NO_NATS_ALLOWED:
758 case UNACCEPTABLE_ADDRESSES:
759 case UNEXPECTED_NAT_DETECTED:
760 case COOKIE2:
761 case NAT_DETECTION_SOURCE_IP:
762 case NAT_DETECTION_DESTINATION_IP:
763 task = (task_t*)ike_mobike_create(
764 this->ike_sa, FALSE);
765 break;
766 case AUTH_LIFETIME:
767 task = (task_t*)ike_auth_lifetime_create(
768 this->ike_sa, FALSE);
769 break;
770 default:
771 break;
772 }
773 break;
774 }
775 case DELETE:
776 {
777 delete = (delete_payload_t*)payload;
778 if (delete->get_protocol_id(delete) == PROTO_IKE)
779 {
780 task = (task_t*)ike_delete_create(this->ike_sa, FALSE);
781 }
782 else
783 {
784 task = (task_t*)child_delete_create(this->ike_sa,
785 PROTO_NONE, 0);
786 }
787 break;
788 }
789 default:
790 break;
791 }
792 if (task)
793 {
794 break;
795 }
796 }
797 enumerator->destroy(enumerator);
798
799 if (task == NULL)
800 {
801 task = (task_t*)ike_dpd_create(FALSE);
802 }
803 this->passive_tasks->insert_last(this->passive_tasks, task);
804 break;
805 }
806 #ifdef ME
807 case ME_CONNECT:
808 {
809 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
810 this->passive_tasks->insert_last(this->passive_tasks, task);
811 }
812 #endif /* ME */
813 default:
814 break;
815 }
816
817 /* let the tasks process the message */
818 iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE);
819 while (iterator->iterate(iterator, (void*)&task))
820 {
821 switch (task->process(task, message))
822 {
823 case SUCCESS:
824 /* task completed, remove it */
825 iterator->remove(iterator);
826 task->destroy(task);
827 break;
828 case NEED_MORE:
829 /* processed, but task needs at least another call to build() */
830 break;
831 case FAILED:
832 default:
833 /* critical failure, destroy IKE_SA */
834 iterator->remove(iterator);
835 iterator->destroy(iterator);
836 task->destroy(task);
837 return DESTROY_ME;
838 }
839 }
840 iterator->destroy(iterator);
841
842 return build_response(this, message);
843 }
844
845 /**
846 * Implementation of task_manager_t.process_message
847 */
848 static status_t process_message(private_task_manager_t *this, message_t *msg)
849 {
850 u_int32_t mid = msg->get_message_id(msg);
851
852 if (msg->get_request(msg))
853 {
854 if (mid == this->responding.mid)
855 {
856 charon->bus->message(charon->bus, msg, TRUE);
857 if (process_request(this, msg) != SUCCESS)
858 {
859 flush(this);
860 return DESTROY_ME;
861 }
862 this->responding.mid++;
863 }
864 else if ((mid == this->responding.mid - 1) && this->responding.packet)
865 {
866 packet_t *clone;
867 host_t *me, *other;
868
869 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
870 "retransmitting response", mid);
871 clone = this->responding.packet->clone(this->responding.packet);
872 me = msg->get_destination(msg);
873 other = msg->get_source(msg);
874 clone->set_source(clone, me->clone(me));
875 clone->set_destination(clone, other->clone(other));
876 charon->sender->send(charon->sender, clone);
877 }
878 else
879 {
880 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
881 mid, this->responding.mid);
882 }
883 }
884 else
885 {
886 if (mid == this->initiating.mid)
887 {
888 if (process_response(this, msg) != SUCCESS)
889 {
890 flush(this);
891 return DESTROY_ME;
892 }
893 }
894 else
895 {
896 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
897 mid, this->initiating.mid);
898 return SUCCESS;
899 }
900 }
901 return SUCCESS;
902 }
903
904 /**
905 * Implementation of task_manager_t.queue_task
906 */
907 static void queue_task(private_task_manager_t *this, task_t *task)
908 {
909 if (task->get_type(task) == IKE_MOBIKE)
910 { /* there is no need to queue more than one mobike task */
911 iterator_t *iterator;
912 task_t *current;
913
914 iterator = this->queued_tasks->create_iterator(this->queued_tasks, TRUE);
915 while (iterator->iterate(iterator, (void**)&current))
916 {
917 if (current->get_type(current) == IKE_MOBIKE)
918 {
919 iterator->destroy(iterator);
920 task->destroy(task);
921 return;
922 }
923 }
924 iterator->destroy(iterator);
925 }
926 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
927 this->queued_tasks->insert_last(this->queued_tasks, task);
928 }
929
930 /**
931 * Implementation of task_manager_t.adopt_tasks
932 */
933 static void adopt_tasks(private_task_manager_t *this, private_task_manager_t *other)
934 {
935 task_t *task;
936
937 /* move queued tasks from other to this */
938 while (other->queued_tasks->remove_last(other->queued_tasks,
939 (void**)&task) == SUCCESS)
940 {
941 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
942 task->migrate(task, this->ike_sa);
943 this->queued_tasks->insert_first(this->queued_tasks, task);
944 }
945 }
946
947 /**
948 * Implementation of task_manager_t.busy
949 */
950 static bool busy(private_task_manager_t *this)
951 {
952 return (this->active_tasks->get_count(this->active_tasks) > 0);
953 }
954
955 /**
956 * Implementation of task_manager_t.reset
957 */
958 static void reset(private_task_manager_t *this,
959 u_int32_t initiate, u_int32_t respond)
960 {
961 task_t *task;
962
963 /* reset message counters and retransmit packets */
964 DESTROY_IF(this->responding.packet);
965 DESTROY_IF(this->initiating.packet);
966 this->responding.packet = NULL;
967 this->initiating.packet = NULL;
968 if (initiate != UINT_MAX)
969 {
970 this->initiating.mid = initiate;
971 }
972 if (respond != UINT_MAX)
973 {
974 this->responding.mid = respond;
975 }
976 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
977
978 /* reset active tasks */
979 while (this->active_tasks->remove_last(this->active_tasks,
980 (void**)&task) == SUCCESS)
981 {
982 task->migrate(task, this->ike_sa);
983 this->queued_tasks->insert_first(this->queued_tasks, task);
984 }
985
986 this->reset = TRUE;
987 }
988
989 /**
990 * Implementation of task_manager_t.destroy
991 */
992 static void destroy(private_task_manager_t *this)
993 {
994 flush(this);
995
996 this->active_tasks->destroy(this->active_tasks);
997 this->queued_tasks->destroy(this->queued_tasks);
998 this->passive_tasks->destroy(this->passive_tasks);
999
1000 DESTROY_IF(this->responding.packet);
1001 DESTROY_IF(this->initiating.packet);
1002 free(this);
1003 }
1004
1005 /*
1006 * see header file
1007 */
1008 task_manager_t *task_manager_create(ike_sa_t *ike_sa)
1009 {
1010 private_task_manager_t *this = malloc_thing(private_task_manager_t);
1011
1012 this->public.process_message = (status_t(*)(task_manager_t*,message_t*))process_message;
1013 this->public.queue_task = (void(*)(task_manager_t*,task_t*))queue_task;
1014 this->public.initiate = (status_t(*)(task_manager_t*))build_request;
1015 this->public.retransmit = (status_t(*)(task_manager_t*,u_int32_t))retransmit;
1016 this->public.reset = (void(*)(task_manager_t*,u_int32_t,u_int32_t))reset;
1017 this->public.adopt_tasks = (void(*)(task_manager_t*,task_manager_t*))adopt_tasks;
1018 this->public.busy = (bool(*)(task_manager_t*))busy;
1019 this->public.destroy = (void(*)(task_manager_t*))destroy;
1020
1021 this->ike_sa = ike_sa;
1022 this->responding.packet = NULL;
1023 this->initiating.packet = NULL;
1024 this->responding.mid = 0;
1025 this->initiating.mid = 0;
1026 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1027 this->queued_tasks = linked_list_create();
1028 this->active_tasks = linked_list_create();
1029 this->passive_tasks = linked_list_create();
1030 this->reset = FALSE;
1031
1032 return &this->public;
1033 }