fixed some compiler warnings
[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 DESTROY_IF(this->initiating.packet);
446 status = this->ike_sa->generate_message(this->ike_sa, message,
447 &this->initiating.packet);
448 message->destroy(message);
449 if (status != SUCCESS)
450 {
451 /* message generation failed. There is nothing more to do than to
452 * close the SA */
453 flush(this);
454 return DESTROY_ME;
455 }
456
457 return retransmit(this, this->initiating.mid);
458 }
459
460 /**
461 * handle an incoming response message
462 */
463 static status_t process_response(private_task_manager_t *this,
464 message_t *message)
465 {
466 iterator_t *iterator;
467 task_t *task;
468
469 if (message->get_exchange_type(message) != this->initiating.type)
470 {
471 DBG1(DBG_IKE, "received %N response, but expected %N",
472 exchange_type_names, message->get_exchange_type(message),
473 exchange_type_names, this->initiating.type);
474 return DESTROY_ME;
475 }
476
477 /* catch if we get resetted while processing */
478 this->reset = FALSE;
479 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
480 while (iterator->iterate(iterator, (void*)&task))
481 {
482 switch (task->process(task, message))
483 {
484 case SUCCESS:
485 /* task completed, remove it */
486 iterator->remove(iterator);
487 task->destroy(task);
488 break;
489 case NEED_MORE:
490 /* processed, but task needs another exchange */
491 break;
492 case FAILED:
493 default:
494 /* critical failure, destroy IKE_SA */
495 iterator->destroy(iterator);
496 return DESTROY_ME;
497 }
498 if (this->reset)
499 { /* start all over again if we were reset */
500 this->reset = FALSE;
501 iterator->destroy(iterator);
502 return build_request(this);
503 }
504 }
505 iterator->destroy(iterator);
506
507 this->initiating.mid++;
508 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
509
510 return build_request(this);
511 }
512
513 /**
514 * handle exchange collisions
515 */
516 static void handle_collisions(private_task_manager_t *this, task_t *task)
517 {
518 iterator_t *iterator;
519 task_t *active;
520 task_type_t type;
521
522 type = task->get_type(task);
523
524 /* do we have to check */
525 if (type == IKE_REKEY || type == CHILD_REKEY ||
526 type == CHILD_DELETE || type == IKE_DELETE || type == IKE_REAUTH)
527 {
528 /* find an exchange collision, and notify these tasks */
529 iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
530 while (iterator->iterate(iterator, (void**)&active))
531 {
532 switch (active->get_type(active))
533 {
534 case IKE_REKEY:
535 if (type == IKE_REKEY || type == IKE_DELETE ||
536 type == IKE_REAUTH)
537 {
538 ike_rekey_t *rekey = (ike_rekey_t*)active;
539 rekey->collide(rekey, task);
540 break;
541 }
542 continue;
543 case CHILD_REKEY:
544 if (type == CHILD_REKEY || type == CHILD_DELETE)
545 {
546 child_rekey_t *rekey = (child_rekey_t*)active;
547 rekey->collide(rekey, task);
548 break;
549 }
550 continue;
551 default:
552 continue;
553 }
554 iterator->destroy(iterator);
555 return;
556 }
557 iterator->destroy(iterator);
558 }
559 /* destroy task if not registered in any active task */
560 task->destroy(task);
561 }
562
563 /**
564 * build a response depending on the "passive" task list
565 */
566 static status_t build_response(private_task_manager_t *this, message_t *request)
567 {
568 iterator_t *iterator;
569 task_t *task;
570 message_t *message;
571 host_t *me, *other;
572 bool delete = FALSE;
573 status_t status;
574
575 me = request->get_destination(request);
576 other = request->get_source(request);
577
578 message = message_create();
579 message->set_exchange_type(message, request->get_exchange_type(request));
580 /* send response along the path the request came in */
581 message->set_source(message, me->clone(me));
582 message->set_destination(message, other->clone(other));
583 message->set_message_id(message, this->responding.mid);
584 message->set_request(message, FALSE);
585
586 iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE);
587 while (iterator->iterate(iterator, (void*)&task))
588 {
589 switch (task->build(task, message))
590 {
591 case SUCCESS:
592 /* task completed, remove it */
593 iterator->remove(iterator);
594 handle_collisions(this, task);
595 case NEED_MORE:
596 /* processed, but task needs another exchange */
597 break;
598 case FAILED:
599 default:
600 /* destroy IKE_SA, but SEND response first */
601 delete = TRUE;
602 break;
603 }
604 if (delete)
605 {
606 break;
607 }
608 }
609 iterator->destroy(iterator);
610
611 /* remove resonder SPI if IKE_SA_INIT failed */
612 if (delete && request->get_exchange_type(request) == IKE_SA_INIT)
613 {
614 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
615 id->set_responder_spi(id, 0);
616 }
617
618 /* message complete, send it */
619 DESTROY_IF(this->responding.packet);
620 status = this->ike_sa->generate_message(this->ike_sa, message,
621 &this->responding.packet);
622 charon->bus->message(charon->bus, message, FALSE);
623 message->destroy(message);
624 if (status != SUCCESS)
625 {
626 return DESTROY_ME;
627 }
628
629 charon->sender->send(charon->sender,
630 this->responding.packet->clone(this->responding.packet));
631 if (delete)
632 {
633 return DESTROY_ME;
634 }
635 return SUCCESS;
636 }
637
638 /**
639 * handle an incoming request message
640 */
641 static status_t process_request(private_task_manager_t *this,
642 message_t *message)
643 {
644 iterator_t *iterator;
645 task_t *task = NULL;
646 payload_t *payload;
647 notify_payload_t *notify;
648 delete_payload_t *delete;
649
650 /* create tasks depending on request type */
651 switch (message->get_exchange_type(message))
652 {
653 case IKE_SA_INIT:
654 {
655 task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL);
656 this->passive_tasks->insert_last(this->passive_tasks, task);
657 task = (task_t*)ike_natd_create(this->ike_sa, FALSE);
658 this->passive_tasks->insert_last(this->passive_tasks, task);
659 task = (task_t*)ike_cert_pre_create(this->ike_sa, FALSE);
660 this->passive_tasks->insert_last(this->passive_tasks, task);
661 #ifdef ME
662 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
663 this->passive_tasks->insert_last(this->passive_tasks, task);
664 #endif /* ME */
665 task = (task_t*)ike_auth_create(this->ike_sa, FALSE);
666 this->passive_tasks->insert_last(this->passive_tasks, task);
667 task = (task_t*)ike_cert_post_create(this->ike_sa, FALSE);
668 this->passive_tasks->insert_last(this->passive_tasks, task);
669 task = (task_t*)ike_config_create(this->ike_sa, FALSE);
670 this->passive_tasks->insert_last(this->passive_tasks, task);
671 task = (task_t*)child_create_create(this->ike_sa, NULL);
672 this->passive_tasks->insert_last(this->passive_tasks, task);
673 task = (task_t*)ike_auth_lifetime_create(this->ike_sa, FALSE);
674 this->passive_tasks->insert_last(this->passive_tasks, task);
675 task = (task_t*)ike_mobike_create(this->ike_sa, FALSE);
676 this->passive_tasks->insert_last(this->passive_tasks, task);
677 break;
678 }
679 case CREATE_CHILD_SA:
680 { /* FIXME: we should prevent this on mediation connections */
681 bool notify_found = FALSE, ts_found = FALSE;
682 iterator = message->get_payload_iterator(message);
683 while (iterator->iterate(iterator, (void**)&payload))
684 {
685 switch (payload->get_type(payload))
686 {
687 case NOTIFY:
688 {
689 /* if we find a rekey notify, its CHILD_SA rekeying */
690 notify = (notify_payload_t*)payload;
691 if (notify->get_notify_type(notify) == REKEY_SA &&
692 (notify->get_protocol_id(notify) == PROTO_AH ||
693 notify->get_protocol_id(notify) == PROTO_ESP))
694 {
695 notify_found = TRUE;
696 }
697 break;
698 }
699 case TRAFFIC_SELECTOR_INITIATOR:
700 case TRAFFIC_SELECTOR_RESPONDER:
701 {
702 /* if we don't find a TS, its IKE rekeying */
703 ts_found = TRUE;
704 break;
705 }
706 default:
707 break;
708 }
709 }
710 iterator->destroy(iterator);
711
712 if (ts_found)
713 {
714 if (notify_found)
715 {
716 task = (task_t*)child_rekey_create(this->ike_sa, NULL);
717 }
718 else
719 {
720 task = (task_t*)child_create_create(this->ike_sa, NULL);
721 }
722 }
723 else
724 {
725 task = (task_t*)ike_rekey_create(this->ike_sa, FALSE);
726 }
727 this->passive_tasks->insert_last(this->passive_tasks, task);
728 break;
729 }
730 case INFORMATIONAL:
731 {
732 iterator = message->get_payload_iterator(message);
733 while (iterator->iterate(iterator, (void**)&payload))
734 {
735 switch (payload->get_type(payload))
736 {
737 case NOTIFY:
738 {
739 notify = (notify_payload_t*)payload;
740 switch (notify->get_notify_type(notify))
741 {
742 case ADDITIONAL_IP4_ADDRESS:
743 case ADDITIONAL_IP6_ADDRESS:
744 case NO_ADDITIONAL_ADDRESSES:
745 case UPDATE_SA_ADDRESSES:
746 case NO_NATS_ALLOWED:
747 case UNACCEPTABLE_ADDRESSES:
748 case UNEXPECTED_NAT_DETECTED:
749 case COOKIE2:
750 case NAT_DETECTION_SOURCE_IP:
751 case NAT_DETECTION_DESTINATION_IP:
752 task = (task_t*)ike_mobike_create(
753 this->ike_sa, FALSE);
754 break;
755 case AUTH_LIFETIME:
756 task = (task_t*)ike_auth_lifetime_create(
757 this->ike_sa, FALSE);
758 break;
759 default:
760 break;
761 }
762 break;
763 }
764 case DELETE:
765 {
766 delete = (delete_payload_t*)payload;
767 if (delete->get_protocol_id(delete) == PROTO_IKE)
768 {
769 task = (task_t*)ike_delete_create(this->ike_sa, FALSE);
770 }
771 else
772 {
773 task = (task_t*)child_delete_create(this->ike_sa, NULL);
774 }
775 break;
776 }
777 default:
778 break;
779 }
780 if (task)
781 {
782 break;
783 }
784 }
785 iterator->destroy(iterator);
786
787 if (task == NULL)
788 {
789 task = (task_t*)ike_dpd_create(FALSE);
790 }
791 this->passive_tasks->insert_last(this->passive_tasks, task);
792 break;
793 }
794 #ifdef ME
795 case ME_CONNECT:
796 {
797 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
798 this->passive_tasks->insert_last(this->passive_tasks, task);
799 }
800 #endif /* ME */
801 default:
802 break;
803 }
804
805 /* let the tasks process the message */
806 iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE);
807 while (iterator->iterate(iterator, (void*)&task))
808 {
809 switch (task->process(task, message))
810 {
811 case SUCCESS:
812 /* task completed, remove it */
813 iterator->remove(iterator);
814 task->destroy(task);
815 break;
816 case NEED_MORE:
817 /* processed, but task needs at least another call to build() */
818 break;
819 case FAILED:
820 default:
821 /* critical failure, destroy IKE_SA */
822 iterator->destroy(iterator);
823 return DESTROY_ME;
824 }
825 }
826 iterator->destroy(iterator);
827
828 return build_response(this, message);
829 }
830
831 /**
832 * Implementation of task_manager_t.process_message
833 */
834 static status_t process_message(private_task_manager_t *this, message_t *msg)
835 {
836 u_int32_t mid = msg->get_message_id(msg);
837
838 if (msg->get_request(msg))
839 {
840 if (mid == this->responding.mid)
841 {
842 charon->bus->message(charon->bus, msg, TRUE);
843 if (process_request(this, msg) != SUCCESS)
844 {
845 flush(this);
846 return DESTROY_ME;
847 }
848 this->responding.mid++;
849 }
850 else if ((mid == this->responding.mid - 1) && this->responding.packet)
851 {
852 packet_t *clone;
853 host_t *me, *other;
854
855 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
856 "retransmitting response", mid);
857 clone = this->responding.packet->clone(this->responding.packet);
858 me = msg->get_destination(msg);
859 other = msg->get_source(msg);
860 clone->set_source(clone, me->clone(me));
861 clone->set_destination(clone, other->clone(other));
862 charon->sender->send(charon->sender, clone);
863 }
864 else
865 {
866 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
867 mid, this->responding.mid);
868 }
869 }
870 else
871 {
872 if (mid == this->initiating.mid)
873 {
874 if (process_response(this, msg) != SUCCESS)
875 {
876 flush(this);
877 return DESTROY_ME;
878 }
879 }
880 else
881 {
882 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
883 mid, this->initiating.mid);
884 return SUCCESS;
885 }
886 }
887 return SUCCESS;
888 }
889
890 /**
891 * Implementation of task_manager_t.queue_task
892 */
893 static void queue_task(private_task_manager_t *this, task_t *task)
894 {
895 if (task->get_type(task) == IKE_MOBIKE)
896 { /* there is no need to queue more than one mobike task */
897 iterator_t *iterator;
898 task_t *current;
899
900 iterator = this->queued_tasks->create_iterator(this->queued_tasks, TRUE);
901 while (iterator->iterate(iterator, (void**)&current))
902 {
903 if (current->get_type(current) == IKE_MOBIKE)
904 {
905 iterator->destroy(iterator);
906 task->destroy(task);
907 return;
908 }
909 }
910 iterator->destroy(iterator);
911 }
912 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
913 this->queued_tasks->insert_last(this->queued_tasks, task);
914 }
915
916 /**
917 * Implementation of task_manager_t.adopt_tasks
918 */
919 static void adopt_tasks(private_task_manager_t *this, private_task_manager_t *other)
920 {
921 task_t *task;
922
923 /* move queued tasks from other to this */
924 while (other->queued_tasks->remove_last(other->queued_tasks,
925 (void**)&task) == SUCCESS)
926 {
927 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
928 task->migrate(task, this->ike_sa);
929 this->queued_tasks->insert_first(this->queued_tasks, task);
930 }
931
932 /* reset active tasks and move them to others queued tasks */
933 while (other->active_tasks->remove_last(other->active_tasks,
934 (void**)&task) == SUCCESS)
935 {
936 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
937 task->migrate(task, this->ike_sa);
938 this->queued_tasks->insert_first(this->queued_tasks, task);
939 }
940 }
941
942 /**
943 * Implementation of task_manager_t.busy
944 */
945 static bool busy(private_task_manager_t *this)
946 {
947 return (this->active_tasks->get_count(this->active_tasks) > 0);
948 }
949
950 /**
951 * Implementation of task_manager_t.reset
952 */
953 static void reset(private_task_manager_t *this)
954 {
955 task_t *task;
956
957 /* reset message counters and retransmit packets */
958 DESTROY_IF(this->responding.packet);
959 DESTROY_IF(this->initiating.packet);
960 this->responding.packet = NULL;
961 this->initiating.packet = NULL;
962 this->responding.mid = 0;
963 this->initiating.mid = 0;
964 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
965
966 /* reset active tasks */
967 while (this->active_tasks->remove_last(this->active_tasks,
968 (void**)&task) == SUCCESS)
969 {
970 task->migrate(task, this->ike_sa);
971 this->queued_tasks->insert_first(this->queued_tasks, task);
972 }
973
974 this->reset = TRUE;
975 }
976
977 /**
978 * Implementation of task_manager_t.destroy
979 */
980 static void destroy(private_task_manager_t *this)
981 {
982 flush(this);
983
984 this->active_tasks->destroy(this->active_tasks);
985 this->queued_tasks->destroy(this->queued_tasks);
986 this->passive_tasks->destroy(this->passive_tasks);
987
988 DESTROY_IF(this->responding.packet);
989 DESTROY_IF(this->initiating.packet);
990 free(this);
991 }
992
993 /*
994 * see header file
995 */
996 task_manager_t *task_manager_create(ike_sa_t *ike_sa)
997 {
998 private_task_manager_t *this = malloc_thing(private_task_manager_t);
999
1000 this->public.process_message = (status_t(*)(task_manager_t*,message_t*))process_message;
1001 this->public.queue_task = (void(*)(task_manager_t*,task_t*))queue_task;
1002 this->public.initiate = (status_t(*)(task_manager_t*))build_request;
1003 this->public.retransmit = (status_t(*)(task_manager_t*,u_int32_t))retransmit;
1004 this->public.reset = (void(*)(task_manager_t*))reset;
1005 this->public.adopt_tasks = (void(*)(task_manager_t*,task_manager_t*))adopt_tasks;
1006 this->public.busy = (bool(*)(task_manager_t*))busy;
1007 this->public.destroy = (void(*)(task_manager_t*))destroy;
1008
1009 this->ike_sa = ike_sa;
1010 this->responding.packet = NULL;
1011 this->initiating.packet = NULL;
1012 this->responding.mid = 0;
1013 this->initiating.mid = 0;
1014 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1015 this->queued_tasks = linked_list_create();
1016 this->active_tasks = linked_list_create();
1017 this->passive_tasks = linked_list_create();
1018 this->reset = FALSE;
1019
1020 return &this->public;
1021 }