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